command-line-args
Advanced tools
Comparing version 6.0.0-preview.1 to 6.0.0
22
index.js
@@ -1,2 +0,6 @@ | ||
'use strict' | ||
import Definitions from './lib/option-definitions.js' | ||
import ArgvParser from './lib/argv-parser.js' | ||
import Option from './lib/option.js' | ||
import OutputGrouped from './lib/output-grouped.js' | ||
import Output from './lib/output.js' | ||
@@ -6,3 +10,2 @@ /** | ||
*/ | ||
module.exports = commandLineArgs | ||
@@ -14,3 +17,3 @@ /** | ||
* | ||
* @param {module:definition[]} - An array of [OptionDefinition](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) objects | ||
* @param {Array<OptionDefinition>} - An array of [OptionDefinition](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) objects | ||
* @param {object} [options] - Options. | ||
@@ -21,2 +24,3 @@ * @param {string[]} [options.argv] - An array of strings which, if present will be parsed instead of `process.argv`. | ||
* @param {boolean} [options.camelCase] - If `true`, options with hypenated names (e.g. `move-to`) will be returned in camel-case (e.g. `moveTo`). | ||
* @param {boolean} [options.caseInsensitive] - If `true`, the case of each option name or alias parsed is insignificant. In other words, both `--Verbose` and `--verbose`, `-V` and `-v` would be equivalent. Defaults to false. | ||
* @returns {object} | ||
@@ -39,13 +43,11 @@ * @throws `UNKNOWN_OPTION` If `options.partial` is false and the user set an undefined option. The `err.optionName` property contains the arg that specified an unknown option, e.g. `--one`. | ||
if (options.stopAtFirstUnknown) options.partial = true | ||
const Definitions = require('./lib/option-definitions') | ||
optionDefinitions = Definitions.from(optionDefinitions) | ||
optionDefinitions = Definitions.from(optionDefinitions, options.caseInsensitive) | ||
const ArgvParser = require('./lib/argv-parser') | ||
const parser = new ArgvParser(optionDefinitions, { | ||
argv: options.argv, | ||
stopAtFirstUnknown: options.stopAtFirstUnknown | ||
stopAtFirstUnknown: options.stopAtFirstUnknown, | ||
caseInsensitive: options.caseInsensitive | ||
}) | ||
const Option = require('./lib/option') | ||
const OutputClass = optionDefinitions.isGrouped() ? require('./lib/output-grouped') : require('./lib/output') | ||
const OutputClass = optionDefinitions.isGrouped() ? OutputGrouped : Output | ||
const output = new OutputClass(optionDefinitions) | ||
@@ -87,1 +89,3 @@ | ||
} | ||
export default commandLineArgs |
@@ -1,3 +0,5 @@ | ||
'use strict' | ||
const argvTools = require('argv-tools') | ||
import * as argvTools from './argv-tools.js' | ||
import Definitions from './option-definitions.js' | ||
import findReplace from 'find-replace' | ||
import t from 'typical' | ||
@@ -16,52 +18,29 @@ /** | ||
* @param {string[]} [options.argv] - Overrides `process.argv` | ||
* @param {boolean} [options.stopAtFirstUnknown] | ||
* @param {boolean} [options.stopAtFirstUnknown] - | ||
* @param {boolean} [options.caseInsensitive] - Arguments will be parsed in a case insensitive manner. Defaults to false. | ||
*/ | ||
constructor (definitions, options) { | ||
this.options = Object.assign({}, options) | ||
const Definitions = require('./option-definitions') | ||
/** | ||
* Option Definitions | ||
* @type {OptionDefinition[]} | ||
*/ | ||
this.definitions = Definitions.from(definitions) | ||
this.definitions = Definitions.from(definitions, this.options.caseInsensitive) | ||
/** | ||
* Argv | ||
* @type {Array<string|object>} | ||
*/ | ||
this.argv = argvTools.ArgvArray.from(this.options.argv) | ||
if (this.argv.hasCombinedShortOptions()) { | ||
this.expandCluster() | ||
findReplace(this.argv, argvTools.re.combinedShort.test.bind(argvTools.re.combinedShort), arg => { | ||
arg = arg.slice(1) | ||
return arg.split('').map(letter => ({ origArg: `-${arg}`, arg: '-' + letter })) | ||
}) | ||
} | ||
} | ||
expandCluster () { | ||
const findReplace = require('find-replace') | ||
findReplace(this.argv, argvTools.re.combinedShort, arg => { | ||
const result = [] | ||
arg = arg.slice(1) | ||
for (const letter of arg.split('')) { | ||
const def = this.definitions.get(`-${letter}`) | ||
if (def) { | ||
if (def.isBoolean()) { | ||
result.push({ origArg: `-${arg}`, arg: `-${letter}` }) | ||
} else { | ||
result.push({ origArg: `-${arg}`, arg: `-${letter}` }) | ||
const attachedValue = arg.slice(arg.indexOf(letter) + 1) | ||
if (attachedValue) { | ||
result.push({ origArg: `-${arg}`, arg: attachedValue }) | ||
} | ||
break | ||
} | ||
} else { | ||
result.push({ origArg: `-${arg}`, arg: `-${letter}` }) | ||
} | ||
} | ||
return result | ||
}) | ||
} | ||
/** | ||
* Yields one `{ event, name, value, arg, def }` argInfo object for each arg in `process.argv` (or `options.argv`). | ||
*/ | ||
* [Symbol.iterator] () { | ||
const definitions = this.definitions | ||
const t = require('typical') | ||
@@ -89,3 +68,3 @@ let def | ||
if (argvTools.isOption(arg)) { | ||
def = definitions.get(arg) | ||
def = definitions.get(arg, this.options.caseInsensitive) | ||
value = undefined | ||
@@ -102,3 +81,3 @@ if (def) { | ||
const matches = arg.match(argvTools.re.optEquals) | ||
def = definitions.get(matches[1]) | ||
def = definitions.get(matches[1], this.options.caseInsensitive) | ||
if (def) { | ||
@@ -163,2 +142,2 @@ if (def.isBoolean()) { | ||
module.exports = ArgvParser | ||
export default ArgvParser |
@@ -1,3 +0,2 @@ | ||
'use strict' | ||
const t = require('typical') | ||
import t from 'typical' | ||
@@ -247,3 +246,3 @@ /** | ||
/* pick up any remaining properties */ | ||
for (let prop in definition) { | ||
for (const prop in definition) { | ||
if (!this[prop]) this[prop] = definition[prop] | ||
@@ -256,2 +255,3 @@ } | ||
} | ||
isMultiple () { | ||
@@ -267,2 +267,2 @@ return this.multiple || this.lazyMultiple | ||
module.exports = OptionDefinition | ||
export default OptionDefinition |
@@ -1,5 +0,5 @@ | ||
'use strict' | ||
const arrayify = require('array-back') | ||
const argvTools = require('argv-tools') | ||
const t = require('typical') | ||
import arrayify from 'array-back' | ||
import * as argvTools from './argv-tools.js' | ||
import t from 'typical' | ||
import Definition from './option-definition.js' | ||
@@ -16,5 +16,6 @@ /** | ||
* validate option definitions | ||
* @param {boolean} [caseInsensitive=false] - whether arguments will be parsed in a case insensitive manner | ||
* @returns {string} | ||
*/ | ||
validate () { | ||
validate (caseInsensitive) { | ||
const someHaveNoName = this.some(def => !def.name) | ||
@@ -71,3 +72,3 @@ if (someHaveNoName) { | ||
const duplicateName = hasDuplicates(this.map(def => def.name)) | ||
const duplicateName = hasDuplicates(this.map(def => caseInsensitive ? def.name.toLowerCase() : def.name)) | ||
if (duplicateName) { | ||
@@ -80,3 +81,3 @@ halt( | ||
const duplicateAlias = hasDuplicates(this.map(def => def.alias)) | ||
const duplicateAlias = hasDuplicates(this.map(def => caseInsensitive && t.isDefined(def.alias) ? def.alias.toLowerCase() : def.alias)) | ||
if (duplicateAlias) { | ||
@@ -89,3 +90,3 @@ halt( | ||
const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)) | ||
const duplicateDefaultOption = this.filter(def => def.defaultOption === true).length > 1 | ||
if (duplicateDefaultOption) { | ||
@@ -112,10 +113,25 @@ halt( | ||
* Get definition by option arg (e.g. `--one` or `-o`) | ||
* @param {string} | ||
* @param {string} [arg] the argument name to get the definition for | ||
* @param {boolean} [caseInsensitive] whether to use case insensitive comparisons when finding the appropriate definition | ||
* @returns {Definition} | ||
*/ | ||
get (arg) { | ||
get (arg, caseInsensitive) { | ||
if (argvTools.isOption(arg)) { | ||
return argvTools.re.short.test(arg) | ||
? this.find(def => def.alias === argvTools.getOptionName(arg)) | ||
: this.find(def => def.name === argvTools.getOptionName(arg)) | ||
if (argvTools.re.short.test(arg)) { | ||
const shortOptionName = argvTools.getOptionName(arg) | ||
if (caseInsensitive) { | ||
const lowercaseShortOptionName = shortOptionName.toLowerCase() | ||
return this.find(def => t.isDefined(def.alias) && def.alias.toLowerCase() === lowercaseShortOptionName) | ||
} else { | ||
return this.find(def => def.alias === shortOptionName) | ||
} | ||
} else { | ||
const optionName = argvTools.getOptionName(arg) | ||
if (caseInsensitive) { | ||
const lowercaseOptionName = optionName.toLowerCase() | ||
return this.find(def => def.name.toLowerCase() === lowercaseOptionName) | ||
} else { | ||
return this.find(def => def.name === optionName) | ||
} | ||
} | ||
} else { | ||
@@ -137,5 +153,7 @@ return this.find(def => def.name === arg) | ||
} | ||
whereNotGrouped () { | ||
return this.filter(def => !containsValidGroup(def)) | ||
} | ||
whereDefaultValueSet () { | ||
@@ -145,7 +163,6 @@ return this.filter(def => t.isDefined(def.defaultValue)) | ||
static from (definitions) { | ||
static from (definitions, caseInsensitive) { | ||
if (definitions instanceof this) return definitions | ||
const Definition = require('./option-definition') | ||
const result = super.from(arrayify(definitions), def => Definition.create(def)) | ||
result.validate() | ||
result.validate(caseInsensitive) | ||
return result | ||
@@ -177,2 +194,2 @@ } | ||
module.exports = Definitions | ||
export default Definitions |
@@ -1,3 +0,2 @@ | ||
'use strict' | ||
const Option = require('./option') | ||
import Option from './option.js' | ||
@@ -14,2 +13,2 @@ class FlagOption extends Option { | ||
module.exports = FlagOption | ||
export default FlagOption |
@@ -1,6 +0,5 @@ | ||
'use strict' | ||
import arrayify from 'array-back' | ||
import t from 'typical' | ||
import Definition from './option-definition.js' | ||
const _value = new WeakMap() | ||
const arrayify = require('array-back') | ||
const t = require('typical') | ||
const Definition = require('./option-definition') | ||
@@ -76,3 +75,3 @@ /** | ||
if (definition.isBoolean()) { | ||
return require('./option-flag').create(definition) | ||
return FlagOption.create(definition) | ||
} else { | ||
@@ -84,2 +83,12 @@ return new this(definition) | ||
module.exports = Option | ||
class FlagOption extends Option { | ||
set (val) { | ||
super.set(true) | ||
} | ||
static create (def) { | ||
return new this(def) | ||
} | ||
} | ||
export default Option |
@@ -1,9 +0,8 @@ | ||
'use strict' | ||
const Output = require('./output') | ||
import Output from './output.js' | ||
import arrayify from 'array-back' | ||
import t from 'typical' | ||
import camelCase from 'lodash.camelcase' | ||
class GroupedOutput extends Output { | ||
toObject (options) { | ||
const arrayify = require('array-back') | ||
const t = require('typical') | ||
const camelCase = require('lodash.camelcase') | ||
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown }) | ||
@@ -41,2 +40,2 @@ const superOutput = super.toObject(options) | ||
module.exports = GroupedOutput | ||
export default GroupedOutput |
@@ -1,3 +0,4 @@ | ||
'use strict' | ||
const Option = require('./option') | ||
import Option from './option.js' | ||
import Definitions from './option-definitions.js' | ||
import camelCase from 'lodash.camelcase' | ||
@@ -10,3 +11,2 @@ /** | ||
super() | ||
const Definitions = require('./option-definitions') | ||
/** | ||
@@ -25,3 +25,2 @@ * @type {OptionDefinitions} | ||
toObject (options) { | ||
const camelCase = require('lodash.camelcase') | ||
options = options || {} | ||
@@ -41,2 +40,2 @@ const output = {} | ||
module.exports = Output | ||
export default Output |
{ | ||
"name": "command-line-args", | ||
"version": "6.0.0-preview.1", | ||
"version": "6.0.0", | ||
"description": "A mature, feature-complete library to parse command-line options.", | ||
"repository": "https://github.com/75lb/command-line-args.git", | ||
"repository": "https://github.com/75lb/command-line-args", | ||
"scripts": { | ||
"test": "test-runner test/*.js test/internals/*.js", | ||
"test": "npm run dist && npm run test:ci", | ||
"test:ci": "test-runner 'test/**/*.js' 'test/**/*.cjs'", | ||
"docs": "jsdoc2md index.js > doc/API.md && jsdoc2md lib/option-definition.js > doc/option-definition.md", | ||
"cover": "nyc --reporter=text-lcov test-runner test/*.js test/internals/*.js | coveralls" | ||
"dist": "rollup -c" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
"import": "./index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"keywords": [ | ||
@@ -27,20 +33,28 @@ "argv", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=12.20" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lib" | ||
"lib", | ||
"dist" | ||
], | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"test-runner": "^0.5.0" | ||
"@rollup/plugin-commonjs": "^26.0.1", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"jsdoc-to-markdown": "^8.0.1", | ||
"rollup": "~4.18.0", | ||
"test-runner": "^0.10.1" | ||
}, | ||
"dependencies": { | ||
"argv-tools": "^0.1.1", | ||
"array-back": "^2.0.0", | ||
"find-replace": "^2.0.1", | ||
"array-back": "^6.2.2", | ||
"find-replace": "^5.0.1", | ||
"lodash.camelcase": "^4.3.0", | ||
"typical": "^2.6.1" | ||
"typical": "^7.1.1" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"dist" | ||
], | ||
"envs": [] | ||
} | ||
} |
@@ -1,8 +0,7 @@ | ||
[![view on npm](https://img.shields.io/npm/v/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
[![npm module downloads](https://img.shields.io/npm/dt/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
[![Build Status](https://travis-ci.org/75lb/command-line-args.svg?branch=master)](https://travis-ci.org/75lb/command-line-args) | ||
[![Coverage Status](https://coveralls.io/repos/github/75lb/command-line-args/badge.svg?branch=master)](https://coveralls.io/github/75lb/command-line-args?branch=master) | ||
[![Dependency Status](https://david-dm.org/75lb/command-line-args.svg)](https://david-dm.org/75lb/command-line-args) | ||
[![view on npm](https://badgen.net/npm/v/command-line-args)](https://www.npmjs.org/package/command-line-args) | ||
[![npm module downloads](https://badgen.net/npm/dt/command-line-args)](https://www.npmjs.org/package/command-line-args) | ||
[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/command-line-args)](https://github.com/75lb/command-line-args/network/dependents?dependent_type=REPOSITORY) | ||
[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/command-line-args)](https://github.com/75lb/command-line-args/network/dependents?dependent_type=PACKAGE) | ||
[![Node.js CI](https://github.com/75lb/command-line-args/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/command-line-args/actions/workflows/node.js.yml) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
[![Join the chat at https://gitter.im/75lb/command-line-args](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/75lb/command-line-args?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -12,5 +11,7 @@ ***Upgraders, please read the [release notes](https://github.com/75lb/command-line-args/releases)*** | ||
# command-line-args | ||
A mature, feature-complete library to parse command-line options. | ||
## Synopsis | ||
You can set options using the main notation standards ([learn more](https://github.com/75lb/command-line-args/wiki/Notation-rules)). These commands are all equivalent, setting the same values: | ||
@@ -104,2 +105,2 @@ ``` | ||
© 2014-18 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown). | ||
© 2014-24 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown). |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
96096
4
13
2746
1
105
Yes
5
+ Addedarray-back@6.2.2(transitive)
+ Addedfind-replace@5.0.1(transitive)
+ Addedtypical@7.1.1(transitive)
- Removedargv-tools@^0.1.1
- Removedargv-tools@0.1.2(transitive)
- Removedarray-back@2.0.0(transitive)
- Removedfind-replace@2.0.1(transitive)
- Removedtest-value@3.0.0(transitive)
- Removedtypical@2.6.1(transitive)
Updatedarray-back@^6.2.2
Updatedfind-replace@^5.0.1
Updatedtypical@^7.1.1