Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'multipleNonGreedy' flag option to assign only one value per multiple flag #880

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/interfaces/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ export type OptionFlagProps = FlagProps & {
helpValue?: string
options?: readonly string[]
multiple?: boolean
/**
* Parse one value per flag; allow `-m val1 -m val2`, disallow `-m val1 val2`.
* Set to true to use "multiple: true" flags together with args.
* Only respected if multiple is set to true.
*/
multipleNonGreedy?: boolean
/**
* Delimiter to separate the values for a multiple value flag.
* Only respected if multiple is set to true. Default behavior is to
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class Parser<
}
}

if (parsingFlags && this.currentFlag && this.currentFlag.multiple) {
if (parsingFlags && this.currentFlag && this.currentFlag.multiple && !this.currentFlag.multipleNonGreedy) {
this.raw.push({flag: this.currentFlag.name, input, type: 'flag'})
continue
}
Expand Down
42 changes: 42 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,48 @@ See more help with --help`)
})
})

describe('multiple flags with single value', () => {
it('parses multiple flags with single value', async () => {
const out = await parse(['--bar', 'a', 'b', '--bar=c', '--baz=d', 'e'], {
args: {argOne: Args.string()},
flags: {
bar: Flags.string({multiple: true, multipleNonGreedy: 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', async () => {
const out = await parse(['c', '--bar', 'a', 'b'], {
args: {argOne: Args.string(), argTwo: Args.string()},
flags: {
bar: Flags.string({multiple: true, multipleNonGreedy: 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', async () => {
let message = ''

try {
await parse(['--bar', 'a', 'b'], {
flags: {
bar: Flags.string({multiple: true, multipleNonGreedy: true}),
},
})
} catch (error: any) {
message = error.message
}

expect(message).to.include('Unexpected argument: b')
})
})

describe('strict: false', () => {
it('skips flag parsing after "--"', async () => {
const out = await parse(['foo', 'bar', '--', '--myflag'], {
Expand Down