diff --git a/src/flags.ts b/src/flags.ts index e27f532..fcc0b6c 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -40,6 +40,7 @@ export type IOptionFlag = IFlagBase & { helpValue?: string; default?: Default; multiple: boolean; + singleValue: boolean; input: string[]; options?: string[]; } @@ -73,6 +74,7 @@ export function build(defaults: Partial>): Definition { ...options, input: [] as string[], multiple: Boolean(options.multiple), + singleValue: Boolean(options.singleValue), type: 'option', } as any } 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 { + 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'], {