Skip to content

Commit

Permalink
fix: the positional argument parse was clobbering global flag argumen…
Browse files Browse the repository at this point in the history
…ts (#984)
  • Loading branch information
bcoe committed Oct 16, 2017
1 parent a06b67d commit 7e58453
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/command.js
Expand Up @@ -293,13 +293,26 @@ module.exports = function command (yargs, usage, validation) {
return `--${key} ${value}`
}))
})

// short-circuit parse.
if (!unparsed.length) return

const parsed = Parser.detailed(unparsed.join(' '), options)

if (parsed.error) {
yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
} else {
// only copy over positional keys (don't overwrite
// flag arguments that were already parsed).
const positionalKeys = Object.keys(positionalMap)
Object.keys(positionalMap).forEach((key) => {
[].push.apply(positionalKeys, parsed.aliases[key])
})

Object.keys(parsed.argv).forEach((key) => {
if (key !== '_') argv[key] = parsed.argv[key]
if (positionalKeys.indexOf(key) !== -1) {
argv[key] = parsed.argv[key]
}
})
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/command.js
Expand Up @@ -102,6 +102,15 @@ describe('Command', () => {
expect(handlers.wat).to.not.exist
command.getCommands().should.deep.equal(['foo', 'wat'])
})

it('does not overwrite existing values in argv for keys that are not positional', () => {
const argv = yargs('foo foo.js --reporter=html')
.command('foo <file>')
.default('reporter', 'text')
.argv
argv.file.should.equal('foo.js')
argv.reporter.should.equal('html')
})
})

describe('variadic', () => {
Expand Down

0 comments on commit 7e58453

Please sign in to comment.