simple-argparse
Simple Argument parser for Command-line Applications
installation
⇒ npm install simple-argparse
basic usage
sample.js:
description"Application Description" version"0.3.0" ; { app;}
sample output:
⇒ node Sample.js Application Description H, help show this help information s, start starts application V, version show version information See License at http://opensource.org/licenses/MIT
API
The module exports a new Parser
instance, that can be used immediately. If you wish to create more parsers, you instead use the Parser
constructor exported at .Parser
:
var Parser = Parser;var myParser = ;
While instantiating a parser, an output function may be registered with
the parser other than the default console.log
:
var myOtherParser = { socket;};
A Parser
has these methods:
- Parser#description([name:String,] description:String)
-
name:(Optional) refers to the name of your Application
-
description: provides a description of your Application
-
Parser#version(version:String)
-
version: provides version information of your Application. Defaults to
"0.0.0"
-
Parser#option([short:String ,] command:String, description:String [, optionFunction:Function])
-
short: (Optional) short string, preferably one or two letter string that can be used in place of the command.
-
command:
- a string that will be typed by user to fire the command
- any spaces will be replaced by hyphens
-
description: help information regarding this command
-
optionFunction:(Optional) See Parsing below for more information.
-
Parser#defaultOption([optionFunction:Function])
- optionFunction: (Optional) default function to run rather than show help information. See Parsing below for more information.
-
Parser#prerun([hookFunction:Function])
- hookFunction: (Optional) function to run before any of the option functions. This function can manipulate the arguments passed to the option functions by using the
this
context.
- hookFunction: (Optional) function to run before any of the option functions. This function can manipulate the arguments passed to the option functions by using the
-
Parser#epilog(epilog:String)
-
epilog: a string that will appear at the bottom of the help information
-
Parser#parse([arguments:String|Array])
-
arguments:(Optional)
- a string or array representing options, for example,
"name --key=value"
,["name", "--key=value"]
- if left out,
process.argv
will be used instead
- a string or array representing options, for example,
-
Parser#showHelp()
- shows the help information
- is done by passing all the necessary data as string to the registered output function
-
Parser#showVersion()
- similar to Parser#showHelp() but only supplies version information, registered with
.version()
.
- similar to Parser#showHelp() but only supplies version information, registered with
Parsing
All arguments parsed by .parse()
are processed using
yargs-parser, and made available to the option functions as
their this
argument.
An option function refers to the function passed to .option
.
Options that are NOT perceived as options/flags by minimist are passed
to the function as arguments
.
The option name, as inputted by the user, is made available to the function at this._option
.
Note that for the default option (.defaultOption(func)
) no arguments
can be passed to the option function. Also this._option
will always equal "default"
.
Consider the following example:
parse.js:
version"0.0.0" ;
Now running the above script from a terminal:
# default command ⇒ node parse.jsthis._option === defaultthis.verbose === undefined # default command ⇒ node parse.js --verbosethis._option === defaultthis.verbose === true # test command ⇒ node parse.js testthis._option === testthis.verbose === undefinedsuite === undefined # test command ⇒ node parse.js test someSuitethis._option === testthis.verbose === undefinedsuite === someSuite # test command ⇒ node parse.js test someSuite --verbosethis._option === testthis.verbose === truesuite === someSuite
See yargs-parser for more information on the parsing.
The option function is optional. If it is left out, the option will be ignored. This may be useful for commands not yet implemented.
license
The MIT License (MIT)
Copyright (c) 2014-2016 Forfuture LLC we@forfuture.co.ke