yargs-unparser
    Overview
    Documentation
    Tutorials
    Insights
    Code
    Dependencies
    Contributors
    Jobs

yargs-unparser

Converts back a yargs argv object to its original array form

1.5.0  •  Updated 7 months ago  •  by yargs  •  MIT License

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge

Converts back a yargs argv object to its original array form.

Probably the unparser word doesn’t even exist, but it sounds nice and goes well with yargs-parser.

The code originally lived in MOXY’s GitHub but was later moved here for discoverability.

Installation

$ npm install yargs-unparser

Usage

const parse = require('yargs-parser');
const unparse = require('yargs-unparser');

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'];

The second argument of unparse accepts an options object:

  • alias: The aliases so that duplicate options aren’t generated
  • default: The default values so that the options with default values are omitted
  • command: The command first argument so that command names and positional arguments are handled correctly

Example with command options

const yargs = require('yargs');
const unparse = require('yargs-unparser');

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'];

Caveats

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.

If you coerce in weird ways, things might not work correctly.

Tests

$ npm test
$ npm test -- --watch during development

License

MIT License

Popularity

Weekly Downloads
725.4K
Stars
11

Maintenance

Development

Last ver 1 year ago
Created 3 years ago
Last commit 7 months ago
17 days between commits

Technology

Node version: 10.12.0
11.3K unpacked

Compliance

MIT License
OSI Approved
0 vulnerabilities

Contributors

11 contributors
André Cruz
Maintainer, 44 commits, 11 merges, 1 PRs
Works at moxystudio
greenkeeper[bot]
22 commits
Benjamin E. Coe
Maintainer, 5 commits, 13 merges, 2 PRs
Works at Google
Darth Vanger
2 commits, 2 PRs
Works at DataArt
Andrew Bradley
1 commits, 1 PRs
Ram Dobson
1 commits, 1 PRs

Tags

yargs
unparse
expand
inverse
argv
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookTwitterLinkedIn
© 2020 Devstore, Inc.