Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yargs-unparser

Package Overview
Dependencies
Maintainers
16
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yargs-unparser - npm Package Compare versions

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 @@ }

7

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc