/argument-parser

Make parsing node command line arguments enjoyable.

Primary LanguageJavaScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

@zakkudo/argument-parser

Make parsing node command line arguments enjoyable.

Build Status Coverage Status Known Vulnerabilities Node License

Why use this?

  • Straight forward configuration
  • Reusability

What does it do?

  • Parses arguments using multiple configuration types

Install

# Install using npm
npm install @zakkudo/argument-parser
# Install using yarn
yarn add @zakkudo/argument-parser

Examples

Basic example

const parse = new ArgumentParser({
    name: 'download-program',
    version: 'v1.3.4',
    description: 'A program for downloading files very fastly.',
    leftover: 'files',
    schema: [{
        long: 'fast',
        short: 'f',
        type: 'boolean',
        description: 'Makes the download go very fast.',
    }, {
        long: 'token',
        short: 't',
        type: 'string',
        description: 'Token used for authentication.',
    }, {
        long: 'muliplier',
        short: 'm',
        type: 'float',
        description: 'How many times faster the download should be.',
    }, {
        long: 'servers',
        type: 'list',
        typeName: 's1,s2,s3',
        description: 'Servers to use for the fast downloading, separated by a comma.',
    }]
});

const parsed = parse(['--fast', '--token', '1234', 'src/**/*.js', ]
// Returns an object with the below:
// {
//     "fast": true,
//     "leftover": [
//         "src/**/*.js",
//     ],
//     "token": "1234",
// }

parse(['--version']
// Exits, printing: "download-program version v1.3.4"

parse(['--help'])
// Exits, printing:
// usage: download-program [--help] [--version] [--fast] [--token=uuid] [--muliplier=float] [--servers=s1,s2,s3] ...files
// A program for downloading files very fastly.
//
// -h/--help            Show this help information.
// -V/--version         Show the program version.
// -f/--fast            Makes the download go very fast.
// -t/--token=uuid      Token used for authentication.
// -m/--muliplier=float How many times faster the download should be.
// --servers=s1,s2,s3   Servers to use for the fast downloading, separated by a comma.

API

@zakkudo/argument-parser~ArgumentParser ⏏

Kind: Exported class

new ArgumentParser(options)

Param Type Description
options Options The configuration options for how parsing is done. returns {module:@zakkudo/argument-parserArgumentParserParseFunction} A function used to parse arguments given the configuration during construction.

ArgumentParser~ParseFunction ⇒ Object

Parse function

Kind: inner typedef of ArgumentParser
Returns: Object - An object for the given schema configuration
Throws:

  • InvalidArgumentError when and argument is malformed
  • InvalidSchemaError when an invalid schema type is used for one of the actions and it's referenced
Param Type Description
argv Array The arguments you want to parse

ArgumentParser~Schema : Object

The schema configuration for the paramters of the program

Kind: inner typedef of ArgumentParser
Properties

Name Type Description
type Type The type of parameter. One of string, interger, float, or list
[typeName] String The type name used for display. An example would be a glob, filename, or other more concrete concept.
description String The description of the
[long] String The long form of the switch or nothing
[short] String The short form of the switch or nothing

ArgumentParser~Options : Object

Argument parser configuration, controling how argumetns are parsed and how they are shown in help documentation. You must have at least a long or short switch name set.

Kind: inner typedef of ArgumentParser
Properties

Name Type Description
name String The name of the executable this library is being used in;
version String A version string that will be shown with the --version switch;
description String A blurb of text explaining the how's and why's of the program.
schema Schema The configuration
[leftover] String The name for the leftover parameters. Without this, leftover parameters will be disallowed.

@zakkudo/Type~Type : enum

Kind: inner enum of @zakkudo/Type
Read only: true
Properties

Name Type Default Description
INTEGER String integer Used for arguments that should be parsed with parseInt.
FLOAT String float Used for arguments that should be parsed with parseFloat.
STRING String string Used for arguments that should be used as raw string.
BOOLEAN String boolean Used for arguments that should be assumed a true boolean when the flag exists.
LIST String list Used for arguments that should be split into an array, using ',' as the delimiter.