From de79b1fa2c1bfbbd6d1585a81eb5078270ab3d1a Mon Sep 17 00:00:00 2001 From: firefish5000 <1813610+firefish5000@users.noreply.github.com> Date: Mon, 16 Nov 2020 01:29:28 -0600 Subject: [PATCH 1/4] Add failing test --- test/parse.test.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/parse.test.ts b/test/parse.test.ts index 8b8948e..18a26d5 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -426,6 +426,58 @@ See more help with --help`) expect(out.flags).to.deep.include({hello: 'world'}) }) + it('ends when a flag that wants input is found', () => { + const out = parse( + ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world', 'me'], + { + flags: { + foo: flags.string({multiple: true}), + bar: flags.string(), + }, + args: [{name: 'arg', required: false}], + }, + ) + expect(out.flags).to.deep.include({ + foo: ['./a.txt', './b.txt', './c.txt'], + }) + expect(out.flags).to.deep.include({bar: 'world'}) + expect(out.argv).to.deep.equal(['me']) + }) + it('ends when a flag that does not want input is found', () => { + const out = parse( + ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world'], + { + flags: { + foo: flags.string({multiple: true}), + bar: flags.boolean(), + }, + args: [{name: 'arg', required: false}], + }, + ) + expect(out.flags).to.deep.include({ + foo: ['./a.txt', './b.txt', './c.txt'], + }) + expect(out.flags).to.deep.include({bar: true}) + expect(out.argv).to.deep.equal(['world']) + }) + it('ends when next flag is found with strict=false', () => { + const out = parse( + ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world'], + { + flags: { + foo: flags.string({multiple: true}), + bar: flags.boolean(), + }, + strict: false, + }, + ) + /* expect(out.flags).to.deep.include({ + foo: ['./a.txt', './b.txt', './c.txt'], + }) */ + expect(out.flags).to.deep.include({bar: true}) + expect(out.argv).to.deep.equal(['world']) + }) + it('flag multiple with arguments', () => { const out = parse( ['--foo', './a.txt', './b.txt', './c.txt', '--', '15'], From ffea3981e8797257e844a3531a531c78ace69005 Mon Sep 17 00:00:00 2001 From: firefish5000 <1813610+firefish5000@users.noreply.github.com> Date: Mon, 16 Nov 2020 02:05:10 -0600 Subject: [PATCH 2/4] Simple fix for added tests --- src/parse.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/parse.ts b/src/parse.ts index ff85dd1..45368ce 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -119,6 +119,7 @@ export class Parser 2) { From 120e12174da0a1c40a393cea99129ed85c9128c6 Mon Sep 17 00:00:00 2001 From: firefish5000 <1813610+firefish5000@users.noreply.github.com> Date: Mon, 16 Nov 2020 02:34:52 -0600 Subject: [PATCH 3/4] Add more tests covering char flags --- test/parse.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/test/parse.test.ts b/test/parse.test.ts index 18a26d5..7b6c078 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -78,7 +78,19 @@ describe('parse', () => { expect(Boolean(out.flags.myflag)).to.equal(true) expect(Boolean(out.flags.force)).to.equal(true) }) + + it('parses short flags with values', () => { + const out = parse(['-mf', 'cat'], { + flags: { + force: flags.string({char: 'f'}), + myflag: flags.boolean({char: 'm'}), + }, + }) + expect(out.flags.force).to.equal('cat') + expect(out.flags.myflag).to.equal(true) + }) }) + it('parses flag value with "=" to separate', () => { const out = parse(['--myflag=foo'], { flags: { @@ -115,6 +127,18 @@ describe('parse', () => { expect(out.flags).to.deep.equal({myflag: ''}) }) + it('fails with multiple char option flags', () => { + expect(() => parse( + ['-fb', 'foo'], + { + flags: { + foo: flags.string({char: 'f'}), + bar: flags.string({char: 'b'}), + }, + }, + )).to.throw() + }) + it('requires required flag', () => { expect(() => { parse([], { @@ -426,7 +450,7 @@ See more help with --help`) expect(out.flags).to.deep.include({hello: 'world'}) }) - it('ends when a flag that wants input is found', () => { + it('ends when another option flag is found', () => { const out = parse( ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world', 'me'], { @@ -443,7 +467,7 @@ See more help with --help`) expect(out.flags).to.deep.include({bar: 'world'}) expect(out.argv).to.deep.equal(['me']) }) - it('ends when a flag that does not want input is found', () => { + it('ends when a non-option flag is found', () => { const out = parse( ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world'], { @@ -477,6 +501,21 @@ See more help with --help`) expect(out.flags).to.deep.include({bar: true}) expect(out.argv).to.deep.equal(['world']) }) + it('handles char flags intuitively', () => { + const out = parse( + ['-fb', 'foo', 'bar'], + { + flags: { + foo: flags.boolean({char: 'f'}), + bar: flags.string({char: 'b', multiple: true}), + }, + }, + ) + expect(out.flags).to.deep.include({ + bar: ['foo', 'bar'], + foo: true, + }) + }) it('flag multiple with arguments', () => { const out = parse( From 76fa11c8b06914c0b071bbfbd6d04b0f3f305324 Mon Sep 17 00:00:00 2001 From: firefish5000 <1813610+firefish5000@users.noreply.github.com> Date: Mon, 16 Nov 2020 03:07:16 -0600 Subject: [PATCH 4/4] Add tests for --string '--string' --- test/parse.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/parse.test.ts b/test/parse.test.ts index 7b6c078..3aa795c 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -127,6 +127,16 @@ describe('parse', () => { expect(out.flags).to.deep.equal({myflag: ''}) }) + it('parses flag as an arg to a flag option', () => { + const out = parse(['--foo', '--bar'], { + flags: { + foo: flags.string(), + bar: flags.string(), + }, + }) + expect(out.flags).to.deep.equal({foo: '--bar'}) + }) + it('fails with multiple char option flags', () => { expect(() => parse( ['-fb', 'foo'], @@ -450,6 +460,21 @@ See more help with --help`) expect(out.flags).to.deep.include({hello: 'world'}) }) + it('always eats the first argument', () => { + const out = parse(['--foo', '--bar', 'camel', '--car'], { + flags: { + foo: flags.string({multiple: true}), + bar: flags.boolean({default: false}), + car: flags.boolean({default: false}), + }, + }) + expect(out.flags).to.deep.equal({ + foo: ['--bar', 'camel'], + bar: false, + car: true, + }) + }) + it('ends when another option flag is found', () => { const out = parse( ['--foo', './a.txt', './b.txt', './c.txt', '--bar', 'world', 'me'],