Comparing version 1.1.2 to 1.2.0
@@ -22,4 +22,21 @@ { | ||
] | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Launch Doc", | ||
"program": "${workspaceFolder}/node_modules/.bin/doc", | ||
"env": { | ||
"NODE_DEBUG": "doc", | ||
"BABEL_ENV": "debug", | ||
"ZOROASTER_TIMEOUT": "9999999" | ||
}, | ||
"console": "integratedTerminal", | ||
"args": [ | ||
"README-source.md", | ||
"-o", | ||
"README.md" | ||
] | ||
} | ||
] | ||
} |
@@ -8,17 +8,38 @@ "use strict"; | ||
/** | ||
* | ||
* @param {string[]} argv | ||
* @param {*} long | ||
* @param {*} short | ||
* @param {*} bool | ||
* @param {*} number | ||
*/ | ||
const find = (argv, long, short, bool, number) => { | ||
const re = new RegExp(`^-(${short}|-${long})`); | ||
const found = argv.find(a => re.test(a)); | ||
if (!found) return; | ||
if (bool) return true; | ||
const i = argv.indexOf(found); | ||
const i = argv.findIndex(a => re.test(a)); | ||
if (i == -1) return { | ||
argv | ||
}; | ||
if (bool) { | ||
return { | ||
value: true, | ||
argv: [...argv.slice(0, i), ...argv.slice(i + 1)] | ||
}; | ||
} | ||
const j = i + 1; | ||
const value = argv[j]; | ||
if (!value) return; | ||
let value = argv[j]; | ||
if (!value || typeof value == 'string' && value.startsWith('--')) return { | ||
argv | ||
}; | ||
if (number) { | ||
return parseInt(value, 10); | ||
value = parseInt(value, 10); | ||
} | ||
return value; | ||
return { | ||
value, | ||
argv: [...argv.slice(0, i), ...argv.slice(j + 1)] | ||
}; | ||
}; | ||
@@ -62,12 +83,23 @@ /** | ||
function argufy(config = {}, args = process.argv) { | ||
const [,, ...argv] = args; | ||
let [,, ...argv] = args; | ||
/** @type {string} */ | ||
const titles = findTitles(argv); | ||
const res = Object.keys(config).reduce((acc, key) => { | ||
argv = argv.slice(titles.length); | ||
const res = Object.keys(config).reduce(({ | ||
_argv, | ||
...acc | ||
}, key) => { | ||
if (_argv.length == 0) return { | ||
_argv, | ||
...acc | ||
}; | ||
const val = config[key]; | ||
let r; | ||
let value; | ||
if (typeof val == 'string') { | ||
r = find(args, key, val); | ||
({ | ||
value, | ||
argv: _argv | ||
} = find(_argv, key, val)); | ||
} else { | ||
@@ -84,18 +116,32 @@ try { | ||
if (command && multiple && titles.length) { | ||
r = titles; | ||
value = titles; | ||
} else if (command && titles.length) { | ||
r = titles[0]; | ||
value = titles[0]; | ||
} else { | ||
r = find(args, key, short, boolean, number); | ||
({ | ||
value, | ||
argv: _argv | ||
} = find(_argv, key, short, boolean, number)); | ||
} | ||
} catch (err) { | ||
return acc; | ||
return { | ||
_argv, | ||
...acc | ||
}; | ||
} | ||
} | ||
if (r === undefined) return acc; | ||
return { ...acc, | ||
[key]: r | ||
if (value === undefined) return { | ||
_argv, | ||
...acc | ||
}; | ||
}, {}); | ||
const r = { | ||
_argv, | ||
...acc, | ||
[key]: value | ||
}; | ||
return r; | ||
}, { | ||
_argv: argv | ||
}); | ||
return res; | ||
@@ -102,0 +148,0 @@ } |
@@ -0,1 +1,10 @@ | ||
## 24 June 2018 | ||
### 1.2.0 | ||
- [feature] `_argv` property to contain all unmatched arguments. | ||
- [doc] Use `documentary` for documentation. | ||
- [code] Faster processing because of returning early when `argv` was exhausted. | ||
- [package] Remove `eslint` - rely on global. | ||
## 15 June 2018 | ||
@@ -2,0 +11,0 @@ |
@@ -17,2 +17,2 @@ import argufy from '../src' | ||
const res = argufy(config, process.argv) | ||
console.log(res) | ||
console.log(JSON.stringify(res, null, 2)) |
{ | ||
"name": "argufy", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Parse command line arguments to Node.js CLI programs.", | ||
@@ -13,2 +13,3 @@ "main": "build", | ||
"lint": "eslint .", | ||
"doc": "NODE_DEBUG=doc doc README-source.md -o README.md", | ||
"e": "node example", | ||
@@ -32,10 +33,9 @@ "example/": "yarn e example/example.js", | ||
"devDependencies": { | ||
"@babel/cli": "7.0.0-beta.49", | ||
"@babel/core": "7.0.0-beta.49", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.49", | ||
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.49", | ||
"@babel/register": "7.0.0-beta.49", | ||
"@babel/cli": "7.0.0-beta.51", | ||
"@babel/core": "7.0.0-beta.51", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.51", | ||
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.51", | ||
"@babel/register": "7.0.0-beta.51", | ||
"babel-plugin-transform-rename-import": "2.2.0", | ||
"eslint": "4.19.1", | ||
"eslint-config-artdeco": "1.0.0", | ||
"documentary": "1.6.1", | ||
"yarn-s": "1.1.0", | ||
@@ -42,0 +42,0 @@ "zoroaster": "2.1.0" |
# argufy | ||
[![npm version](https://badge.fury.io/js/argufy.svg)](https://badge.fury.io/js/argufy) | ||
[![npm version](https://badge.fury.io/js/argufy.svg)](https://npmjs.org/package/argufy) | ||
@@ -11,2 +11,7 @@ ```sh | ||
- [API](#api) | ||
* [`argufy(config: <string, ConfigItem>, argv?: string[]): object`](#argufyconfig-string-configitemargv-string-object) | ||
* [`_argv property`](#_argv-property) | ||
* [`ConfigItem` Type](#configitem-type) | ||
## API | ||
@@ -20,7 +25,7 @@ | ||
### `argufy(config, argv): object ` | ||
### `argufy(`<br/> `config: <string, ConfigItem>,`<br/> `argv?: string[],`<br/>`): object` | ||
The flags from the arguments will be extracted according to the configuration object and the arguments array. If arguments array is not passed, `process.argv` is used to find arguments. | ||
```js | ||
```javascript | ||
import argufy from 'argufy' | ||
@@ -42,24 +47,34 @@ | ||
const res = argufy(config, process.argv) | ||
console.log(res) | ||
console.log(JSON.stringify(res, null, 2)) | ||
``` | ||
```sh | ||
node example.js --title "Hello World" -w 10 -l -app Argufy | ||
node example.js --title Hello_World -w 10 -l -app Argufy | ||
# or | ||
node example.js "Hello World" -w 10 -l -app Argufy | ||
node example.js Hello_World -w 10 -l -app Argufy | ||
# no support "for arguments with space" | ||
``` | ||
```js | ||
{ title: 'Hello World', list: true, app: 'Argufy', wait: 10 } | ||
```json | ||
{ | ||
"_argv": [], | ||
"title": "Hello_World", | ||
"list": true, | ||
"app": "Argufy", | ||
"wait": 10 | ||
} | ||
``` | ||
The special <a name="_argv-property">`_argv property`</a> will be assigned to contain all unmatched arguments. For example, it can be used to pass any additional parameters through to other program. | ||
### `ConfigItem` Type | ||
The configuration for each flag can either be a shorthand string, or an object. If it is an object, it can include the following parameters: | ||
| property | type | description | example | | ||
|----------|---------|--------------------------------------------------------------------------------------------------------------------------------|-----------------------------| | ||
| short | string | A short version of the argument. | `program -t title` | | ||
| boolean | boolean | Whether to parse as a boolean, does not require a value. | `program -d` | | ||
| number | boolean | Parse argument as a number. | `program -n 10` | | ||
| command | boolean | Can this argument be a command, i.e., be the first argument without having to follow a flag. Sets the argument to be a string. | `program command` | | ||
| multiple | boolean | If `command` is true, should multiple words be parsed as an array of commands. Sets the argument to be an array of strings. | `program command1 command2` | | ||
| Property | Type | Description | Example | | ||
| -------- | ---- | ----------- | ------- | | ||
| `short` | string | A short version of the argument. | `program -t title` | | ||
| `boolean` | boolean | Parse argument as a number. | `program -n 10` | | ||
| `command` | boolean | Can this argument be a command, i.e., be the first argument without having to follow a flag. Sets the argument to be a string. | `program command` | | ||
| `multiple` | boolean | If `command` is true, should multiple words be parsed as an array of commands. Sets the argument to be an array of strings. | `program command1 command2` | | ||
@@ -66,0 +81,0 @@ --- |
@@ -0,14 +1,39 @@ | ||
/** | ||
* | ||
* @param {string[]} argv | ||
* @param {*} long | ||
* @param {*} short | ||
* @param {*} bool | ||
* @param {*} number | ||
*/ | ||
const find = (argv, long, short, bool, number) => { | ||
const re = new RegExp(`^-(${short}|-${long})`) | ||
const found = argv.find(a => re.test(a)) | ||
if (!found) return | ||
if (bool) return true | ||
const i = argv.indexOf(found) | ||
const i = argv.findIndex(a => re.test(a)) | ||
if (i == -1) return { argv } | ||
if (bool) { | ||
return { | ||
value: true, | ||
argv: [ | ||
...argv.slice(0, i), | ||
...argv.slice(i + 1), | ||
], | ||
} | ||
} | ||
const j = i + 1 | ||
const value = argv[j] | ||
if (!value) return | ||
let value = argv[j] | ||
if (!value || (typeof value == 'string' && value.startsWith('--'))) return { argv } | ||
if (number) { | ||
return parseInt(value, 10) | ||
value = parseInt(value, 10) | ||
} | ||
return value | ||
return { | ||
value, | ||
argv: [ | ||
...argv.slice(0, i), | ||
...argv.slice(j + 1), | ||
], | ||
} | ||
} | ||
@@ -51,10 +76,12 @@ | ||
export default function argufy(config = {}, args = process.argv) { | ||
const [ ,, ...argv ] = args | ||
let [, , ...argv] = args | ||
/** @type {string} */ | ||
const titles = findTitles(argv) | ||
const res = Object.keys(config).reduce((acc, key) => { | ||
argv = argv.slice(titles.length) | ||
const res = Object.keys(config).reduce(({ _argv, ...acc }, key) => { | ||
if (_argv.length == 0) return { _argv, ...acc } | ||
const val = config[key] | ||
let r | ||
let value | ||
if (typeof val == 'string') { | ||
r = find(args, key, val) | ||
({ value, argv: _argv } = find(_argv, key, val)) | ||
} else { | ||
@@ -64,15 +91,18 @@ try { | ||
if (command && multiple && titles.length) { | ||
r = titles | ||
} else if (command && titles.length){ | ||
r = titles[0] | ||
value = titles | ||
} else if (command && titles.length) { | ||
value = titles[0] | ||
} else { | ||
r = find(args, key, short, boolean, number) | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return acc | ||
return { _argv, ...acc } | ||
} | ||
} | ||
if (r === undefined) return acc | ||
return { ...acc, [key]: r } | ||
}, {}) | ||
if (value === undefined) return { _argv, ...acc } | ||
const r = { _argv, ...acc, [key]: value } | ||
return r | ||
}, { | ||
_argv: argv, | ||
}) | ||
return res | ||
@@ -79,0 +109,0 @@ } |
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
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
28265
9
17
491
83