Skip to content

Commit

Permalink
fix(core): fix param missing with valued options, fix #1109
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 9, 2023
1 parent 7fa8e76 commit c0593b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/command/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,15 +556,18 @@ export namespace Argv {
// get parameter from next token
quoted = false
if (!param) {
const { type } = option || {}
const { type, values } = option || {}
if (resolveConfig(type).greedy) {
param = Argv.stringify(argv)
quoted = true
argv.tokens = []
} else if (type !== 'boolean' && argv.tokens.length && (type || argv.tokens[0]?.content !== '-')) {
const token = argv.tokens.shift()
param = token.content
quoted = token.quoted
} else {
const isValued = names[names.length - 1] in (values || {})
if (!isValued && type !== 'boolean' && argv.tokens.length && (type || argv.tokens[0]?.content !== '-')) {
const token = argv.tokens.shift()
param = token.content
quoted = token.quoted
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ describe('Parser API', () => {
expect(cmd.parse('-C')).to.have.shape({ options: { gamma: 1 } })
expect(cmd.parse('')).to.have.shape({ options: { gamma: 0 }, args: [] })
})

it('valued override', () => {
cmd = app.command('test <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 } })
})
})

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

0 comments on commit c0593b4

Please sign in to comment.