Comparing version 2020.8.5 to 2020.8.6
@@ -7,2 +7,6 @@ ### change type: f / x / m / p | ||
### 2020.8.6 | ||
* **f** `--` marks all subsequent inputs as arguments | ||
### 2020.8.5 | ||
@@ -9,0 +13,0 @@ |
39
index.js
@@ -7,7 +7,9 @@ // a argument, o option, kv key-value | ||
if (arg.startsWith('--')) { | ||
// single option OR key-value | ||
if (arg === '--') | ||
return ['a', '--'] | ||
const flag = arg.slice(2) | ||
if (flag === '') | ||
throw Error('-- not supported yet. Expected extra character, eg: --h --v ..etc') | ||
else if (flag.includes('=')) { | ||
if (flag.includes('=')) { | ||
// key-value | ||
const [option, value] = flag.split('=') | ||
@@ -20,3 +22,3 @@ if (option === '' || value === '') | ||
} | ||
else return ['o', [flag]] | ||
else return ['o', [flag]] //single option | ||
} | ||
@@ -41,3 +43,3 @@ else if (arg.startsWith('-') && arg.length > 1) { | ||
opts.reduce((acc, next) => { | ||
const [kind, parsed] = parse_arg(next) | ||
const [kind, parsed] = acc.skip ? ['a', next] : parse_arg(next) | ||
@@ -57,15 +59,19 @@ if (kind === 'o') { | ||
acc.opts.$$.push(parsed) | ||
if (parsed === '--' && !acc.skip) { | ||
acc.skip = true | ||
} else { | ||
acc.opts.$$.push(parsed) | ||
if (acc.tag) { | ||
const prop = '$' + acc.tag | ||
if (!acc.opts[prop]) | ||
acc.opts[prop] = [parsed] | ||
else acc.opts[prop].push(parsed) | ||
if (acc.tag) { | ||
const prop = '$' + acc.tag | ||
if (!acc.opts[prop]) | ||
acc.opts[prop] = [parsed] | ||
else acc.opts[prop].push(parsed) | ||
} | ||
if (acc.tag) { | ||
add_args(acc, acc.tag, parsed) | ||
} else acc.plain.push(parsed) | ||
} | ||
if (acc.tag) { | ||
add_args(acc, acc.tag, parsed) | ||
} else acc.plain.push(parsed) | ||
return acc | ||
@@ -93,2 +99,3 @@ | ||
tag: undefined, | ||
skip: false, | ||
opt: {}, | ||
@@ -95,0 +102,0 @@ args: {}, |
{ | ||
"name": "clia", | ||
"version": "2020.8.5", | ||
"version": "2020.8.6", | ||
"description": "Command line parser and t3st example project", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,3 +15,3 @@ # clia | ||
From the command line input flags (abcd) and unflagged (hello) | ||
From the command line input: | ||
```bash | ||
@@ -44,3 +44,3 @@ node your-node-app -a -ab -cd hello | ||
## option -> boolean flag(s) | ||
### option -> boolean flag(s) | ||
@@ -68,2 +68,3 @@ * **short** option | ||
* When a key-value option is stated more than once, the last value is used assigned | ||
* If a `--` is encountered, it is ignored. All subsequent inputs are treated as arguments. | ||
@@ -73,3 +74,3 @@ ## errors are thrown for: | ||
* `__proto__` to prevent prototype pollution | ||
* dangling `--` argument (WIP `--` will indicate all subsequent input to be treated as arguments) | ||
* key-value pair with missing value, eg: `--store=` | ||
@@ -76,0 +77,0 @@ ## testing |
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
7384
88
107