argument-parser-extended

0.5.1 • Public • Published

argument-parser-extended

NPM

Build Status Dependencies Coverage Status npm version

Requirements & Installation

Just run npm install argument-parser-extended

Documentation

ArgumentParser(name, options)

  • name - Name of the program
  • options - Options to be used for validation, see below.

All keys in options are automatically converted from lower camel-case to kebab-case.

{
    flagName: {
        enum: ['list', 'of', 'values', 'accepted'], //conflicts with all other validation parameters.
        type: 'boolean|number|string|array|integer|file'//defaults to boolean, if array all validators will be applied to each element.
        default: 'myDefaultValueIfSwitchIsNotSet',
        required: true|false, //will error if a set to true and a default exists
        min: minValue, //only works for integer and number
        max: maxValue, //see above
        regex: /some regex/, //only works for type string, will cause the parser to throw an error if the passed string does not match
        subType: 'boolean|number|string|integer|file', //The type to be validated if type is an array,
        file: { //only works when type = file
            json: true|false, //is the content json?
            stream: true|false, //return a stream rather than reading the whole file,
            encoding: string, //default is utf8
        },
        short: sting, //must be length of 1, the short flag to alias the flag with
        validator: function //will be called with the preprocessed value parsed from the flag, this function can override the return value by simply returning != undefined, otherwise it should just throw an error.
    }
}

Object ArgumentParser.parse(str)

Parses the given string and returns a object representing the parsed data, throws if anything failed.

All "additional" data that was not associated with a flag will be available in the append field.

Object ArgumentParser.run()

Parses the command line directly, otherwise behaves just like parse.

string ArgumentParser.getHelpString()

Returns nicely formatted usage info.

Usage examples

var ArgumentParser = require('argument-parser-extended');
 
var argParser = new ArgumentParser('example', {
    myParam: {
        type: 'string',
        default: 'nothing'
    },
    myConfig: {
        type: 'file',
        file: {
            json: true
        }
    }
});
 
try {
    var conf = argParser.run();
    console.log(conf.myParam, conf.myConfig);
} catch(e) {
    console.error(argParser.getHelpText());
}
node filename.js --my-param hello --my-config path/to/json.json

Readme

Keywords

Package Sidebar

Install

npm i argument-parser-extended

Weekly Downloads

2

Version

0.5.1

License

BSD-2-Clause

Last publish

Collaborators

  • simonschick