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

command-line-args

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

command-line-args - npm Package Compare versions

Comparing version 2.1.6 to 3.0.0

11

bin/cli.js

@@ -18,13 +18,8 @@ #!/usr/bin/env node

var cli = commandLineArgs(cliOptions)
try {
console.log(cli.parse())
console.log(commandLineArgs(cliOptions))
} catch (err) {
halt(err.message)
console.error(err.message)
process.exitCode = 1
}
}
function halt (msg) {
console.error(msg)
process.exit(1)
}

@@ -69,3 +69,3 @@ 'use strict';

value: function validate(definitions) {
var invalidOption = undefined;
var invalidOption = void 0;

@@ -72,0 +72,0 @@ var optionWithoutDefinition = this.list.filter(function (arg) {

'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var arrayify = require('array-back');

@@ -17,92 +11,47 @@ var Definitions = require('./definitions');

var CommandLineArgs = function () {
function CommandLineArgs(definitions) {
_classCallCheck(this, CommandLineArgs);
function commandLineArgs(definitions, argv) {
definitions = new Definitions(definitions);
argv = new Argv(argv);
argv.expandOptionEqualsNotation();
argv.expandGetoptNotation();
argv.validate(definitions);
this.definitions = new Definitions(definitions);
}
var output = definitions.createOutput();
var def = void 0;
_createClass(CommandLineArgs, [{
key: 'parse',
value: function parse(argv) {
var _this = this;
argv = new Argv(argv);
argv.expandOptionEqualsNotation();
argv.expandGetoptNotation();
argv.validate(this.definitions);
var output = this.definitions.createOutput();
var def = undefined;
argv.list.forEach(function (item) {
if (option.isOption(item)) {
def = _this.definitions.get(item);
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue());
if (def.isBoolean()) {
outputSet(output, def.name, true);
def = null;
}
} else {
var value = item;
if (!def) {
def = _this.definitions.getDefault();
if (!def) return;
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue());
}
var outputValue = def.type ? def.type(value) : value;
outputSet(output, def.name, outputValue);
if (!def.multiple) def = null;
}
});
for (var key in output) {
var value = output[key];
if (Array.isArray(value) && value._initial) delete value._initial;
argv.list.forEach(function (item) {
if (option.isOption(item)) {
def = definitions.get(item);
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue());
if (def.isBoolean()) {
outputSet(output, def.name, true);
def = null;
}
} else {
var value = item;
if (!def) {
def = definitions.getDefault();
if (!def) return;
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue());
}
if (this.definitions.isGrouped()) {
var _ret = function () {
var grouped = {
_all: output
};
var outputValue = def.type ? def.type(value) : value;
outputSet(output, def.name, outputValue);
_this.definitions.whereGrouped().forEach(function (def) {
arrayify(def.group).forEach(function (groupName) {
grouped[groupName] = grouped[groupName] || {};
if (t.isDefined(output[def.name])) {
grouped[groupName][def.name] = output[def.name];
}
});
});
if (!def.multiple) def = null;
}
});
_this.definitions.whereNotGrouped().forEach(function (def) {
if (t.isDefined(output[def.name])) {
if (!grouped._none) grouped._none = {};
grouped._none[def.name] = output[def.name];
}
});
return {
v: grouped
};
}();
for (var key in output) {
var value = output[key];
if (Array.isArray(value) && value._initial) delete value._initial;
}
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} else {
return output;
}
}
}, {
key: 'getUsage',
value: function getUsage(options) {
var getUsage = require('command-line-usage');
return getUsage(this.definitions.list, options);
}
}]);
if (definitions.isGrouped()) {
return groupOutput(definitions, output);
} else {
return output;
}
}
return CommandLineArgs;
}();
function outputSet(output, property, value) {

@@ -120,4 +69,23 @@ if (output[property] && output[property]._initial) {

function commandLineArgs(definitions) {
return new CommandLineArgs(definitions);
function groupOutput(definitions, output) {
var grouped = {
_all: output
};
definitions.whereGrouped().forEach(function (def) {
arrayify(def.group).forEach(function (groupName) {
grouped[groupName] = grouped[groupName] || {};
if (t.isDefined(output[def.name])) {
grouped[groupName][def.name] = output[def.name];
}
});
});
definitions.whereNotGrouped().forEach(function (def) {
if (t.isDefined(output[def.name])) {
if (!grouped._none) grouped._none = {};
grouped._none[def.name] = output[def.name];
}
});
return grouped;
}

@@ -42,3 +42,3 @@ 'use strict';

var invalidOption = undefined;
var invalidOption = void 0;

@@ -45,0 +45,0 @@ var numericAlias = this.list.some(function (def) {

# Examples
Most of these example files are modules exporting an array of [Option Definitions](https://github.com/75lb/command-line-args#optiondefinition-). They are consumed using the command-line-args test harness.
Most of these example files are modules exporting an array of [Option Definitions](https://github.com/75lb/command-line-args/#optiondefinition-). They are consumed using the command-line-args test harness.

@@ -4,0 +4,0 @@ ## Install

@@ -11,9 +11,9 @@ /*

var cli = commandLineArgs([
var optionDefinitions = [
{ name: 'help', type: Boolean },
{ name: 'files', type: String, multiple: true, defaultOption: true },
{ name: 'log-level', type: String }
])
]
var options = cli.parse()
var options = commandLineArgs(optionDefinitions)

@@ -20,0 +20,0 @@ /* all supplied files should exist and --log-level should be one from the list */

@@ -30,3 +30,5 @@ 'use strict'

/* expand --option=name style args */
/**
* expand --option=name style args
*/
expandOptionEqualsNotation () {

@@ -49,3 +51,5 @@ const optEquals = option.optEquals

/* expand getopt-style combined options */
/**
* expand getopt-style combined options
*/
expandGetoptNotation () {

@@ -62,2 +66,6 @@ const combinedArg = option.combined

/**
* Inspect the user-supplied options for validation issues.
* @throws `UNKNOWN_OPTION`
*/
validate (definitions) {

@@ -64,0 +72,0 @@ let invalidOption

@@ -9,4 +9,2 @@ 'use strict'

/**
* A library to collect command-line args and generate a usage guide.
*
* @module command-line-args

@@ -17,115 +15,71 @@ */

/**
* A class encapsulating operations you can perform using an [OptionDefinition](#exp_module_definition--OptionDefinition) array as input.
* Returns an object containing all options set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
*
* @typicalname cli
* @param {module:definition[]} - An array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects
* @param [argv] {string[]} - An array of strings, which if passed will be parsed instead of `process.argv`.
* @returns {object}
* @throws `UNKNOWN_OPTION` if the user sets an option without a definition
* @throws `NAME_MISSING` if an option definition is missing the required `name` property
* @throws `INVALID_TYPE` if an option definition has a `type` value that's not a function
* @throws `INVALID_ALIAS` if an alias is numeric, a hyphen or a length other than 1
* @throws `DUPLICATE_NAME` if an option definition name was used more than once
* @throws `DUPLICATE_ALIAS` if an option definition alias was used more than once
* @throws `DUPLICATE_DEFAULT_OPTION` if more than one option definition has `defaultOption: true`
* @alias module:command-line-args
* @example
* ```js
* const commandLineArgs = require('command-line-args')
* const options = commandLineArgs([
* { name: 'file' },
* { name: 'verbose' },
* { name: 'depth'}
* ])
* ```
*/
class CommandLineArgs {
/**
* The constructor will throw if you pass invalid option definitions. You should fix these issues before proceeding.
*
* @param {module:definition[]} - An optional array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects
* @throws `NAME_MISSING` if an option definition is missing the required `name` property
* @throws `INVALID_TYPE` if an option definition has a `type` value that's not a function
* @throws `INVALID_ALIAS` if an alias is numeric, a hyphen or a length other than 1
* @throws `DUPLICATE_NAME` if an option definition name was used more than once
* @throws `DUPLICATE_ALIAS` if an option definition alias was used more than once
* @throws `DUPLICATE_DEFAULT_OPTION` if more than one option definition has `defaultOption: true`
* @example
* ```js
* const commandLineArgs = require('command-line-args')
* const cli = commandLineArgs([
* { name: 'file' },
* { name: 'verbose' },
* { name: 'depth'}
* ])
* ```
*/
constructor (definitions) {
this.definitions = new Definitions(definitions)
}
function commandLineArgs (definitions, argv) {
definitions = new Definitions(definitions)
argv = new Argv(argv)
argv.expandOptionEqualsNotation()
argv.expandGetoptNotation()
argv.validate(definitions)
/**
* Returns an object containing all the values and flags set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
*
* @param [argv] {string[]} - An array of strings, which if passed will be parsed instead of `process.argv`.
* @returns {object}
* @throws `UNKNOWN_OPTION` if the user sets an option without a definition
*/
parse (argv) {
argv = new Argv(argv)
argv.expandOptionEqualsNotation()
argv.expandGetoptNotation()
argv.validate(this.definitions)
/* create output initialised with default values */
const output = definitions.createOutput()
let def
/* create output initialised with default values */
const output = this.definitions.createOutput()
let def
/* walk argv building the output */
argv.list.forEach(item => {
if (option.isOption(item)) {
def = this.definitions.get(item)
/* walk argv building the output */
argv.list.forEach(item => {
if (option.isOption(item)) {
def = definitions.get(item)
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue())
if (def.isBoolean()) {
outputSet(output, def.name, true)
def = null
}
} else {
const value = item
if (!def) {
def = definitions.getDefault()
if (!def) return
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue())
if (def.isBoolean()) {
outputSet(output, def.name, true)
def = null
}
} else {
const value = item
if (!def) {
def = this.definitions.getDefault()
if (!def) return
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue())
}
}
const outputValue = def.type ? def.type(value) : value
outputSet(output, def.name, outputValue)
const outputValue = def.type ? def.type(value) : value
outputSet(output, def.name, outputValue)
if (!def.multiple) def = null
}
})
/* clear _initial flags */
for (let key in output) {
const value = output[key]
if (Array.isArray(value) && value._initial) delete value._initial
if (!def.multiple) def = null
}
})
/* group the output values */
if (this.definitions.isGrouped()) {
const grouped = {
_all: output
}
this.definitions.whereGrouped().forEach(def => {
arrayify(def.group).forEach(groupName => {
grouped[groupName] = grouped[groupName] || {}
if (t.isDefined(output[def.name])) {
grouped[groupName][def.name] = output[def.name]
}
})
})
this.definitions.whereNotGrouped().forEach(def => {
if (t.isDefined(output[def.name])) {
if (!grouped._none) grouped._none = {}
grouped._none[def.name] = output[def.name]
}
})
return grouped
} else {
return output
}
/* clear _initial flags */
for (let key in output) {
const value = output[key]
if (Array.isArray(value) && value._initial) delete value._initial
}
/**
* Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use.
*
* @param [options] {object} - the options to pass to [command-line-usage](https://github.com/75lb/command-line-usage)
* @returns {string}
*/
getUsage (options) {
const getUsage = require('command-line-usage')
return getUsage(this.definitions.list, options)
/* group the output values */
if (definitions.isGrouped()) {
return groupOutput(definitions, output)
} else {
return output
}

@@ -146,5 +100,23 @@ }

/* Factory method: initialises a new CommandLineArgs instance. */
function commandLineArgs (definitions) {
return new CommandLineArgs(definitions)
function groupOutput (definitions, output) {
const grouped = {
_all: output
}
definitions.whereGrouped().forEach(def => {
arrayify(def.group).forEach(groupName => {
grouped[groupName] = grouped[groupName] || {}
if (t.isDefined(output[def.name])) {
grouped[groupName][def.name] = output[def.name]
}
})
})
definitions.whereNotGrouped().forEach(def => {
if (t.isDefined(output[def.name])) {
if (!grouped._none) grouped._none = {}
grouped._none[def.name] = output[def.name]
}
})
return grouped
}

@@ -7,3 +7,3 @@ 'use strict'

/**
* Describes a command-line option. The additional properties `description` and `typeLabel` used by {@link module:command-line-args#getUsage .getUsage()} are described [here](https://github.com/75lb/command-line-usage#getusagedefinitions-options--string-).
* Describes a command-line option. Additionally, you can add `description` and `typeLabel` propeties and make use of [command-line-usage](https://github.com/75lb/command-line-usage).
* @alias module:definition

@@ -10,0 +10,0 @@ * @typicalname option

{
"name": "command-line-args",
"version": "2.1.6",
"description": "A library to collect command-line args and generate a usage guide.",
"version": "3.0.0",
"description": "A library to parse command-line options.",
"repository": "https://github.com/75lb/command-line-args.git",

@@ -30,15 +30,14 @@ "main": "index",

"devDependencies": {
"babel-preset-es2015": "^6.5.0",
"coveralls": "^2.11.4",
"jsdoc-to-markdown": "^1.1.1",
"tape": "^4",
"test-value": "^1.0.0"
"babel-preset-es2015": "^6.9.0",
"coveralls": "^2.11.9",
"jsdoc-to-markdown": "^1.3.6",
"tape": "^4.5.1",
"test-value": "^2.0.0"
},
"dependencies": {
"array-back": "^1.0.2",
"command-line-usage": "^2",
"core-js": "^2.0.1",
"feature-detect-es6": "^1.2.0",
"find-replace": "^1",
"typical": "^2.3.0"
"array-back": "^1.0.3",
"core-js": "^2.4.0",
"feature-detect-es6": "^1.3.0",
"find-replace": "^1.0.2",
"typical": "^2.4.2"
},

@@ -45,0 +44,0 @@ "standard": {

@@ -10,3 +10,3 @@ [![view on npm](http://img.shields.io/npm/v/command-line-args.svg)](https://www.npmjs.org/package/command-line-args)

# command-line-args
A library to collect command-line args and generate a usage guide.
A library to parse command-line options.

@@ -24,17 +24,17 @@ *If your app requires a git-like command interface, consider using [command-line-commands](https://github.com/75lb/command-line-commands).*

To access the values, first describe the options your app accepts (see [option definitions](#option-definitions)).
To access the values, first describe the options your app accepts (see [option definitions](#optiondefinition-)).
```js
var commandLineArgs = require('command-line-args');
const commandLineArgs = require('command-line-args')
var cli = commandLineArgs([
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'timeout', alias: 't', type: Number }
])
]
```
The [`type`](#module_definition--OptionDefinition+type) property is a setter function (the value you receive is the output of this), giving you full control over the value received.
The [`type`](#optiontype--function) property is a setter function (the value you receive is the output of this), giving you full control over the value received.
Next, collect the command line args using [.parse()](#module_command-line-args--CommandLineArgs+parse):
Next, parse the options using [commandLineArgs()](#commandlineargsdefinitions-argv--object-):
```js
var options = cli.parse()
const options = commandLineArgs(optionDefinitions)
```

@@ -54,5 +54,5 @@

When dealing with large amounts of options it often makes sense to [group](#module_definition--Definition+group) them.
When dealing with large amounts of options it often makes sense to [group](#optiongroup--string--arraystring) them.
The [.getUsage()](#module_command-line-args--CommandLineArgs+getUsage) method generates a usage guide. For example:
A usage guide can be generated using [command-line-usage](https://github.com/75lb/command-line-usage), for example:

@@ -66,3 +66,3 @@ ![usage](https://raw.githubusercontent.com/75lb/command-line-usage/master/example/screens/footer.png)

* Argument order is insignificant. Whether you set `--example` at the beginning or end of the arg list makes no difference.
* Options with a [type](#module_definition--OptionDefinition+type) of `Boolean` do not need to supply a value. Setting `--flag` or `-f` will set that option's value to `true`. This is the only [type](#module_definition--OptionDefinition+type) with special behaviour.
* Options with a [type](#optiontype--function) of `Boolean` do not need to supply a value. Setting `--flag` or `-f` will set that option's value to `true`. This is the only [type](#optiontype--function) with special behaviour.
* Three ways to set an option value

@@ -72,3 +72,3 @@ * `--option value`

* `-o value`
* Two ways to a set list of values (on options with [multiple](#module_definition--OptionDefinition+multiple) set)
* Two ways to a set list of values (on options with [multiple](#optionmultiple--boolean) set)
* `--list one two three`

@@ -92,3 +92,3 @@ * `--list one --list two --list three`

If you install globally you get the `command-line-args` test-harness. You test by piping in a module which exports an option definitions array. You can then view the `.parse()` output for the args you pass.
If you install globally you get the `command-line-args` test-harness. You test by piping in a module which exports an option definitions array. You can then view the output for the args you pass.

@@ -108,24 +108,11 @@ For example:

# API Reference
<a name="module_command-line-args"></a>
## command-line-args
A library to collect command-line args and generate a usage guide.
<a name="exp_module_command-line-args--commandLineArgs"></a>
### commandLineArgs(definitions, [argv]) ⇒ <code>object</code> ⏏
Returns an object containing all options set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
* [command-line-args](#module_command-line-args)
* [CommandLineArgs](#exp_module_command-line-args--CommandLineArgs) ⏏
* [new CommandLineArgs(definitions)](#new_module_command-line-args--CommandLineArgs_new)
* [.parse([argv])](#module_command-line-args--CommandLineArgs+parse) ⇒ <code>object</code>
* [.getUsage([options])](#module_command-line-args--CommandLineArgs+getUsage) ⇒ <code>string</code>
<a name="exp_module_command-line-args--CommandLineArgs"></a>
### CommandLineArgs ⏏
A class encapsulating operations you can perform using an [OptionDefinition](#exp_module_definition--OptionDefinition) array as input.
**Kind**: Exported class
<a name="new_module_command-line-args--CommandLineArgs_new"></a>
#### new CommandLineArgs(definitions)
The constructor will throw if you pass invalid option definitions. You should fix these issues before proceeding.
**Kind**: Exported function
**Throws**:
- `UNKNOWN_OPTION` if the user sets an option without a definition
- `NAME_MISSING` if an option definition is missing the required `name` property

@@ -141,3 +128,4 @@ - `INVALID_TYPE` if an option definition has a `type` value that's not a function

| --- | --- | --- |
| definitions | <code>[Array.&lt;definition&gt;](#module_definition)</code> | An optional array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects |
| definitions | <code>[Array.&lt;definition&gt;](#module_definition)</code> | An array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects |
| [argv] | <code>Array.&lt;string&gt;</code> | An array of strings, which if passed will be parsed instead of `process.argv`. |

@@ -147,3 +135,3 @@ **Example**

const commandLineArgs = require('command-line-args')
const cli = commandLineArgs([
const options = commandLineArgs([
{ name: 'file' },

@@ -154,30 +142,6 @@ { name: 'verbose' },

```
<a name="module_command-line-args--CommandLineArgs+parse"></a>
#### cli.parse([argv]) ⇒ <code>object</code>
Returns an object containing all the values and flags set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
<a name="exp_module_definition--OptionDefinition"></a>
**Kind**: instance method of <code>[CommandLineArgs](#exp_module_command-line-args--CommandLineArgs)</code>
**Throws**:
- `UNKNOWN_OPTION` if the user sets an option without a definition
| Param | Type | Description |
| --- | --- | --- |
| [argv] | <code>Array.&lt;string&gt;</code> | An array of strings, which if passed will be parsed instead of `process.argv`. |
<a name="module_command-line-args--CommandLineArgs+getUsage"></a>
#### cli.getUsage([options]) ⇒ <code>string</code>
Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use.
**Kind**: instance method of <code>[CommandLineArgs](#exp_module_command-line-args--CommandLineArgs)</code>
| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>object</code> | the options to pass to [command-line-usage](https://github.com/75lb/command-line-usage) |
<a name="exp_module_definition--OptionDefinition"></a>
## OptionDefinition ⏏
Describes a command-line option. The additional properties `description` and `typeLabel` used by [.getUsage()](#module_command-line-args--CommandLineArgs+getUsage) are described [here](https://github.com/75lb/command-line-usage#getusagedefinitions-options--string-).
Describes a command-line option. Additionally, you can add `description` and `typeLabel` propeties and make use of [command-line-usage](https://github.com/75lb/command-line-usage).

@@ -195,2 +159,3 @@ **Kind**: Exported class

<a name="module_definition--OptionDefinition.OptionDefinition+name"></a>
### option.name : <code>string</code>

@@ -226,2 +191,3 @@ The only required definition property is `name`, so the simplest working example is

<a name="module_definition--OptionDefinition.OptionDefinition+type"></a>
### option.type : <code>function</code>

@@ -260,2 +226,3 @@ The `type` value is a setter function (you receive the output from this), enabling you to be specific about the type and value received.

<a name="module_definition--OptionDefinition.OptionDefinition+alias"></a>
### option.alias : <code>string</code>

@@ -279,2 +246,3 @@ getopt-style short option names. Can be any single character (unicode included) except a digit or hypen.

<a name="module_definition--OptionDefinition.OptionDefinition+multiple"></a>
### option.multiple : <code>boolean</code>

@@ -297,2 +265,3 @@ Set this flag if the option takes a list of values. You will receive an array of values, each passed through the `type` function (if specified).

<a name="module_definition--OptionDefinition.OptionDefinition+defaultOption"></a>
### option.defaultOption : <code>boolean</code>

@@ -315,2 +284,3 @@ Any unclaimed command-line args will be set on this option. This flag is typically set on the most commonly-used option to make for more concise usage (i.e. `$ myapp *.js` instead of `$ myapp --files *.js`).

<a name="module_definition--OptionDefinition.OptionDefinition+defaultValue"></a>
### option.defaultValue : <code>\*</code>

@@ -334,2 +304,3 @@ An initial value for the option.

<a name="module_definition--OptionDefinition.OptionDefinition+group"></a>
### option.group : <code>string</code> &#124; <code>Array.&lt;string&gt;</code>

@@ -336,0 +307,0 @@ When your app has a large amount of options it makes sense to organise them in groups.

@@ -13,3 +13,3 @@ var test = require('tape')

var argv = [ '-v' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
verbose: true

@@ -22,3 +22,3 @@ })

var argv = [ '-d' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
'dry-run': true

@@ -31,3 +31,3 @@ })

var argv = [ '-v', '-c' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
verbose: true,

@@ -34,0 +34,0 @@ colour: true

@@ -9,6 +9,6 @@ var test = require('tape')

]
t.deepEqual(cliArgs(optionDefinitions).parse([ '--colour' ]), {
t.deepEqual(cliArgs(optionDefinitions, [ '--colour' ]), {
colour: null
})
t.deepEqual(cliArgs(optionDefinitions).parse([ '--colour', '--files', 'yeah' ]), {
t.deepEqual(cliArgs(optionDefinitions, [ '--colour', '--files', 'yeah' ]), {
colour: null,

@@ -25,3 +25,3 @@ files: 'yeah'

var argv = [ '--colours', '../what', '../ever' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
colours: [ '../what', '../ever' ]

@@ -28,0 +28,0 @@ })

var test = require('tape')
var cliArgs = require('../')
var detect = require('feature-detect-es6')

@@ -4,0 +3,0 @@ var Argv

var test = require('tape')
var cliArgs = require('../')
var detect = require('feature-detect-es6')

@@ -14,3 +13,3 @@ var Definitions

test('.createOutput()', function (t) {
var definitions = new Definitions([ { name: 'one', defaultValue: 'eins' }])
var definitions = new Definitions([ { name: 'one', defaultValue: 'eins' } ])
t.deepEqual(definitions.createOutput(), { one: 'eins' })

@@ -17,0 +16,0 @@ t.end()

@@ -9,3 +9,3 @@ var test = require('tape')

var argv = [ 'file1', 'file2' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
files: 'file2'

@@ -21,3 +21,3 @@ })

var argv = [ 'file1', 'file2' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
files: [ 'file1', 'file2' ]

@@ -29,8 +29,8 @@ })

test('defaultOption: after a boolean', function (t) {
var cli = cliArgs([
var definitions = [
{ name: 'one', type: Boolean },
{ name: 'two', defaultOption: true }
])
]
t.deepEqual(
cli.parse([ '--one', 'sfsgf' ]),
cliArgs(definitions, [ '--one', 'sfsgf' ]),
{ one: true, two: 'sfsgf' }

@@ -49,3 +49,3 @@ )

var argv = [ '--one', '1', 'file1', 'file2', '--two', '2' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
one: '1',

@@ -65,3 +65,3 @@ two: '2',

var argv = [ 'file0', '--one', 'file1', '--files', 'file2', '--two', '2', 'file3' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
one: true,

@@ -75,7 +75,7 @@ two: '2',

test('defaultOption: floating args present but no defaultOption', function (t) {
var cli = cliArgs([
var definitions = [
{ name: 'one', type: Boolean }
])
]
t.deepEqual(
cli.parse([ 'aaa', '--one', 'aaa', 'aaa' ]),
cliArgs(definitions, [ 'aaa', '--one', 'aaa', 'aaa' ]),
{ one: true }

@@ -82,0 +82,0 @@ )

@@ -5,14 +5,14 @@ var test = require('tape')

test('default value', function (t) {
t.deepEqual(cliArgs([ { name: 'one' }, { name: 'two', defaultValue: 'two' } ]).parse([ '--one', '1' ]), {
t.deepEqual(cliArgs([ { name: 'one' }, { name: 'two', defaultValue: 'two' } ], [ '--one', '1' ]), {
one: '1',
two: 'two'
})
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }]).parse([]), {
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }], []), {
two: 'two'
})
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }]).parse([ '--two', 'zwei' ]), {
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }], [ '--two', 'zwei' ]), {
two: 'zwei'
})
t.deepEqual(
cliArgs([{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }]).parse([ '--two', 'duo' ]),
cliArgs([{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }], [ '--two', 'duo' ]),
{ two: [ 'duo' ] }

@@ -26,3 +26,3 @@ )

var defs = [{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }]
var result = cliArgs(defs).parse([])
var result = cliArgs(defs, [])
t.deepEqual(result, { two: [ 'two', 'zwei' ] })

@@ -37,3 +37,3 @@ t.end()

var argv = [ 'duo' ]
t.deepEqual(cliArgs(defs).parse(argv), { two: [ 'duo' ] })
t.deepEqual(cliArgs(defs, argv), { two: [ 'duo' ] })
t.end()

@@ -49,3 +49,3 @@ })

var argv = []
t.deepEqual(cliArgs(defs).parse(argv), {
t.deepEqual(cliArgs(defs, argv), {
one: 0,

@@ -52,0 +52,0 @@ two: false

@@ -6,3 +6,3 @@ var test = require('tape')

process.argv = [ 'node', 'filename', '--one', 'eins' ]
t.deepEqual(cliArgs({ name: 'one' }).parse(process.argv), {
t.deepEqual(cliArgs({ name: 'one' }, process.argv), {
one: 'eins'

@@ -15,3 +15,3 @@ })

process.argv = [ 'node', 'filename', '--one', 'eins' ]
t.deepEqual(cliArgs({ name: 'one' }).parse(), {
t.deepEqual(cliArgs({ name: 'one' }), {
one: 'eins'

@@ -18,0 +18,0 @@ })

@@ -11,3 +11,3 @@ var test = require('tape')

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -27,3 +27,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -44,3 +44,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -61,3 +61,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -74,3 +74,3 @@ } catch (err) {

try {
cliArgs([ { name: 'one', type: 'string' } ]).parse(argv)
cliArgs([ { name: 'one', type: 'string' } ], argv)
t.fail()

@@ -82,3 +82,3 @@ } catch (err) {

try {
cliArgs([ { name: 'one', type: 234 } ]).parse(argv)
cliArgs([ { name: 'one', type: 234 } ], argv)
t.fail()

@@ -90,3 +90,3 @@ } catch (err) {

try {
cliArgs([ { name: 'one', type: {} } ]).parse(argv)
cliArgs([ { name: 'one', type: {} } ], argv)
t.fail()

@@ -98,3 +98,3 @@ } catch (err) {

t.doesNotThrow(function () {
cliArgs([ { name: 'one', type: function () {} } ]).parse(argv)
cliArgs([ { name: 'one', type: function () {} } ], argv)
}, /invalid/i)

@@ -111,3 +111,3 @@

t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one', '1' ]),
cliArgs(optionDefinitions, [ '--one', '1' ]),
{ one: 1 }

@@ -117,3 +117,3 @@ )

try {
cliArgs(optionDefinitions).parse([ '--one', '--two' ])
cliArgs(optionDefinitions, [ '--one', '--two' ])
t.fail()

@@ -125,3 +125,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse([ '--one', '2', '--two', 'two' ])
cliArgs(optionDefinitions, [ '--one', '2', '--two', 'two' ])
t.fail()

@@ -133,3 +133,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse([ '-a', '2' ])
cliArgs(optionDefinitions, [ '-a', '2' ])
t.fail()

@@ -141,3 +141,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse([ '-sdf' ])
cliArgs(optionDefinitions, [ '-sdf' ])
t.fail()

@@ -159,3 +159,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -177,3 +177,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -195,3 +195,3 @@ } catch (err) {

try {
cliArgs(optionDefinitions).parse(argv)
cliArgs(optionDefinitions, argv)
t.fail()

@@ -198,0 +198,0 @@ } catch (err) {

@@ -11,4 +11,3 @@ var test = require('tape')

test('groups', function (t) {
var cli = cliArgs(optionDefinitions)
t.deepEqual(cli.parse([ '--one', '1', '--two', '2', '--three', '3' ]), {
t.deepEqual(cliArgs(optionDefinitions, [ '--one', '1', '--two', '2', '--three', '3' ]), {
a: {

@@ -38,4 +37,3 @@ one: '1',

var cli = cliArgs(optionDefinitions)
t.deepEqual(cli.parse([ '--one', '1', '--two', '2', '--three', '3' ]), {
t.deepEqual(cliArgs(optionDefinitions, [ '--one', '1', '--two', '2', '--three', '3' ]), {
a: {

@@ -42,0 +40,0 @@ one: '1',

@@ -13,4 +13,3 @@ var test = require('tape')

var argv = [ '--one', '-t', '--three' ]
var cli = cliArgs(optionDefinitions)
var result = cli.parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.strictEqual(result.one, true)

@@ -17,0 +16,0 @@ t.strictEqual(result.two, true)

@@ -12,4 +12,3 @@ var test = require('tape')

var argv = [ '--один', '1', '--两', '2', '-т', '3' ]
var cli = cliArgs(optionDefinitions)
var result = cli.parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.strictEqual(result.один, '1')

@@ -16,0 +15,0 @@ t.strictEqual(result.两, '2')

@@ -12,3 +12,3 @@ var test = require('tape')

var argv = [ '-abc', 'yeah' ]
t.deepEqual(cliArgs(optionDefinitions).parse(argv), {
t.deepEqual(cliArgs(optionDefinitions, argv), {
flagA: true,

@@ -29,4 +29,3 @@ flagB: true,

var argv = [ '--one=1', '--two', '2', '--three=3' ]
var cli = cliArgs(optionDefinitions)
var result = cli.parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.strictEqual(result.one, '1')

@@ -33,0 +32,0 @@ t.strictEqual(result.two, '2')

@@ -10,3 +10,3 @@ var test = require('tape')

var argv = [ '--array', '--array', '--array' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -13,0 +13,0 @@ array: [ true, true, true ]

@@ -9,17 +9,16 @@ var test = require('tape')

test('type-boolean: different values', function (t) {
var cli = cliArgs(optionDefinitions)
t.deepEqual(
cli.parse([ '--one' ]),
cliArgs(optionDefinitions, [ '--one' ]),
{ one: true }
)
t.deepEqual(
cli.parse([ '--one', 'true' ]),
cliArgs(optionDefinitions, [ '--one', 'true' ]),
{ one: true }
)
t.deepEqual(
cli.parse([ '--one', 'false' ]),
cliArgs(optionDefinitions, [ '--one', 'false' ]),
{ one: true }
)
t.deepEqual(
cli.parse([ '--one', 'sfsgf' ]),
cliArgs(optionDefinitions, [ '--one', 'sfsgf' ]),
{ one: true }

@@ -26,0 +25,0 @@ )

@@ -11,3 +11,3 @@ var test = require('tape')

var argv = []
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {})

@@ -19,3 +19,3 @@ t.end()

var argv = [ '--one', '--two' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -30,3 +30,3 @@ one: true,

var argv = [ '--one', '--two' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -41,3 +41,3 @@ one: true,

var argv = [ '--one', 'one', '--two' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -52,3 +52,3 @@ one: 'one',

var argv = [ '--one', 'one', '--two', 'two' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -55,0 +55,0 @@ one: 'one',

@@ -10,3 +10,3 @@ var test = require('tape')

var argv = [ '--array', '1', '2', '3' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -23,3 +23,3 @@ array: [ 1, 2, 3 ]

var argv = [ '--array', '1', '--array', '2', '--array', '3' ]
var result = cliArgs(optionDefinitions).parse(argv)
var result = cliArgs(optionDefinitions, argv)
t.deepEqual(result, {

@@ -26,0 +26,0 @@ array: [ 1, 2, 3 ]

@@ -10,14 +10,14 @@ var test = require('tape')

t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one', '1' ]),
cliArgs(optionDefinitions, [ '--one', '1' ]),
{ one: 1 }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one' ]),
cliArgs(optionDefinitions, [ '--one' ]),
{ one: null }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one', '-1' ]),
cliArgs(optionDefinitions, [ '--one', '-1' ]),
{ one: -1 }
)
var result = cliArgs(optionDefinitions).parse([ '--one', 'asdf' ])
var result = cliArgs(optionDefinitions, [ '--one', 'asdf' ])
t.ok(isNaN(result.one))

@@ -24,0 +24,0 @@

@@ -12,11 +12,11 @@ var test = require('tape')

t.deepEqual(
cliArgs(optionDefinitions).parse([ '--file', 'one.js' ]),
cliArgs(optionDefinitions, [ '--file', 'one.js' ]),
{ file: [ 'one.js' ] }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--file', 'one.js', 'two.js' ]),
cliArgs(optionDefinitions, [ '--file', 'one.js', 'two.js' ]),
{ file: [ 'one.js', 'two.js' ] }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--file' ]),
cliArgs(optionDefinitions, [ '--file' ]),
{ file: [] }

@@ -23,0 +23,0 @@ )

@@ -12,7 +12,7 @@ var test = require('tape')

t.deepEqual(
cliArgs(optionDefinitions).parse([ '--file', 'one.js' ]),
cliArgs(optionDefinitions, [ '--file', 'one.js' ]),
{ file: 'one.js' }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--file' ]),
cliArgs(optionDefinitions, [ '--file' ]),
{ file: null }

@@ -19,0 +19,0 @@ )

@@ -10,11 +10,11 @@ var test = require('tape')

t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one', 'yeah' ]),
cliArgs(optionDefinitions, [ '--one', 'yeah' ]),
{ one: 'yeah' }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one' ]),
cliArgs(optionDefinitions, [ '--one' ]),
{ one: null }
)
t.deepEqual(
cliArgs(optionDefinitions).parse([ '--one', '3' ]),
cliArgs(optionDefinitions, [ '--one', '3' ]),
{ one: '3' }

@@ -25,1 +25,10 @@ )

})
/* currently not supported, it would complain --yeah is an invalid option */
test.skip('type-string: pass a value resembling an option', function (t) {
t.deepEqual(
cliArgs(optionDefinitions, [ '--one', '--yeah' ]),
{ one: '--yeah' }
)
t.end()
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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