yargs-unparser
Advanced tools
Comparing version 1.0.3 to 1.1.0
67
index.js
'use strict'; | ||
const yargs = require('yargs/yargs'); | ||
const flatten = require('flat'); | ||
@@ -45,18 +46,41 @@ const castArray = require('lodash/castArray'); | ||
// ------------------------------------------------------------ | ||
function unparsePositional(argv, options, unparsed) { | ||
const knownPositional = []; | ||
function unparser(argv, options) { | ||
options = Object.assign({ | ||
alias: {}, | ||
}, options); | ||
// Unparse command if set, collecting all known positional arguments | ||
// e.g.: build <first> <second> <rest...> | ||
if (options.command) { | ||
const { 0: cmd, index } = options.command.match(/[^<[]*/); | ||
const { demanded, optional } = yargs() | ||
.getCommandInstance() | ||
.parseCommand(`foo ${options.command.substr(index + cmd.length)}`); | ||
const unparsed = []; | ||
// Push command (can be a deep command) | ||
unparsed.push(...cmd.trim().split(/\s+/)); | ||
// Unparse positional arguments | ||
argv._ && unparsed.push(...argv._); | ||
// Push positional arguments | ||
[...demanded, ...optional].forEach(({ cmd: cmds, variadic }) => { | ||
knownPositional.push(...cmds); | ||
// Unparse option arguments | ||
const cmd = cmds[0]; | ||
const args = (variadic ? argv[cmd] || [] : [argv[cmd]]) | ||
.filter((arg) => arg != null) | ||
.map((arg) => `${arg}`); | ||
unparsed.push(...args); | ||
}); | ||
} | ||
// Unparse unkown positional arguments | ||
argv._ && unparsed.push(...argv._.slice(knownPositional.length)); | ||
return knownPositional; | ||
} | ||
function unparseOptions(argv, options, knownPositional, unparsed) { | ||
const optionsArgv = omitBy(argv, (value, key) => | ||
// Remove positional arguments | ||
knownPositional.includes(key) || | ||
// Remove special _, -- and $0 | ||
key === '_' || key === '--' || key === '$0' || | ||
['_', '--', '$0'].includes(key) || | ||
// Remove aliases | ||
@@ -70,6 +94,29 @@ isAlias(key, options.alias) || | ||
} | ||
} | ||
function unparseEndOfOptions(argv, options, unparsed) { | ||
// Unparse ending (--) arguments if set | ||
argv['--'] && unparsed.push('--', ...argv['--']); | ||
} | ||
// ------------------------------------------------------------ | ||
function unparser(argv, options) { | ||
options = Object.assign({ | ||
alias: {}, | ||
command: null, | ||
}, options); | ||
const unparsed = []; | ||
// Unparse known & unknown positional arguments (foo <first> <second> [rest...]) | ||
// All known positional will be returned so that they are not added as flags | ||
const knownPositional = unparsePositional(argv, options, unparsed); | ||
// Unparse option arguments (--foo hello --bar hi) | ||
unparseOptions(argv, options, knownPositional, unparsed); | ||
// Unparse "end-of-options" arguments (stuff after " -- ") | ||
unparseEndOfOptions(argv, options, unparsed); | ||
return unparsed; | ||
@@ -76,0 +123,0 @@ } |
{ | ||
"name": "yargs-unparser", | ||
"description": "Converts back a yargs argv object to its original array form.", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
@@ -34,3 +34,3 @@ "yargs", | ||
"husky": "^0.14.3", | ||
"jest": "^20.0.4", | ||
"jest": "^21.0.1", | ||
"lint-staged": "^4.0.2", | ||
@@ -41,4 +41,5 @@ "yargs-parser": "^7.0.0" | ||
"flat": "^4.0.0", | ||
"lodash": "^4.17.4" | ||
"lodash": "^4.17.4", | ||
"yargs": "^9.0.1" | ||
} | ||
} |
@@ -32,11 +32,14 @@ # yargs-unparser | ||
```js | ||
const unparse = require('yargs-unparser'); | ||
const parse = require('yargs-parse'); | ||
const parse = require('yargs-parser'); | ||
const unparse = require('yargs-unparse'); | ||
unparse(parse(['node', 'cli.js', '--no-boolean', '--number', '4', '--string', 'foo'], { | ||
number: 'number', | ||
string: 'string', | ||
boolean: 'boolean', | ||
})); | ||
// ['node', 'cli.js', '--no-boolean', '--number', '4', '--string', 'foo']); | ||
const argv = parse(['--no-boolean', '--number', '4', '--string', 'foo'], { | ||
boolean: ['boolean'], | ||
number: ['number'], | ||
string: ['string'], | ||
}); | ||
// { boolean: false, number: 4, '--string', 'foo', _: [] } | ||
const unparsedArgv = unparse(argv); | ||
// ['--no-boolean', '--number', '4', '--string', 'foo']; | ||
``` | ||
@@ -46,8 +49,32 @@ | ||
- `aliases`: The [aliases](https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts) so that duplicate options aren't generated | ||
- `alias`: The [aliases](https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts) so that duplicate options aren't generated | ||
- `command`: The [command](https://github.com/yargs/yargs/blob/master/docs/advanced.md#commands) first argument so that command names and positional arguments are handled correctly | ||
### Example with `command` options | ||
```js | ||
const yargs = require('yargs'); | ||
const unparse = require('yargs-unparse'); | ||
const argv = yargs | ||
.command('my-command <positional>', 'My awesome command', (yargs) => | ||
yargs | ||
.option('boolean', { type: 'boolean' }) | ||
.option('number', { type: 'number' }) | ||
.option('string', { type: 'string' }) | ||
) | ||
.parse(['my-command', 'hello', '--no-boolean', '--number', '4', '--string', 'foo']); | ||
// { positional: 'hello', boolean: false, number: 4, '--string', 'foo', _: ['my-command'] } | ||
const unparsedArgv = unparse(argv, { | ||
command: 'my-command <positional>', | ||
}); | ||
// ['my-command', 'hello', '--no-boolean', '--number', '4', '--string', 'foo']; | ||
``` | ||
**NOTE**: The returned array can be parsed again by `yargs-parser` using the default configuration. If you used custom configuration that you want `yargs-unparser` to be aware, please fill an [issue](https://github.com/moxystudio/yargs-unparser/issues). | ||
**NOTE**: If you `coerce` in weird ways, things might not work correctly. | ||
## Tests | ||
@@ -54,0 +81,0 @@ |
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
9629
98
87
3
+ Addedyargs@^9.0.1
+ Addedansi-regex@2.1.13.0.1(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedcliui@3.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedcross-spawn@5.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedexeca@0.7.0(transitive)
+ Addedfind-up@2.1.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedinvert-kv@1.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-fullwidth-code-point@1.0.02.0.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlcid@1.0.0(transitive)
+ Addedload-json-file@2.0.0(transitive)
+ Addedlocate-path@2.0.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addedmem@1.1.0(transitive)
+ Addedmimic-fn@1.2.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedos-locale@2.1.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedp-limit@1.3.0(transitive)
+ Addedp-locate@2.0.0(transitive)
+ Addedp-try@1.0.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@2.0.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedread-pkg@2.0.0(transitive)
+ Addedread-pkg-up@2.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstring-width@1.0.22.1.1(transitive)
+ Addedstrip-ansi@3.0.14.0.0(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@2.1.0(transitive)
+ Addedy18n@3.2.2(transitive)
+ Addedyallist@2.1.2(transitive)
+ Addedyargs@9.0.1(transitive)
+ Addedyargs-parser@7.0.0(transitive)