Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Handle flags with multiple inputs without the flag before each input #25

Merged
merged 6 commits into from
Jun 1, 2018
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/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
private readonly raw: ParsingToken[] = []
private readonly booleanFlags: { [k: string]: Flags.IBooleanFlag<any> }
private readonly context: any
private currentFlag?: Flags.IOptionFlag<any>
constructor(private readonly input: T) {
const {pickBy} = m.util
this.context = input.context || {}
Expand Down Expand Up @@ -92,6 +93,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
}
const flag = this.input.flags[name]
if (flag.type === 'option') {
this.currentFlag = flag
let input
if (long || arg.length < 3) {
input = this.argv.shift()
Expand Down Expand Up @@ -125,6 +127,10 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
}
// not actually a flag if it reaches here so parse as an arg
}
if (parsingFlags && this.currentFlag && this.currentFlag.multiple) {
this.raw.push({type: 'flag', flag: this.currentFlag.name, input})
continue
}
// not a flag, parse as arg
const arg = this.input.args[this._argTokens.length]
if (arg) arg.input = input
Expand Down
27 changes: 27 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,33 @@ See more help with --help`)
// })
})

describe('flag with multiple inputs', () => {
it('flag multiple with flag in the middle', () => {
const out = parse(['--foo=bar', '--foo', '100', '--hello', 'world'], {
flags: {foo: flags.string({multiple: true}), hello: flags.string()},
})
expect(out.flags).to.deep.include({foo: ['bar', '100']})
expect(out.flags).to.deep.include({hello: 'world'})
})

it('flag multiple without flag in the middle', () => {
const out = parse(['--foo', './a.txt', './b.txt', './c.txt', '--hello', 'world'], {
flags: {foo: flags.string({multiple: true}), hello: flags.string()},
})
expect(out.flags).to.deep.include({foo: ['./a.txt', './b.txt', './c.txt']})
expect(out.flags).to.deep.include({hello: 'world'})
})

it('flag multiple with arguments', () => {
const out = parse(['--foo', './a.txt', './b.txt', './c.txt', '--', '15'], {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice job catching this case, I wasn't sure how it would behave

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

than you :)

args: [{name: 'num'}],
flags: {foo: flags.string({multiple: true})},
})
expect(out.flags).to.deep.include({foo: ['./a.txt', './b.txt', './c.txt']})
expect(out.args).to.deep.include({num: '15'})
})
})

describe('defaults', () => {
it('defaults', () => {
const out = parse([], {
Expand Down