Skip to content

Commit

Permalink
Merge pull request #20 from hsablonniere/prevent-string-coercion-to-n…
Browse files Browse the repository at this point in the history
…umber

Prevent minimist from coercing strings to numbers
  • Loading branch information
divarvel authored Jan 5, 2018
2 parents 52f1e5d + 015dc1f commit 184a703
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ cli.cleanArgv = function(argv) {
cli.parse = function(cliApp, argv) {
argv = (typeof argv === "undefined") ? process.argv : argv;

var flags = command.getFlags(cliApp);
var flagNames = _.flatten(_.map(flags, "names"));
var flagNames = command.getFlagNames(cliApp);
var optionNames = command.getOptionNames(cliApp);

var opts = {
string: optionNames, // All other options should be treated as strings (and not coerced)
boolean: flagNames // Declare flags as not expecting values
};

Expand Down
25 changes: 18 additions & 7 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,23 @@ command.helpCommand = function() {
return c;
}();

command.getFlags = function(cmd) {
var flags = _.reject(cmd.options, function(opt) { return opt.expects_value; });
command.getFlagNames = function(cmd) {
return _([cmd])
.concat(cmd.commands)
.flatMap('options')
.reject(function(opt) { return opt.expects_value; })
.flatMap('names')
.uniq()
.value()
};

var commandsFlags = _.map(cmd.commands, function(scmd) {
return command.getFlags(scmd);
});

return flags.concat(_.flatten(commandsFlags));
};
command.getOptionNames = function (cmd) {
return _([cmd])
.concat(cmd.commands)
.flatMap('options')
.filter(function(opt) { return opt.expects_value; })
.flatMap('names')
.uniq()
.value()
}
38 changes: 28 additions & 10 deletions test/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,35 @@ test('command with arguments', function(t) {
});

test('retrieve flags', function(t) {
t.plan(2);
var flag1 = cliparse.flag('test');
var flag2 = cliparse.flag('other-test');
var cmd1 = cliparse.command('name', { options: [flag1]});
var cmd2 = cliparse.command('name', { options: [flag1], commands: [ cliparse.command('inner', { options: [flag2] })]});

var r1 = command.getFlags(cmd1);
var r2 = command.getFlags(cmd2);
t.plan(2);
var flag1 = cliparse.flag('flag-one');
var flag2 = cliparse.flag('flag-two', { aliases: ["f"] });
var option1 = cliparse.option('opt-one');
var option2 = cliparse.option('opt-two', { aliases: ["o"] });
var cmd1 = cliparse.command('name', { options: [flag1, option2]});
var cmd2 = cliparse.command('name', { options: [flag1, option1], commands: [ cliparse.command('inner', { options: [flag2, option2] })]});

var r1 = command.getFlagNames(cmd1);
var r2 = command.getFlagNames(cmd2);

t.same(r1, ['flag-one'], 'simple command');
t.same(r2, ['flag-one', 'flag-two', 'f'], 'nested commands');
});

t.same(r1, [flag1], 'simple command');
t.same(r2, [flag1, flag2], 'nested commands');
test('retrieve option names', function(t) {
t.plan(2);
var flag1 = cliparse.flag('flag-one');
var flag2 = cliparse.flag('flag-two', { aliases: ["f"] });
var option1 = cliparse.option('opt-one');
var option2 = cliparse.option('opt-two', { aliases: ["o"] });
var cmd1 = cliparse.command('name', { options: [flag1, option2]});
var cmd2 = cliparse.command('name', { options: [flag1, option1], commands: [ cliparse.command('inner', { options: [flag2, option2] })]});

var r1 = command.getOptionNames(cmd1);
var r2 = command.getOptionNames(cmd2);

t.same(r1, ['opt-two', 'o'], 'simple command');
t.same(r2, ['opt-one', 'opt-two', 'o'], 'nested commands');
});

test('command with unknown options', function(t) {
Expand Down

0 comments on commit 184a703

Please sign in to comment.