Comparing version 2020.8.8 to 2020.8.9
@@ -0,1 +1,6 @@ | ||
### 2020.8.9 | ||
* remove unhandled kind throw | ||
* improved error for unexpected input | ||
### 2020.8.8 | ||
@@ -2,0 +7,0 @@ |
16
index.js
@@ -32,3 +32,3 @@ const parse_arg = (arg) => { | ||
if (next.includes('__proto__')) | ||
throw Error('__proto__ not allowed within an argument to prevent prototype pollution.') | ||
throw Error(`__proto__ not allowed within an argument to prevent prototype pollution.`) | ||
@@ -46,4 +46,3 @@ const [kind, parsed] = acc.skip ? ['a', next] : parse_arg(next) | ||
} | ||
if (kind === 'a') { | ||
else if (kind === 'a') { | ||
if (parsed === '--' && !acc.skip) { | ||
@@ -55,11 +54,9 @@ acc.skip = true | ||
else acc.plain.push(parsed) | ||
return acc | ||
} | ||
if (kind === 'kv') { | ||
else if (kind === 'kv') { | ||
const [k, v] = parsed | ||
acc.args[k] = [...acc.args[k] || [], v] | ||
return acc | ||
} | ||
else throw Error(`Unhandled combine option ${kind}`) | ||
return acc | ||
}, { | ||
@@ -91,2 +88,5 @@ tag: undefined, | ||
if(!Array.isArray(args) || !Array.isArray(alias)) | ||
throw Error(`expected input to be array(s). Eg clia(process.argv.slice(2),['alias','names'])`) | ||
const parsed = combine_input(args) | ||
@@ -93,0 +93,0 @@ |
{ | ||
"name": "clia", | ||
"version": "2020.8.8", | ||
"version": "2020.8.9", | ||
"description": "Command line parser and t3st example project", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
106
README.md
# clia | ||
Command line arguments parser and [t3st](https://www.npmjs.com/package/t3st) example project | ||
Command line arguments parser and [t3st](https://www.npmjs.com/package/t3st) example project. | ||
## usage | ||
In your [nodejs](https://www.w3schools.com/nodejs/) project dir, run: | ||
Example command line input: | ||
```bash | ||
npm i clia | ||
``` | ||
Example command line input: | ||
```bash | ||
node your-node-app hello -a -ab -d world | ||
@@ -29,73 +24,56 @@ ``` | ||
conf === { | ||
arg: { d: 'world' }, | ||
args: { d: [ 'world' ] }, | ||
opt: { a: true, b: true, d: true }, | ||
plain: [ 'hello' ] | ||
// arguments before any options | ||
plain: [ 'hello' ], | ||
// options saved in opt (eg. --a -bd) | ||
opt: { a: true, b: true, d: true }, | ||
// arguments after options are tagged with the last option (eg -d world, or --d world) | ||
// argument --key=value also saved in args, eg --d=world | ||
args: { d: [ 'world' ] }, | ||
// the first value of each args property, so that you can use arg.prop instead of args.prop[0] | ||
arg: { d: 'world' }, | ||
} | ||
``` | ||
## parlance | ||
``` | ||
node your-app a -b c --d --e=f | ||
``` | ||
``` | ||
a: plain argument | ||
b: short option | ||
c: tagged argument | ||
d: long option | ||
e+f: key-value | ||
``` | ||
### **option**: boolean flag | ||
* **long** option | ||
* starts with double `--` | ||
* refers to **one** option | ||
* **short** option | ||
* starts with single `-` | ||
* refers to one **or more** options | ||
### **argument**: string of one or more character(s) | ||
* **plain**: | ||
* argument(s) preceding any options | ||
* **tagged** | ||
* argument(s) succeeding the last short option or long option | ||
* The `arg` object returns the first `args` if there are any | ||
* **key-value** | ||
* `--key=value` tagged argument that only sets the argument | ||
* When a key-value option is stated more than once, all values are saved under `args`. | ||
## parsing | ||
* If `--` is encountered, it is ignored. All subsequent inputs are treated as arguments. | ||
* An error is thrown when: | ||
* any argument containts `__proto__` *to prevent prototype pollution* | ||
* key-value pair with missing key or value, eg: `--store=` or `--=pet` | ||
If `--` is encountered, it is ignored. All subsequent inputs are treated as arguments. | ||
## testing | ||
An error is thrown when: | ||
* any argument containts `__proto__` *to prevent prototype pollution* | ||
* key-value pair with missing key or value, eg: `--store=` or `--=pet` | ||
Clone and run tests: | ||
## alias | ||
```bash | ||
git clone https://github.com/devmachiine/clia.git | ||
cd clia | ||
npm test | ||
```javascript | ||
clia('run -o yaml --d=/usr/bin --fruit=mango'.split(' ') | ||
, ['output', 'directory', 'fruit']) | ||
``` | ||
To run live _(aka hot-reload)_ tests: | ||
```bash | ||
npm i && npm i -g nodemon # optional to speed up reload | ||
# ctrl+c to exit. | ||
npm start | ||
yields | ||
```javascript | ||
{ | ||
arg: { | ||
o: 'yaml', output: 'yaml', | ||
d: '/usr/bin', directory: '/usr/bin', | ||
fruit: 'mango' | ||
}, | ||
args: { | ||
o: ['yaml'], output: ['yaml'], | ||
d: ['/usr/bin'], directory: ['/usr/bin'], | ||
// note key-value doesn't set option | ||
// even when kv/value matches alias | ||
fruit: ['mango'] | ||
}, | ||
// note key-value doesn't set opt | ||
// even when kv/value is short option that has an alias | ||
opt: { o: true, output: true }, | ||
plain: ['run'] | ||
} | ||
``` | ||
## references | ||
## Docs | ||
[The Art of Unix Programming](http://www.catb.org/~esr/writings/taoup/html/ch10s05.html) | ||
[All examples here](https://github.com/devmachiine/clia/tree/master/tests) | ||
[GNU argument syntax conventions](https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html) | ||
[Dev/specs](https://github.com/devmachiine/clia/blob/master/notes.md) | ||
[getopts](https://github.com/jorgebucaran/getopts#readme) (therefore [this IEEE doc](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02)) | ||
@@ -102,0 +80,0 @@ ![CI](https://github.com/devmachiine/clia/workflows/CI/badge.svg) |
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
7375
87