From ac7bc6ad30569b22cd8721d3bd702acf0e13ddae Mon Sep 17 00:00:00 2001 From: cxam Date: Mon, 18 May 2020 19:50:34 +0100 Subject: [PATCH 1/3] Add singleValue boolean flag --- src/flags.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/flags.ts b/src/flags.ts index 58adfac..c6386be 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -36,6 +36,7 @@ export type IOptionFlag = IFlagBase & { helpValue?: string; default?: Default; multiple: boolean; + singleValue: boolean; input: string[]; options?: string[]; } @@ -62,6 +63,7 @@ export function build(defaults: Partial>): Definition { ...options, input: [] as string[], multiple: Boolean(options.multiple), + singleValue: Boolean(options.singleValue), type: 'option', } as any } From cdcad8cf037b2bca2223da8305f5dd7689aa1247 Mon Sep 17 00:00:00 2001 From: cxam Date: Mon, 18 May 2020 19:51:38 +0100 Subject: [PATCH 2/3] Skip multiple flag if singleValue is set --- src/parse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parse.ts b/src/parse.ts index ff85dd1..6c20d46 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -141,7 +141,7 @@ export class Parser Date: Mon, 18 May 2020 19:51:56 +0100 Subject: [PATCH 3/3] Add tests for singleValue flag option --- test/parse.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/parse.test.ts b/test/parse.test.ts index f697b27..63d8fa9 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -290,6 +290,42 @@ See more help with --help`) }) }) + describe('multiple flags with single value', () => { + it('parses multiple flags with single value', () => { + const out = parse(['--bar', 'a', 'b', '--bar=c', '--baz=d', 'e'], { + args: [{name: 'argOne'}], + flags: { + bar: flags.string({multiple: true, singleValue: true}), + baz: flags.string({multiple: true}), + }, + }) + expect(out.flags.baz.join('|')).to.equal('d|e') + expect(out.flags.bar.join('|')).to.equal('a|c') + expect(out.args).to.deep.equal({argOne: 'b'}) + }) + + it('parses multiple flags with single value multiple args', () => { + const out = parse(['c', '--bar', 'a', 'b'], { + args: [{name: 'argOne'}, {name: 'argTwo'}], + flags: { + bar: flags.string({multiple: true, singleValue: true}), + }, + }) + expect(out.flags.bar.join('|')).to.equal('a') + expect(out.args).to.deep.equal({argOne: 'c', argTwo: 'b'}) + }) + + it('fails to parse with single value and no args option', () => { + expect(() => { + parse(['--bar', 'a', 'b'], { + flags: { + bar: flags.string({multiple: true, singleValue: true}), + }, + }) + }).to.throw('Unexpected argument: b') + }) + }) + describe('strict: false', () => { it('skips flag parsing after "--"', () => { const out = parse(['foo', 'bar', '--', '--myflag'], {