@dkh-dev/parse-argv
Advanced tools
+18
-21
@@ -20,26 +20,22 @@ 'use strict' | ||
| // named arg | ||
| let current | ||
| // auto-index | ||
| let index = 0 | ||
| let current | ||
| // starts a `--name value` pair | ||
| const start = name => { | ||
| // start of a `--name value` pair | ||
| const open = name => { | ||
| current = name | ||
| } | ||
| // ends the current name-value pair | ||
| const end = (value = null, name = current) => { | ||
| // end of the current name-value pair | ||
| const close = (content = true, name = current) => { | ||
| current = null | ||
| if (!name) { | ||
| if (value) { | ||
| args[ index ] = value | ||
| index++ | ||
| } | ||
| const key = name ? clean(name) : index++ | ||
| return | ||
| } | ||
| const number = Number(content) | ||
| const value = number.toString() === content ? number : content | ||
| const number = Number(value) | ||
| args[ clean(name) ] = number.toString() === value ? number : value || true | ||
| args[ key ] = value | ||
| } | ||
@@ -49,10 +45,11 @@ | ||
| if (!arg.startsWith('-')) { | ||
| // this might be a param value | ||
| return void end(arg) | ||
| return void close(arg) | ||
| } | ||
| end() | ||
| if (current) { | ||
| close() | ||
| } | ||
| if (!arg.includes('=')) { | ||
| return void start(arg) | ||
| return void open(arg) | ||
| } | ||
@@ -62,6 +59,6 @@ | ||
| end(value, name) | ||
| close(value, name) | ||
| }) | ||
| end() | ||
| close() | ||
@@ -68,0 +65,0 @@ return args |
+3
-3
| { | ||
| "name": "@dkh-dev/parse-argv", | ||
| "version": "2.1.0", | ||
| "version": "2.1.1", | ||
| "description": "Parse process arguments", | ||
@@ -10,4 +10,4 @@ "main": "index.js", | ||
| "devDependencies": { | ||
| "@dkh-dev/eslint-config": "^1.6.0", | ||
| "tape": "^4.10.1" | ||
| "@dkh-dev/eslint-config": "^1.6.2", | ||
| "tape": "^5.0.1" | ||
| }, | ||
@@ -14,0 +14,0 @@ "scripts": { |
+17
-4
@@ -1,2 +0,2 @@ | ||
| # parseArgv | ||
| # [@dkh-dev/parse-argv](https://www.npmjs.com/package/@dkh-dev/parse-argv) | ||
@@ -18,6 +18,19 @@ _Parse process arguments_ | ||
| console.log(parseArgv(process.argv)); | ||
| console.log(parseArgv(process.argv.slice(2))); | ||
| // $ node test --a=b -b -c d e --e --f="g h" -i=123 -j=/k l/ -k | ||
| // => { a: 'b', b: true, c: 'd', e: true, f: 'g h', i: 123, j: '/k', k: true } | ||
| // $ node test parse --a=b -b -c d e --e --f="g h" -i=123 -j=/k l/ -k | ||
| /* => { | ||
| '0': 'parse', | ||
| '1': 'e', | ||
| '2': 'l/', | ||
| a: 'b', | ||
| b: true, | ||
| c: 'd', | ||
| e: true, | ||
| f: 'g h', | ||
| i: 123, | ||
| j: '/k', | ||
| k: true | ||
| } | ||
| */ | ||
| ```` |
3308
6.2%36
56.52%45
-4.26%