Comparing version 0.3.5 to 0.4.0
{ | ||
"name": "cliparse", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "Declarative CLI parsing for node apps", | ||
@@ -5,0 +5,0 @@ "main": "src/cliparse.js", |
@@ -301,2 +301,4 @@ # Declarative CLI parsing for node apps | ||
`[]` | ||
- `privateOptions`: array of options that will not be inherited in sub commands. Default value: | ||
`[]` | ||
- `commands`: array of subcommands (constructed with `command`). Default | ||
@@ -303,0 +305,0 @@ value: `[]` |
@@ -19,2 +19,3 @@ var _ = require("lodash"); | ||
options.options = options.options || []; | ||
options.privateOptions = options.privateOptions || []; | ||
options.commands = options.commands || []; | ||
@@ -33,3 +34,3 @@ options.action = typeof cb !== 'undefined' ? cb : null; | ||
var allOptions = parentOptions.concat(cmd.options); | ||
var allOptions = parentOptions.concat(cmd.options).concat(cmd.privateOptions); | ||
@@ -83,3 +84,3 @@ if(typeof currentWord === 'string' && currentWord.slice(0, 1) === '-') { | ||
var parsedArguments = argument.parseList(cmd.args, givenArgs); | ||
var parsedOptions = option.parseObject(cmd.options.concat(parentOptions), givenOpts); | ||
var parsedOptions = option.parseObject(cmd.options.concat(cmd.privateOptions).concat(parentOptions), givenOpts); | ||
@@ -122,18 +123,22 @@ if(parsers.isSuccess(parsedArguments) && parsers.isSuccess(parsedOptions)) { | ||
command.usage = function(context) { | ||
var cmd = _.last(context); | ||
var elems = _(context).map(function(cmd) { | ||
var required = _.filter(cmd.options, function(opt) { return opt.required; }); | ||
return [ cmd.name ].concat(_.map(required, option.usage)); | ||
}).flatten(); | ||
var args = _.map(cmd.args, argument.usage); | ||
const cmd = _.last(context); | ||
return elems.concat(args).join(' '); | ||
const requiredOptions = _(context).map(function(cmd) { return cmd.options }) | ||
.flatten() | ||
.concat(cmd.privateOptions) | ||
.filter((option) => option.required); | ||
return [ | ||
cmd.name, | ||
requiredOptions.map(option.usage), | ||
cmd.args.map(argument.usage), | ||
].join(' '); | ||
}; | ||
command.help = function(context) { | ||
var options = _.flatten(_.map(context, function(cmd) { return cmd.options; })); | ||
var cmd = _.last(context); | ||
var options = _.flatten(_.map(context, function(cmd) { return cmd.options; })).concat(cmd.privateOptions); | ||
var usage = "Usage: " + command.usage(context); | ||
var cmd = _.last(context); | ||
var argumentHelp = _.map(cmd.args, argument.help); | ||
@@ -198,3 +203,3 @@ var commandsList = _.map(cmd.commands, command.singleLineHelp); | ||
return _(getCommandsOfCommand(cmd)) | ||
.flatMap('options') | ||
.flatMap((cmd) => [...cmd.options, ...cmd.privateOptions]) | ||
.reject(function(opt) { return opt.expects_value; }) | ||
@@ -208,3 +213,3 @@ .flatMap('names') | ||
return _(getCommandsOfCommand(cmd)) | ||
.flatMap('options') | ||
.flatMap((cmd) => [...cmd.options, ...cmd.privateOptions]) | ||
.filter(function(opt) { return opt.expects_value; }) | ||
@@ -211,0 +216,0 @@ .flatMap('names') |
@@ -30,2 +30,14 @@ var test = require("tape"); | ||
test('command with one private flag', function(t) { | ||
t.plan(2); | ||
var option = cliparse.flag('test'); | ||
var cmd = cliparse.command( 'name', { privateOptions: [option] }); | ||
var r1 = command.parse(cmd, [], [], { test: true }); | ||
var r2 = command.parse(cmd, [], [], {}); | ||
t.same(r1.success, { options: { test: true }, args: [], namedArgs: {}, unnamedArgs: [] }, 'option present'); | ||
t.same(r2.success, { options: { test: false }, args: [], namedArgs: {}, unnamedArgs: [] }, 'option not there'); | ||
}); | ||
test('command with a required option without default', function(t) { | ||
@@ -43,2 +55,14 @@ t.plan(2); | ||
test('command with a required private option without default', function(t) { | ||
t.plan(2); | ||
var option = cliparse.flag('test', { required: true }); | ||
var cmd = cliparse.command( 'name', { privateOptions: [option] }); | ||
var r1 = command.parse(cmd, [], [], { test: true }); | ||
var r2 = command.parse(cmd, [], [], {}); | ||
t.same(r1.success, { options: { test: true }, args: [], namedArgs: {}, unnamedArgs: [] }, 'option present'); | ||
t.same(r2.success, undefined, 'option not there'); | ||
}); | ||
test('command with arguments', function(t) { | ||
@@ -45,0 +69,0 @@ t.plan(3); |
@@ -73,1 +73,19 @@ var test = require("tape"); | ||
}); | ||
test('do not carry top-level private options to subcommands', function(t) { | ||
t.plan(4); | ||
var topCommand = cliparse.command('top', { | ||
privateOptions: [ cliparse.flag("test") ], | ||
commands: [ | ||
cliparse.command('nested', {}) | ||
] | ||
}); | ||
var r = command.parse(topCommand, [], ['nested'], { test: true }); | ||
console.log(r.error); | ||
t.equal(r.success, undefined, 'parse must fail on unknown options'); | ||
t.equal(r.context.length, 2, 'subcommand parse'); | ||
t.equal(_.last(r.context).name, 'nested', 'correct subcommand parse'); | ||
t.same(r.error, { args: [], options: [ { error: 'Unknown option: test' } ] }, 'correct subcommand parse'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
64872
1355
382
0