
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
simple-argparse
Advanced tools
Simple Argument parser for Command-line Applications
⇒ npm install simple-argparse
sample.js:
require("simple-argparse")
.description("Application Description")
.version("0.3.0")
.option("s", "start", "starts application", startFunc)
.epilog("See License at http://opensource.org/licenses/MIT")
.parse();
function startFunc(host, port) {
app.listen(port, host);
}
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
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 = require("simple-argparse").Parser;
var myParser = new Parser();
While instantiating a parser, an output function may be registered with
the parser other than the default console.log:
var myOtherParser = new Parser(function(output) {
socket.emit("commandComplete", output);
});
A Parser has these methods:
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:
description: help information regarding this command
optionFunction:(Optional) See Parsing below for more information.
Parser#defaultOption([optionFunction:Function])
Parser#prerun([hookFunction:Function])
this context.Parser#epilog(epilog:String)
epilog: a string that will appear at the bottom of the help information
Parser#parse([arguments:String])
arguments:(Optional)
process.argv will be used insteadParser#showHelp()
Parser#showVersion()
.version().All arguments parsed by .parse() are processed using
minimist, 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:
require("simple-argparse")
.version("0.0.0")
.option("test", "run tests", function(suite) {
console.log("this._option === %s", this._option);
console.log("this.verbose === %s", this.verbose);
console.log("suite === %s", suite);
})
.defaultOption(function() {
console.log("this._option === %s", this._option);
console.log("this.verbose === %s", this.verbose);
})
.parse();
Now running the above script from a terminal:
# default command
⇒ node parse.js
this._option === default
this.verbose === undefined
# default command
⇒ node parse.js --verbose
this._option === default
this.verbose === true
# test command
⇒ node parse.js test
this._option === test
this.verbose === undefined
suite === undefined
# test command
⇒ node parse.js test someSuite
this._option === test
this.verbose === undefined
suite === someSuite
# test command
⇒ node parse.js test someSuite --verbose
this._option === test
this.verbose === true
suite === someSuite
See minimist 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.
The MIT License (MIT)
Copyright (c) 2014-2015 Forfuture LLC we@forfuture.co.ke
0.3.0 - 2015-08-05
Added:
FAQs
Simple Argument parser for Command-line Applications
We found that simple-argparse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.