Skip to content

Commit

Permalink
fix(cli): override if an arg is defined multiple times
Browse files Browse the repository at this point in the history
```bash
karma start --log-level info --port 12 --log-level debug --port 34
```
Is now parsed as:
```js
{
  logLevel: 'DEBUG',
  port: 34
}
```

Closes #1192
  • Loading branch information
vojtajina committed Nov 24, 2014
1 parent 85328f3 commit 31eb2c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ var processArgs = function(argv, options, fs, path) {

// TODO(vojta): warn/throw when unknown argument (probably mispelled)
Object.getOwnPropertyNames(argv).forEach(function(name) {
var argumentValue = argv[name];
if (name !== '_' && name !== '$0') {
options[helper.dashToCamel(name)] = argv[name];
if (Array.isArray(argumentValue)) {
// If the same argument is defined multiple times, override.
argumentValue = argumentValue.pop();
}
options[helper.dashToCamel(name)] = argumentValue;
}
});

Expand Down
9 changes: 9 additions & 0 deletions test/unit/cli.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe 'cli', ->

describe 'processArgs', ->

it 'should override if multiple options given', ->
# optimist parses --port 123 --port 456 as port = [123, 456] which makes no sense
options = processArgs ['some.conf', '--port', '12', '--log-level', 'info',
'--port', '34', '--log-level', 'debug']

expect(options.port).to.equal 34
expect(options.logLevel).to.equal 'DEBUG'


it 'should return camelCased options', ->
options = processArgs ['some.conf', '--port', '12', '--single-run']

Expand Down

0 comments on commit 31eb2c2

Please sign in to comment.