Skip to content

Commit

Permalink
fix(koishi): fix edge case of greedy + strict options, fix koishijs#1473
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 7, 2024
1 parent d06e1e9 commit 1a5cf57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/command/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ export namespace Argv {
}
const name = content.slice(i, j)
if (this.config.strictOptions && !this._namedOptions[name]) {
if (this.ctx.$commander.resolveDomain(argDecl.type).greedy) {
argv.tokens.unshift(token)
args.push(this.ctx.$commander.parseValue(Argv.stringify(argv), 'argument', argv, argDecl))
break
}
args.push(this.ctx.$commander.parseValue(content, 'argument', argv, argDecl))
continue
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ describe('Parser API', () => {
expect(cmd.parse('-c')).to.have.shape({ options: { gamma: 1 } })
})

it('greedy + strict options', () => {
// https://github.com/koishijs/koishi/issues/1473
cmd = app.command('test-greedy-strict [foo:text]', { strictOptions: true })
expect(cmd.parse('-a -b -c')).to.have.shape({ args: ['-a -b -c'] })
})

it('valued options', () => {
cmd = app.command('cmd2 <foo> [bar:text]')
cmd.option('alpha', '-A, --no-alpha', { value: false })
Expand Down

0 comments on commit 1a5cf57

Please sign in to comment.