diff --git a/packages/core/src/command/parser.ts b/packages/core/src/command/parser.ts index 965907aa21..e1d900bc0b 100644 --- a/packages/core/src/command/parser.ts +++ b/packages/core/src/command/parser.ts @@ -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 diff --git a/packages/core/tests/parser.spec.ts b/packages/core/tests/parser.spec.ts index 2cb391783a..4b50ca4607 100644 --- a/packages/core/tests/parser.spec.ts +++ b/packages/core/tests/parser.spec.ts @@ -99,12 +99,17 @@ describe('Parser API', () => { }) it('valued override', () => { - cmd = app.command('test ') + cmd = app.command('test2 ') cmd.option('writer', '-w ') 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', () => {