/ moubarak.eu / node_modules / arg /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[TXT]LICENSE.md2 years ago1.0K 
[TXT]README.md2 years ago3.9K9b1f1b9 rm old utils [كارل مبارك]
[   ]index.js2 years ago2.2K8b8b68d rm everythin MOTD related [كارل مبارك]
[   ]package.json2 years ago444 2324c9f added npm start script [كارل مبارك]
[   ]test.js2 years ago5.8K 
[   ]yarn.lock2 years ago114K 
README.md

Arg

arg is yet another command line option parser.

Installation

Use Yarn or NPM to install.

$ yarn add arg

or

$ npm install arg

Usage

arg() takes either 1 or 2 arguments:

  1. Command line specification object (see below)
  2. Parse options (Optional, defaults to {permissive: false, argv: process.argv.slice(2)})

It returns an object with any values present on the command-line (missing options are thus missing from the resulting object). Arg 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 arg = require('arg');

// `argument_array` is an optional parameter
const args = arg(spec, options = {permissive: false, argv: process.argv.slice(2)}]);

For example:

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

const args = arg({
    // 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=&gt;value pair is either a type (function or [function]) or a string (indicating an alias).

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.

Options

If a second parameter is specified and is an object, it specifies parsing options to modify the behavior of arg().

argv

If you have already sliced or generated a number of raw arguments to be parsed (as opposed to letting arg slice them from process.argv) you may specify them in the argv option.

For example:

const args = arg(
    {
        '--foo': String
    }, {
        argv: ['hello', '--foo', 'world']
    }
);

results in:

const args = {
    _: ['hello'],
    '--foo': 'world'
};

permissive

When permissive set to true, arg will push any unknown arguments onto the "extra" argument array (result._) instead of throwing an error about an unknown flag.

For example:

const arg = require('arg');

const argv = ['--foo', 'hello', '--qux', 'qix', '--bar', '12345', 'hello again'];

const args = arg(
    {
        '--foo': String,
        '--bar': Number
    }, {
        argv,
        permissive: true
    }
);

results in:

const args = {
    _:          ['--qux', 'qix', 'hello again'],
    '--foo':    'hello',
    '--bar':    12345
}

License

Copyright &copy; 2017-2018 by ZEIT, Inc. Released under the MIT License.

Apache/2.4.38 (Debian) Server at www.karls.computer Port 80