0.0.1 • Published 5 years ago

@zakkudo/argument-parser v0.0.1

Weekly downloads
1
License
BSD-3-Clause
Repository
github
Last release
5 years ago

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

ParamTypeDescription
optionsOptionsThe configuration options for how parsing is done. returns {module:@zakkudo/argument-parser~ArgumentParser~ParseFunction} 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
ParamTypeDescription
argvArrayThe arguments you want to parse

ArgumentParser~Schema : Object

The schema configuration for the paramters of the program

Kind: inner typedef of ArgumentParser
Properties

NameTypeDescription
typeTypeThe type of parameter. One of string, interger, float, or list
typeNameStringThe type name used for display. An example would be a glob, filename, or other more concrete concept.
descriptionStringThe description of the
longStringThe long form of the switch or nothing
shortStringThe 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

NameTypeDescription
nameStringThe name of the executable this library is being used in;
versionStringA version string that will be shown with the --version switch;
descriptionStringA blurb of text explaining the how's and why's of the program.
schemaSchemaThe configuration
leftoverStringThe 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

NameTypeDefaultDescription
INTEGERStringintegerUsed for arguments that should be parsed with parseInt.
FLOATStringfloatUsed for arguments that should be parsed with parseFloat.
STRINGStringstringUsed for arguments that should be used as raw string.
BOOLEANStringbooleanUsed for arguments that should be assumed a true boolean when the flag exists.
LISTStringlistUsed for arguments that should be split into an array, using ',' as the delimiter.