Comparing version 1.0.2 to 1.1.0
@@ -44,3 +44,2 @@ "use strict"; | ||
* | ||
* ```js | ||
* const config = { | ||
@@ -60,3 +59,2 @@ * title: 't', // program -t Title | ||
* } | ||
* ``` | ||
* | ||
@@ -68,8 +66,7 @@ * @param {string[]} args value of process.argv. It is assumed that user arguments start from the 3rd position. | ||
const argufy = (config = {}, args = []) => { | ||
const argufy = (config = {}, args = process.argv) => { | ||
const [,, ...argv] = args; | ||
/** @type {string} */ | ||
const [first] = argv; | ||
const title = first && first.startsWith('-') ? undefined : first; | ||
const titles = findTitles(argv); | ||
const res = Object.keys(config).reduce((acc, key) => { | ||
@@ -87,5 +84,13 @@ const val = config[key]; | ||
number, | ||
command | ||
command, | ||
multiple | ||
} = val; | ||
r = command && title ? title : find(args, key, short, boolean, number); | ||
if (command && multiple && titles.length) { | ||
r = titles; | ||
} else if (command && titles.length) { | ||
r = titles[0]; | ||
} else { | ||
r = find(args, key, short, boolean, number); | ||
} | ||
} catch (err) { | ||
@@ -100,9 +105,20 @@ return acc; | ||
}; | ||
}, {}); // /** @type {Object} */ | ||
}, {}); | ||
return res; | ||
}; | ||
const findTitles = argv => { | ||
const titles = []; | ||
for (let i = 0; i < argv.length; i++) { | ||
const a = argv[i]; | ||
if (a.startsWith('-')) break; | ||
titles.push(a); | ||
} | ||
return titles; | ||
}; | ||
var _default = argufy; | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,9 @@ | ||
## 15 June 2018 | ||
### 1.1.0 | ||
- [doc] describe configuration object | ||
- [feature] set default argv to `process.argv` | ||
- [feature] parse multiple commands | ||
## 11 June 2018 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "argufy", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Parse command line arguments to Node.js CLI programs.", | ||
@@ -5,0 +5,0 @@ "main": "build", |
@@ -19,5 +19,5 @@ # argufy | ||
### `argufy(config, argv):object ` | ||
### `argufy(config, argv): object ` | ||
The flags from the arguments will be extracted according to the configuration 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. | ||
@@ -54,2 +54,12 @@ ```js | ||
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` | | ||
--- | ||
@@ -56,0 +66,0 @@ |
@@ -38,3 +38,2 @@ /** | ||
* | ||
* ```js | ||
* const config = { | ||
@@ -54,3 +53,2 @@ * title: 't', // program -t Title | ||
* } | ||
* ``` | ||
* | ||
@@ -60,7 +58,6 @@ * @param {string[]} args value of process.argv. It is assumed that user arguments start from the 3rd position. | ||
*/ | ||
const argufy = (config = {}, args = []) => { | ||
const argufy = (config = {}, args = process.argv) => { | ||
const [ ,, ...argv ] = args | ||
/** @type {string} */ | ||
const [first] = argv | ||
const title = first && first.startsWith('-') ? undefined : first | ||
const titles = findTitles(argv) | ||
const res = Object.keys(config).reduce((acc, key) => { | ||
@@ -73,4 +70,10 @@ const val = config[key] | ||
try { | ||
const { short, boolean, number, command } = val | ||
r = command && title ? title : find(args, key, short, boolean, number) | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
r = titles | ||
} else if (command && titles.length){ | ||
r = titles[0] | ||
} else { | ||
r = find(args, key, short, boolean, number) | ||
} | ||
} catch (err) { | ||
@@ -83,6 +86,15 @@ return acc | ||
}, {}) | ||
// /** @type {Object} */ | ||
return res | ||
} | ||
const findTitles = argv => { | ||
const titles = [] | ||
for (let i = 0; i < argv.length; i++) { | ||
const a = argv[i] | ||
if (a.startsWith('-')) break | ||
titles.push(a) | ||
} | ||
return titles | ||
} | ||
export default argufy |
@@ -27,2 +27,17 @@ import { equal, deepEqual } from 'zoroaster/assert' | ||
}, | ||
'extracts commands'({ TITLE, WAIT }) { | ||
const TITLE2 = `${TITLE}2` | ||
const args = ['.nvm/versions/node/v8.10.0/bin/node', 'script', TITLE, TITLE2, '-w', WAIT] | ||
const res = argufy({ | ||
title: { | ||
command: true, | ||
multiple: true, | ||
}, | ||
wait: 'w', | ||
}, args) | ||
deepEqual(res, { | ||
title: [TITLE, TITLE2], | ||
wait: WAIT, | ||
}) | ||
}, | ||
'parses boolean'({ config }) { | ||
@@ -29,0 +44,0 @@ const args = ['.nvm/versions/node/v8.10.0/bin/node', 'script', '--list'] |
Sorry, the diff of this file is not supported yet
21092
366
68