command-line-args
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -37,5 +37,5 @@ <a name="module_option-definition"></a> | ||
| --- | -------------------- | ------------ | | ||
| 2 | `--file` | `{ file: null }` | | ||
| 1 | `--file` | `{ file: null }` | | ||
| 2 | `--file lib.js` | `{ file: 'lib.js' }` | | ||
| 4 | `--depth 2` | `{ depth: '2' }` | | ||
| 3 | `--depth 2` | `{ depth: '2' }` | | ||
@@ -57,3 +57,3 @@ Unicode option names and aliases are valid, for example: | ||
The most common values used are `String`, `Number` and `Boolean` but you can use a custom function, for example: | ||
The most common values used are `String` (the default), `Number` and `Boolean` but you can use a custom function, for example: | ||
@@ -65,3 +65,2 @@ ```js | ||
constructor (filename) { | ||
const fs = require('fs') | ||
this.filename = filename | ||
@@ -137,3 +136,4 @@ this.exists = fs.existsSync(filename) | ||
const optionDefinitions = [ | ||
{ name: 'files', type: String, lazyMultiple: true } | ||
{ name: 'files', lazyMultiple: true }, | ||
{ name: 'verbose', alias: 'v', type: Boolean, lazyMultiple: true } | ||
] | ||
@@ -145,2 +145,3 @@ ``` | ||
| 1 | `--files one.js --files two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
| 2 | `-vvv` | `{ verbose: [ true, true, true ] }` | | ||
@@ -151,7 +152,7 @@ **Kind**: instance property of [<code>OptionDefinition</code>](#exp_module_option-definition--OptionDefinition) | ||
#### option.defaultOption : <code>boolean</code> | ||
Any values unaccounted for by an option definition 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. `$ example *.js` instead of `$ example --files *.js`). | ||
Any values unaccounted for by an option definition will be set on the `defaultOption`. This flag is typically set on the most commonly-used option to make for more concise usage (i.e. `$ example *.js` instead of `$ example --files *.js`). | ||
```js | ||
const optionDefinitions = [ | ||
{ name: 'files', type: String, multiple: true, defaultOption: true } | ||
{ name: 'files', multiple: true, defaultOption: true } | ||
] | ||
@@ -174,3 +175,3 @@ ``` | ||
const optionDefinitions = [ | ||
{ name: 'files', type: String, multiple: true, defaultValue: [ 'one.js' ] }, | ||
{ name: 'files', multiple: true, defaultValue: [ 'one.js' ] }, | ||
{ name: 'max', type: Number, defaultValue: 3 } | ||
@@ -177,0 +178,0 @@ ] |
@@ -81,5 +81,3 @@ 'use strict' | ||
const result = output.toObject({ skipUnknown: !options.partial }) | ||
const optionUtil = require('./lib/option-util') | ||
return options.camelCase ? optionUtil.camelCaseObject(result) : result | ||
return output.toObject({ skipUnknown: !options.partial, camelCase: options.camelCase }) | ||
} |
@@ -29,5 +29,5 @@ 'use strict' | ||
* | --- | -------------------- | ------------ | | ||
* | 2 | `--file` | `{ file: null }` | | ||
* | 1 | `--file` | `{ file: null }` | | ||
* | 2 | `--file lib.js` | `{ file: 'lib.js' }` | | ||
* | 4 | `--depth 2` | `{ depth: '2' }` | | ||
* | 3 | `--depth 2` | `{ depth: '2' }` | | ||
* | ||
@@ -49,3 +49,3 @@ * Unicode option names and aliases are valid, for example: | ||
* | ||
* The most common values used are `String`, `Number` and `Boolean` but you can use a custom function, for example: | ||
* The most common values used are `String` (the default), `Number` and `Boolean` but you can use a custom function, for example: | ||
* | ||
@@ -57,3 +57,2 @@ * ```js | ||
* constructor (filename) { | ||
* const fs = require('fs') | ||
* this.filename = filename | ||
@@ -132,3 +131,4 @@ * this.exists = fs.existsSync(filename) | ||
* const optionDefinitions = [ | ||
* { name: 'files', type: String, lazyMultiple: true } | ||
* { name: 'files', lazyMultiple: true }, | ||
* { name: 'verbose', alias: 'v', type: Boolean, lazyMultiple: true } | ||
* ] | ||
@@ -140,2 +140,3 @@ * ``` | ||
* | 1 | `--files one.js --files two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 2 | `-vvv` | `{ verbose: [ true, true, true ] }` | | ||
* | ||
@@ -147,7 +148,7 @@ * @type {boolean} | ||
/** | ||
* Any values unaccounted for by an option definition 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. `$ example *.js` instead of `$ example --files *.js`). | ||
* Any values unaccounted for by an option definition will be set on the `defaultOption`. This flag is typically set on the most commonly-used option to make for more concise usage (i.e. `$ example *.js` instead of `$ example --files *.js`). | ||
* | ||
* ```js | ||
* const optionDefinitions = [ | ||
* { name: 'files', type: String, multiple: true, defaultOption: true } | ||
* { name: 'files', multiple: true, defaultOption: true } | ||
* ] | ||
@@ -171,3 +172,3 @@ * ``` | ||
* const optionDefinitions = [ | ||
* { name: 'files', type: String, multiple: true, defaultValue: [ 'one.js' ] }, | ||
* { name: 'files', multiple: true, defaultValue: [ 'one.js' ] }, | ||
* { name: 'max', type: Number, defaultValue: 3 } | ||
@@ -174,0 +175,0 @@ * ] |
@@ -8,2 +8,4 @@ 'use strict' | ||
const t = require('typical') | ||
const camelCase = require('lodash.camelcase') | ||
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown }) | ||
const superOutput = super.toObject(options) | ||
@@ -18,7 +20,8 @@ const unknown = superOutput._unknown | ||
this.definitions.whereGrouped().forEach(def => { | ||
const outputValue = superOutput[def.name] | ||
const name = options.camelCase ? camelCase(def.name) : def.name | ||
const outputValue = superOutputNoCamel[def.name] | ||
for (const groupName of arrayify(def.group)) { | ||
grouped[groupName] = grouped[groupName] || {} | ||
if (t.isDefined(outputValue)) { | ||
grouped[groupName][def.name] = outputValue | ||
grouped[groupName][name] = outputValue | ||
} | ||
@@ -29,6 +32,7 @@ } | ||
this.definitions.whereNotGrouped().forEach(def => { | ||
const outputValue = superOutput[def.name] | ||
const name = options.camelCase ? camelCase(def.name) : def.name | ||
const outputValue = superOutputNoCamel[def.name] | ||
if (t.isDefined(outputValue)) { | ||
if (!grouped._none) grouped._none = {} | ||
grouped._none[def.name] = outputValue | ||
grouped._none[name] = outputValue | ||
} | ||
@@ -35,0 +39,0 @@ }) |
@@ -24,6 +24,7 @@ 'use strict' | ||
toObject (options) { | ||
const camelCase = require('lodash.camelcase') | ||
options = options || {} | ||
const output = {} | ||
for (const item of this) { | ||
const name = item[0] | ||
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0] | ||
const option = item[1] | ||
@@ -30,0 +31,0 @@ if (name === '_unknown' && !option.get().length) continue |
{ | ||
"name": "command-line-args", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "A mature, feature-complete library to parse command-line options.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/75lb/command-line-args.git", |
@@ -42,3 +42,3 @@ [![view on npm](https://img.shields.io/npm/v/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
{ | ||
files: [ | ||
src: [ | ||
'one.js', | ||
@@ -56,3 +56,3 @@ 'two.js' | ||
* [Command-based syntax (like git)](https://github.com/75lb/command-line-args/wiki/Implement-command-parsing-(git-style)) in the form: | ||
* [Command-based syntax](https://github.com/75lb/command-line-args/wiki/Implement-command-parsing-(git-style)) (git style) in the form: | ||
@@ -66,6 +66,6 @@ ``` | ||
``` | ||
$ git commit --squash -m This is my commit message | ||
$ git commit --squash -m "This is my commit message" | ||
``` | ||
* [Command and sub-command syntax (like docker)](https://github.com/75lb/command-line-args/wiki/Implement-multiple-command-parsing-(docker-style)) in the form: | ||
* [Command and sub-command syntax](https://github.com/75lb/command-line-args/wiki/Implement-multiple-command-parsing-(docker-style)) (docker style) in the form: | ||
@@ -72,0 +72,0 @@ ``` |
@@ -8,3 +8,3 @@ 'use strict' | ||
runner.test('camel-case', function () { | ||
runner.test('camel-case: regular', function () { | ||
const optionDefinitions = [ | ||
@@ -21,1 +21,60 @@ { name: 'one-two' }, | ||
}) | ||
runner.test('camel-case: grouped', function () { | ||
const optionDefinitions = [ | ||
{ name: 'one-one', group: 'a' }, | ||
{ name: 'two-two', group: 'a' }, | ||
{ name: 'three-three', group: 'b', type: Boolean }, | ||
{ name: 'four-four' } | ||
] | ||
const argv = [ '--one-one', '1', '--two-two', '2', '--three-three', '--four-four', '4' ] | ||
const result = commandLineArgs(optionDefinitions, { argv, camelCase: true }) | ||
a.deepStrictEqual(result, { | ||
a: { | ||
oneOne: '1', | ||
twoTwo: '2' | ||
}, | ||
b: { | ||
threeThree: true | ||
}, | ||
_all: { | ||
oneOne: '1', | ||
twoTwo: '2', | ||
threeThree: true, | ||
fourFour: '4' | ||
}, | ||
_none: { | ||
fourFour: '4' | ||
} | ||
}) | ||
}) | ||
runner.test('camel-case: grouped with unknowns', function () { | ||
const optionDefinitions = [ | ||
{ name: 'one-one', group: 'a' }, | ||
{ name: 'two-two', group: 'a' }, | ||
{ name: 'three-three', group: 'b', type: Boolean }, | ||
{ name: 'four-four' } | ||
] | ||
const argv = [ '--one-one', '1', '--two-two', '2', '--three-three', '--four-four', '4', '--five' ] | ||
const result = commandLineArgs(optionDefinitions, { argv, camelCase: true, partial: true }) | ||
a.deepStrictEqual(result, { | ||
a: { | ||
oneOne: '1', | ||
twoTwo: '2' | ||
}, | ||
b: { | ||
threeThree: true | ||
}, | ||
_all: { | ||
oneOne: '1', | ||
twoTwo: '2', | ||
threeThree: true, | ||
fourFour: '4' | ||
}, | ||
_none: { | ||
fourFour: '4' | ||
}, | ||
_unknown: [ '--five' ] | ||
}) | ||
}) |
@@ -121,1 +121,55 @@ 'use strict' | ||
}) | ||
runner.test('groups: with partial', function () { | ||
const definitions = [ | ||
{ name: 'one', group: 'a' }, | ||
{ name: 'two', group: 'a' }, | ||
{ name: 'three', group: 'b' } | ||
] | ||
const argv = [ '--one', '1', '--two', '2', '--three', '3', 'ham', '--cheese' ] | ||
a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
b: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
}, | ||
_unknown: [ 'ham', '--cheese' ] | ||
}) | ||
}) | ||
runner.test('partial: with partial, multiple groups and _none', function () { | ||
const definitions = [ | ||
{ name: 'one', group: ['a', 'f'] }, | ||
{ name: 'two', group: ['a', 'g'] }, | ||
{ name: 'three' } | ||
] | ||
const argv = [ '--cheese', '--one', '1', 'ham', '--two', '2', '--three', '3', '-c' ] | ||
a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
f: { | ||
one: '1' | ||
}, | ||
g: { | ||
two: '2' | ||
}, | ||
_none: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
}, | ||
_unknown: [ '--cheese', 'ham', '-c' ] | ||
}) | ||
}) |
@@ -162,56 +162,2 @@ 'use strict' | ||
runner.test('partial: groups', function () { | ||
const definitions = [ | ||
{ name: 'one', group: 'a' }, | ||
{ name: 'two', group: 'a' }, | ||
{ name: 'three', group: 'b' } | ||
] | ||
const argv = [ '--one', '1', '--two', '2', '--three', '3', 'ham', '--cheese' ] | ||
a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
b: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
}, | ||
_unknown: [ 'ham', '--cheese' ] | ||
}) | ||
}) | ||
runner.test('partial: multiple groups and _none', function () { | ||
const definitions = [ | ||
{ name: 'one', group: ['a', 'f'] }, | ||
{ name: 'two', group: ['a', 'g'] }, | ||
{ name: 'three' } | ||
] | ||
const argv = [ '--cheese', '--one', '1', 'ham', '--two', '2', '--three', '3', '-c' ] | ||
a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
f: { | ||
one: '1' | ||
}, | ||
g: { | ||
two: '2' | ||
}, | ||
_none: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
}, | ||
_unknown: [ '--cheese', 'ham', '-c' ] | ||
}) | ||
}) | ||
runner.test('partial: defaultOption with --option=value notation', function () { | ||
@@ -218,0 +164,0 @@ const definitions = [ |
@@ -21,3 +21,3 @@ 'use strict' | ||
runner.skip('stopAtFirstUnknown: with a singlular defaultOption', function () { | ||
runner.test('stopAtFirstUnknown: with a singlular defaultOption', function () { | ||
const optionDefinitions = [ | ||
@@ -30,3 +30,4 @@ { name: 'one', defaultOption: true }, | ||
a.deepStrictEqual(result, { | ||
one: '1' | ||
one: '1', | ||
_unknown: [ '--', '--two', '2' ] | ||
}) | ||
@@ -33,0 +34,0 @@ }) |
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
110725
2815
43