Skip to content

Commit

Permalink
fix(core): fix type cast for variadic arguments, fix #1111
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 10, 2023
1 parent a61ca03 commit f793cdd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/command/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,19 @@ export namespace Argv {
argv.source = this.name + ' ' + Argv.stringify(argv)
}

let lastArgDecl: Declaration

while (!argv.error && argv.tokens?.length) {
const token = argv.tokens[0]
let { content, quoted } = token

// variadic argument
const argDecl = this._arguments[args.length] || lastArgDecl
if (args.length === this._arguments.length - 1 && argDecl.variadic) {
lastArgDecl = argDecl
}

// greedy argument
const argDecl = this._arguments[args.length]
if (content[0] !== '-' && resolveConfig(argDecl?.type).greedy) {
args.push(Argv.parseValue(Argv.stringify(argv), true, 'argument', argv, argDecl))
break
Expand Down
7 changes: 6 additions & 1 deletion packages/core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ describe('Parser API', () => {
})

it('valued override', () => {
cmd = app.command('test <msg>')
cmd = app.command('test2 <msg>')
cmd.option('writer', '-w <id>')
cmd.option('writer', '-W, --anonymous', { value: 0 })
expect(cmd.parse('foo -w 1 bar')).to.have.shape({ args: ['foo', 'bar'], options: { writer: 1 } })
expect(cmd.parse('foo -W bar')).to.have.shape({ args: ['foo', 'bar'], options: { writer: 0 } })
})

it('typed arguments', () => {
cmd = app.command('test3 [...args:number]')
expect(cmd.parse('1 2 3')).to.have.shape({ args: [1, 2, 3] })
})
})

describe('Advanced Features', () => {
Expand Down

0 comments on commit f793cdd

Please sign in to comment.