@dkh-dev/parse-argv
Advanced tools
+56
-29
@@ -1,34 +0,61 @@ | ||
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const isPair = (arg) => /^\-[a-z-]+=/.test(arg); | ||
| const isKey = (arg) => /^\-[a-z-]+$/.test(arg); | ||
| const parseArgv = (argv) => { | ||
| const argm = {}; | ||
| let key = ''; | ||
| const closeCurrentPair = () => { | ||
| 'use strict' | ||
| const isKey = arg => /^-[a-z-]+$/.test(arg) | ||
| const isPair = arg => /^-[a-z-]+=/.test(arg) | ||
| const sanitize = key => key.substr(key.startsWith('--') ? 2 : 1) | ||
| const split = arg => { | ||
| const index = arg.indexOf('=') | ||
| const key = sanitize(arg.substr(0, index)) | ||
| const value = arg.substr(index + 1) | ||
| return [ key, value ] | ||
| } | ||
| const parseArgv = argv => { | ||
| const argm = {} | ||
| let currentKey | ||
| const pair = (value = true, key = currentKey) => { | ||
| if (key) { | ||
| argm[key] = ''; | ||
| key = ''; | ||
| let convertedValue | ||
| if (typeof value === 'string') { | ||
| if (value.startsWith("'") && value.endsWith("'")) { | ||
| convertedValue = value.substr(1, value.length - 2) | ||
| } else { | ||
| const num = Number(value) | ||
| convertedValue = num.toString() === value ? num : value | ||
| } | ||
| } else { | ||
| convertedValue = value | ||
| } | ||
| argm[ key ] = convertedValue | ||
| currentKey = null | ||
| } | ||
| }; | ||
| } | ||
| argv.forEach(arg => { | ||
| if (isPair(arg)) { | ||
| closeCurrentPair(); | ||
| const parts = arg.split('='); | ||
| argm[parts[0].substr(arg.startsWith('--') ? 2 : 1)] = parts[1]; | ||
| pair() | ||
| const [ key, value ] = split(arg) | ||
| pair(value, key) | ||
| } else if (isKey(arg)) { | ||
| pair() | ||
| currentKey = sanitize(arg) | ||
| } else { | ||
| pair(arg) | ||
| } | ||
| else if (isKey(arg)) { | ||
| closeCurrentPair(); | ||
| key = arg.substr(arg.startsWith('--') ? 2 : 1); | ||
| } | ||
| else { | ||
| if (key) { | ||
| argm[key] = arg; | ||
| key = ''; | ||
| } | ||
| } | ||
| }); | ||
| closeCurrentPair(); | ||
| return argm; | ||
| }; | ||
| exports.default = parseArgv; | ||
| }) | ||
| pair() | ||
| return argm | ||
| } | ||
| module.exports = parseArgv |
+4
-10
| { | ||
| "name": "@dkh-dev/parse-argv", | ||
| "version": "1.1.1", | ||
| "version": "2.0.0", | ||
| "description": "Parse process arguments", | ||
| "main": "index.js", | ||
| "types": "index.d.ts", | ||
| "files": [ | ||
| "index.js", | ||
| "index.d.ts" | ||
| "index.js" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "test": "ts-node test --a=b -b -c d e --e -f \"g h\" -i=j --k l -m 0 -o", | ||
| "test": "node test --a=b -b -c d e --e --f=\"g h\" -i=j -j='k l' -k=123", | ||
| "prepublishOnly": "npm run build & npm test" | ||
@@ -36,7 +33,4 @@ }, | ||
| "devDependencies": { | ||
| "@types/tape": "^4.2.33", | ||
| "tape": "^4.10.1", | ||
| "ts-node": "^8.0.2", | ||
| "typescript": "^3.3.3" | ||
| "tape": "^4.10.1" | ||
| } | ||
| } |
+3
-2
@@ -21,4 +21,5 @@ # parseArgv | ||
| console.log(parseArgv(argv)); | ||
| // ts-node index.ts --a=b -c d e -f "g h" -i=j --k l -m 0 | ||
| // => { a: 'b', c: 'd', f: 'g h', m: '0' } | ||
| // $ node test --a=b -b -c d e --e --f="g h" -i=j -j='k l' -l=123 | ||
| // => { a: 'b', b: true, c: 'd', e: true, f: 'g h', i: 'j', j: "'k", k: 123 } | ||
| ```` |
| export interface ArgumentMap { | ||
| [key: string]: string; | ||
| } | ||
| declare const parseArgv: (argv: string[]) => ArgumentMap; | ||
| export default parseArgv; |
3334
7.31%1
-75%46
17.95%25
4.17%4
-20%