This package has been deprecated

Author message:

The `zarg` package is completely deprecated and will not receive any further updates or releases. Please use `arg` (without the `z`) instead.

zarg

0.2.0 • Public • Published

Zarg

Zarg is yet another command line option parser.

Installation

Use Yarn or NPM to install.

$ yarn add zarg

or

$ npm install zarg

Usage

zarg() takes 1 or 2 arguments:

  1. An array of CLI arguments (Optional, defaults to process.argv.slice(2))
  2. Options argument (see below)

It returns an object with any values present on the command-line (missing options are thus missing from the resulting object). Zarg performs no validation/requirement checking - we leave that up to the application.

All parameters that aren't consumed by options (commonly referred to as "extra" parameters) are added to result._, which is always an array (even if no extra parameters are passed, in which case an empty array is returned).

const zarg = require('zarg');

const args = zarg([argument_array,] options);

For example:

$ node ./hello.js --port=1234 -n 'My name' foo bar --tag qux --tag=qix -- --foobar
// hello.js
const zarg = require('zarg');

const args = zarg({
	// Types
	'--help':    Boolean,
	'--version': Boolean,
	'--port':    Number,      // --port <number> or --port=<number>
	'--name':    String,      // --name <string> or --name=<string>
	'--tag':     [String],    // --tag <string> or --tag=<string>

	// Aliases
	'-v':        '--version',
	'-n':        '--name',    // -n <string>; result is stored in --name
	'--label':   '--name'     // --label <string> or --label=<string>;
	                          //     result is stored in --name
});

console.log(args);
/*
{
	_: ["foo", "bar", "--foobar"],
	'--port': 1234,
	'--name': "My name",
	'--tag': ["qux", "qix"]
}
*/

The values for each key=>value pair is either a type (function or [function]) or a string (indicating an alias).

  • In the case of a function, the string value of the argument's value is passed to it, and the return value is used as the ultimate value.

  • In the case of an array, the only element must be a type function. Array types indicate that the argument may be passed multiple times, and as such the resulting value in the returned object is an array with all of the values that were passed using the specified flag.

  • In the case of a string, an alias is established. If a flag is passed that matches the key, then the value is substituted in its place.

Type functions are passed three arguments:

  1. The parameter value (always a string)
  2. The parameter name (e.g. --label)
  3. The previous value for the destination (useful for reduce-like operatons or for supporting -v multiple times, etc.)

This means the built-in String, Number, and Boolean type constructors "just work" as type functions.

License

Copyright © 2017 by ZEIT, Inc. Released under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i zarg

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • matheuss
  • leo
  • rauchg
  • tootallnate
  • qix