Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cliparse

Package Overview
Dependencies
Maintainers
7
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cliparse - npm Package Compare versions

Comparing version 0.3.3 to 0.3.4

10

package.json
{
"name": "cliparse",
"version": "0.3.3",
"version": "0.3.4",
"description": "Declarative CLI parsing for node apps",

@@ -8,9 +8,9 @@ "main": "src/cliparse.js",

"bluebird": "^3.7.2",
"lodash": "^4.17.20",
"lodash": "^4.17.21",
"minimist": "^1.2.5"
},
"devDependencies": {
"tap": "^14.11.0",
"tap-spec": "^5.0.0",
"tape": "^5.1.1"
"tap": "^16.0.0",
"tap-spec": "^2.2.2",
"tape": "^5.5.2"
},

@@ -17,0 +17,0 @@ "scripts": {

@@ -21,6 +21,6 @@ var _ = require("lodash");

argument.parse = function(argument, value) {
var result;
let result;
if(typeof value !== 'undefined' && value !== null) {
result = argument.parser(value);
} else if(argument.default !== null) {
result = argument?.parser(value) ?? parsers.stringParser(value);
} else if((argument?.default ?? null) !== null) {
result = parsers.success(argument.default);

@@ -36,16 +36,21 @@ } else {

argument.parseList = function(args, providedArguments) {
if(providedArguments.length <= args.length) {
var combined = _.zip(args, _.take(providedArguments, args.length));
const combined = _.zip(args, providedArguments);
var results = _.map(combined, function(kv) {
return argument.parse(kv[0], kv[1]);
const results = _.map(combined, function(kv) {
return argument.parse(kv[0], kv[1]);
});
if(_.every(results, parsers.isSuccess)) {
const namedArgs = _(results)
.takeWhile(r => typeof r.argument?.name !== 'undefined')
.map(r => [r.argument.name, r.success])
.fromPairs()
.value();
return parsers.success({
args: _.map(results, "success"),
namedArgs,
unnamedArgs: _(results).drop(_.size(namedArgs)).map("success").value(),
});
if(_.every(results, parsers.isSuccess)) {
return parsers.success(_.map(results, "success"));
} else {
return parsers.error(results);
}
} else {
return parsers.error([parsers.error("Too many arguments: " + _.drop(providedArguments, args.length).join(", "))]);
return parsers.error(results);
}

@@ -52,0 +57,0 @@ };

@@ -17,4 +17,4 @@ var _ = require("lodash");

options.topLevel = true;
options.version = typeof options.version !== 'undefined' ? options.version : null;
options.helpCommand = typeof options.helpCommand !== 'undefined' ? options.helpCommand : true;
options.version = options.version ?? null;
options.helpCommand = options.helpCommand ?? true;
options.options = [ option.helpOption, option.versionOption ].concat(options.options || []);

@@ -133,3 +133,3 @@

cli.parse = function(cliApp, argv) {
argv = (typeof argv === "undefined") ? process.argv : argv;
argv = argv ?? process.argv;

@@ -136,0 +136,0 @@ var flagNames = command.getFlagNames(cliApp);

@@ -37,5 +37,5 @@ var _ = require("lodash");

availOptionNames = autocomplete.mconcat(_.map(allOptions, option.completeName));
} else if(typeof previousWord === 'string' && previousWord.slice(0, 1) === '-') {
} else if(typeof previousWord === 'string' && previousWord.slice(0, 1) === '-') {
// Complete option value
var name = previousWord.slice(0, 2) === '--' ? previousWord.slice(2) : previousWord.slice(1);
var name = previousWord.slice(0, 2) === '--' ? previousWord.slice(2) : previousWord.slice(1);
var previousOption = _.find(allOptions, function(opt) {

@@ -85,6 +85,5 @@ return _.includes(opt.names, name);

if(parsers.isSuccess(parsedArguments) && parsers.isSuccess(parsedOptions)) {
result = parsers.success({
args: parsedArguments.success,
result = parsers.success(_.assign(parsedArguments.success, {
options: parsedOptions.success
});
}));
} else {

@@ -91,0 +90,0 @@ result = parsers.error({

@@ -42,1 +42,9 @@ var test = require("tape");

});
test('undefined argument defaults to stringParser', function(t) {
var result = argument.parse(undefined, '12');
t.plan(2);
t.same(result.success, '12', 'undefined argument falls back to string');
t.same(result.argument, undefined, 'undefined argument falls back to string');
});

@@ -108,3 +108,3 @@ var minimist = require('minimist');

"metavar": "VALUE",
"complete": function() {return cliparse.autocomplete.words(["test"]);}
"complete": function() {return cliparse.autocomplete.words(["test"]);}
})

@@ -111,0 +111,0 @@ ]

@@ -15,3 +15,3 @@ var test = require("tape");

var result = command.parse(cmd, [], [], {});
t.same(result.success, { options: {}, args: [] });
t.same(result.success, { options: {}, args: [], namedArgs: {}, unnamedArgs: [] });
});

@@ -27,4 +27,4 @@

t.same(r1.success, { options: { test: true }, args: [] }, 'option present');
t.same(r2.success, { options: { test: false }, args: [] }, 'option not there');
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');
});

@@ -40,3 +40,3 @@

t.same(r1.success, { options: { test: true }, args: [] }, 'option present');
t.same(r1.success, { options: { test: true }, args: [], namedArgs: {}, unnamedArgs: [] }, 'option present');
t.same(r2.success, undefined, 'option not there');

@@ -54,7 +54,20 @@ });

t.same(r1.success, { options: {}, args: ['value'] }, 'argument present');
t.same(r1.success, { options: {}, args: ['value'], namedArgs: { test: 'value' }, unnamedArgs: [] }, 'argument present');
t.same(r2.success, undefined, 'must fail if argument not there');
t.same(r3.success, undefined, 'murt fail if unknown argument');
t.same(r3.success, { options: {}, args: ['value', 'extra value'], namedArgs: { test: 'value' }, unnamedArgs: ['extra value'] }, 'must recognize named argument and split unnamed argument');
});
test('command with no named arguments', function(t) {
t.plan(3);
var cmd = cliparse.command('name', {});
var r1 = command.parse(cmd, [], ['value'], {});
var r2 = command.parse(cmd, [], [], {});
var r3 = command.parse(cmd, [], ['value', 'extra value'], {});
t.same(r1.success, { options: {}, args: ['value'], namedArgs: {}, unnamedArgs: ['value'] }, 'argument present');
t.same(r2.success, { options: {}, args: [], namedArgs: {}, unnamedArgs: [] }, 'no arguments means no failure');
t.same(r3.success, { options: {}, args: ['value', 'extra value'], namedArgs: {}, unnamedArgs: ['value', 'extra value'] }, 'must put all arguments as unnamed');
});
test('retrieve flags', function(t) {

@@ -67,7 +80,7 @@ t.plan(3);

var option2 = cliparse.option('opt-two', { aliases: ["o"] });
var option3 = cliparse.option('opt-three', { aliases: ["t"]});
var option3 = cliparse.option('opt-three', { aliases: ["t"]});
var cmd1 = cliparse.command('name', { options: [flag1, option2]});
var cmd2 = cliparse.command('name', { options: [flag1, option1], commands: [ cliparse.command('inner', { options: [flag2, option2] })]});
var cmd3 = cliparse.command('name', { options: [flag1, option1], commands: [
cliparse.command('inner', { options: [flag2, option2], commands: [
cliparse.command('inner', { options: [flag2, option2], commands: [
cliparse.command('second-inner', { options: [flag3, option3] }),

@@ -93,7 +106,7 @@ ]})

var option2 = cliparse.option('opt-two', { aliases: ["o"] });
var option3 = cliparse.option('opt-three', { aliases: ["t"]});
var option3 = cliparse.option('opt-three', { aliases: ["t"]});
var cmd1 = cliparse.command('name', { options: [flag1, option2]});
var cmd2 = cliparse.command('name', { options: [flag1, option1], commands: [ cliparse.command('inner', { options: [flag2, option2] })]});
var cmd3 = cliparse.command('name', { options: [flag1, option1], commands: [
cliparse.command('inner', { options: [flag2, option2], commands: [
cliparse.command('inner', { options: [flag2, option2], commands: [
cliparse.command('second-inner', { options: [flag3, option3] }),

@@ -118,2 +131,2 @@ ]})

t.same(r.success, undefined, 'must fail on unknown options')
});
});

@@ -54,3 +54,3 @@ var test = require("tape");

t.equal(_.last(r.context).name, 'nested', 'correct subcommand parse');
t.same(r.success, { args: [], options: { test: true }}, 'correct subcommand parse');
t.same(r.success, { args: [], namedArgs: {}, unnamedArgs: [], options: { test: true }}, 'correct subcommand parse');
});

@@ -71,4 +71,4 @@

t.equal(_.last(r.context).name, 'nested', 'correct subcommand parse');
t.same(r.success, { args: [], options: { test: true }}, 'correct subcommand parse');
t.same(r.success, { args: [], namedArgs: {}, unnamedArgs: [], options: { test: true }}, 'correct subcommand parse');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc