Skip to content

Commit 31eb2c2

Browse files
committed
fix(cli): override if an arg is defined multiple times
```bash karma start --log-level info --port 12 --log-level debug --port 34 ``` Is now parsed as: ```js { logLevel: 'DEBUG', port: 34 } ``` Closes #1192
1 parent 85328f3 commit 31eb2c2

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ var processArgs = function(argv, options, fs, path) {
1818

1919
// TODO(vojta): warn/throw when unknown argument (probably mispelled)
2020
Object.getOwnPropertyNames(argv).forEach(function(name) {
21+
var argumentValue = argv[name];
2122
if (name !== '_' && name !== '$0') {
22-
options[helper.dashToCamel(name)] = argv[name];
23+
if (Array.isArray(argumentValue)) {
24+
// If the same argument is defined multiple times, override.
25+
argumentValue = argumentValue.pop();
26+
}
27+
options[helper.dashToCamel(name)] = argumentValue;
2328
}
2429
});
2530

test/unit/cli.spec.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ describe 'cli', ->
3232

3333
describe 'processArgs', ->
3434

35+
it 'should override if multiple options given', ->
36+
# optimist parses --port 123 --port 456 as port = [123, 456] which makes no sense
37+
options = processArgs ['some.conf', '--port', '12', '--log-level', 'info',
38+
'--port', '34', '--log-level', 'debug']
39+
40+
expect(options.port).to.equal 34
41+
expect(options.logLevel).to.equal 'DEBUG'
42+
43+
3544
it 'should return camelCased options', ->
3645
options = processArgs ['some.conf', '--port', '12', '--single-run']
3746

0 commit comments

Comments
 (0)