From 31eb2c2c3ca1663eff94f0398768a9b582332a93 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Mon, 24 Nov 2014 15:16:54 -0800 Subject: [PATCH] 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 --- lib/cli.js | 7 ++++++- test/unit/cli.spec.coffee | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 4f3982cd1..a28b55907 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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; } }); diff --git a/test/unit/cli.spec.coffee b/test/unit/cli.spec.coffee index c5f39dd40..e84952487 100644 --- a/test/unit/cli.spec.coffee +++ b/test/unit/cli.spec.coffee @@ -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']