Comparing version 1.2.1 to 1.3.0
@@ -1,8 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = argufy; | ||
/** | ||
@@ -17,7 +10,5 @@ * | ||
const find = (argv, long, short, bool, number) => { | ||
const re = new RegExp(`^-(${short}|-${long})`); | ||
const i = argv.findIndex(a => re.test(a)); | ||
if (i == -1) return { | ||
argv | ||
}; | ||
const re = new RegExp(`^-(${short}|-${long})`) | ||
const i = argv.findIndex(a => re.test(a)) | ||
if (i == -1) return { argv } | ||
@@ -27,21 +18,26 @@ if (bool) { | ||
value: true, | ||
argv: [...argv.slice(0, i), ...argv.slice(i + 1)] | ||
}; | ||
argv: [ | ||
...argv.slice(0, i), | ||
...argv.slice(i + 1), | ||
], | ||
} | ||
} | ||
const j = i + 1; | ||
let value = argv[j]; | ||
if (!value || typeof value == 'string' && value.startsWith('--')) return { | ||
argv | ||
}; | ||
const j = i + 1 | ||
let value = argv[j] | ||
if (!value || (typeof value == 'string' && value.startsWith('--'))) return { argv } | ||
if (number) { | ||
value = parseInt(value, 10); | ||
value = parseInt(value, 10) | ||
} | ||
return { | ||
value, | ||
argv: [...argv.slice(0, i), ...argv.slice(j + 1)] | ||
}; | ||
}; | ||
argv: [ | ||
...argv.slice(0, i), | ||
...argv.slice(j + 1), | ||
], | ||
} | ||
} | ||
/** | ||
@@ -81,117 +77,61 @@ * Parse the config and extract arguments from the `process.argv` array. | ||
*/ | ||
function argufy(config = {}, args = process.argv) { | ||
let [,, ...argv] = args; | ||
function argufy(config = {}, args = process.argv) { | ||
let [, , ...argv] = args | ||
/** @type {string} */ | ||
const titles = findTitles(argv); | ||
argv = argv.slice(titles.length); | ||
const c = {}; | ||
const conf = { ...config | ||
}; | ||
if (titles.length) { | ||
const commands = Object.keys(conf).filter(key => { | ||
try { | ||
const { | ||
command | ||
} = conf[key]; | ||
return command; | ||
} catch (er) { | ||
return; | ||
} | ||
}); | ||
commands.forEach(key => { | ||
const { | ||
multiple | ||
} = conf[key]; | ||
const cc = multiple ? titles : titles[0]; | ||
c[key] = cc; | ||
delete conf[key]; | ||
}); | ||
} | ||
const res = Object.keys(conf).reduce(({ | ||
_argv, | ||
...acc | ||
}, key) => { | ||
if (_argv.length == 0) return { | ||
_argv, | ||
...acc | ||
}; | ||
const val = conf[key]; | ||
let value; | ||
const titles = findTitles(argv) | ||
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 value | ||
if (typeof val == 'string') { | ||
({ | ||
value, | ||
argv: _argv | ||
} = find(_argv, key, val)); | ||
({ value, argv: _argv } = find(_argv, key, val)) | ||
} else { | ||
try { | ||
const { | ||
short, | ||
boolean, | ||
number, | ||
command, | ||
multiple | ||
} = val; | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
value = titles; | ||
value = titles | ||
} else if (command && titles.length) { | ||
value = titles[0]; | ||
value = titles[0] | ||
} else { | ||
({ | ||
value, | ||
argv: _argv | ||
} = find(_argv, key, short, boolean, number)); | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return { | ||
_argv, | ||
...acc | ||
}; | ||
return { _argv, ...acc } | ||
} | ||
} | ||
if (value === undefined) return { | ||
_argv, | ||
...acc | ||
}; | ||
const r = { | ||
_argv, | ||
...acc, | ||
[key]: value | ||
}; | ||
return r; | ||
if (value === undefined) return { _argv, ...acc } | ||
const r = { _argv, ...acc, [key]: value } | ||
return r | ||
}, { | ||
_argv: argv, | ||
...c | ||
}); | ||
return res; | ||
}) | ||
return res | ||
} | ||
const findTitles = argv => { | ||
const titles = []; | ||
const titles = [] | ||
for (let i = 0; i < argv.length; i++) { | ||
const a = argv[i]; | ||
if (a.startsWith('-')) break; | ||
titles.push(a); | ||
const a = argv[i] | ||
if (a.startsWith('-')) break | ||
titles.push(a) | ||
} | ||
return titles | ||
} | ||
return titles; | ||
}; | ||
/* documentary types/index.xml */ | ||
/** | ||
* @typedef {Object} Flag | ||
* @property {string} [short] Shorthand for this argument, usually one letter. | ||
* @property {boolean} [boolean] Does not have to be followed by a value, true if given. | ||
* @property {boolean} [number] Parse as a number. | ||
* @property {boolean} [command] Whether this argument can be specified as the first argument without the dash, such as `example run`: run is the command. | ||
* @property {boolean} [multiple] Extract multiple commands as an array when `command` is set to true. | ||
* | ||
* @typedef {Object} Flag The flag passed to the program. | ||
* @prop {string} [short] Shorthand for this argument, usually one letter. | ||
* @prop {boolean} [boolean=false] Whether the flag is a boolean and does not require a value. Default `false`. | ||
* @prop {boolean} [number=false] Specifies whether the flag should be parsed as a number. Default `false`. | ||
* @prop {boolean} [command=false] If set to true, the value is read from the first argument passed to the CLI command (e.g., `$ cli command`). Default `false`. | ||
* @prop {boolean} [multiple=false] When using the `command` property, will parse the commands as an array. Default `false`. | ||
*/ | ||
/** | ||
* @typedef {Object.<string, string|Flag>} Config | ||
*/ | ||
//# sourceMappingURL=index.js.map | ||
module.exports = argufy |
@@ -1,6 +0,7 @@ | ||
## 25 June 2018 | ||
## 14 January 2019 | ||
### 1.2.1 | ||
### 1.3.0 | ||
- [fix] Fix a bug when a single command was not detected. | ||
- [package] Build with [ÀLaMode](https://alamode.cc), upgrade structure. | ||
- [package] Add `module` field. | ||
@@ -7,0 +8,0 @@ ## 24 June 2018 |
{ | ||
"name": "argufy", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Parse command line arguments to Node.js CLI programs.", | ||
"main": "build", | ||
"module": "src/index.js", | ||
"scripts": { | ||
"t": "zoroaster -b", | ||
"t": "zoroaster -a", | ||
"test": "yarn t test/spec", | ||
"test-build": "BABEL_ENV=test-build yarn t test/spec", | ||
"test-all": "yarn-s test test-build", | ||
"test-watch": "yarn test --watch", | ||
"spec": "yarn t test/spec", | ||
"mask": "yarn t test/mask", | ||
"test-build": "ALAMODE_ENV=test-build yarn t test/spec", | ||
"lint": "eslint .", | ||
"doc": "NODE_DEBUG=doc doc README-source.md -o README.md", | ||
"doc": "NODE_DEBUG=doc doc documentary -o README.md", | ||
"d": "NODE_DEBUG=doc doc src/index.js -g", | ||
"e": "node example", | ||
"b": "b --source-maps", | ||
"example/": "yarn e example/example.js" | ||
"example/": "yarn e example/example.js", | ||
"build": "yarn-s d b doc", | ||
"b": "alamode src -o build -s" | ||
}, | ||
"files": [ | ||
"build" | ||
"build", | ||
"src" | ||
], | ||
@@ -35,6 +39,7 @@ "repository": { | ||
"devDependencies": { | ||
"documentary": "1.6.1", | ||
"alamode": "1.6.1", | ||
"documentary": "1.20.1", | ||
"yarn-s": "1.1.0", | ||
"zoroaster": "2.1.0" | ||
"zoroaster": "3.6.6" | ||
} | ||
} |
@@ -15,5 +15,13 @@ # argufy | ||
* [`ConfigItem` Type](#configitem-type) | ||
* [`Flag`](#type-flag) | ||
- [Copyright](#copyright) | ||
## API | ||
The package is available by importing its default function: | ||
```js | ||
import argufy from 'argufy' | ||
``` | ||
The package assumes that the arguments begin from the 3rd position, i.e., | ||
@@ -72,13 +80,16 @@ | ||
| 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` | | ||
__<a name="type-flag">`Flag`</a>__: The flag passed to the program. | ||
--- | ||
| Name | Type | Description | Default | | ||
| -------- | --------- | ------------------------------------------------------------------------------------------------------------ | ------- | | ||
| short | _string_ | Shorthand for this argument, usually one letter. | - | | ||
| boolean | _boolean_ | Whether the flag is a boolean and does not require a value. | `false` | | ||
| number | _boolean_ | Specifies whether the flag should be parsed as a number. | `false` | | ||
| command | _boolean_ | If set to true, the value is read from the first argument passed to the CLI command (e.g., `$ cli command`). | `false` | | ||
| multiple | _boolean_ | When using the `command` property, will parse the commands as an array. | `false` | | ||
(c) [artdecocode][1] 2018 | ||
## Copyright | ||
[1]: https://artdeco.bz | ||
(c) [Art Deco][1] 2019 | ||
[1]: https://artd.eco |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
246
94
14289
4
6