simple-argparse
Advanced tools
Comparing version 0.2.2 to 0.3.0
82
index.js
@@ -81,7 +81,9 @@ /* | ||
this._commands = { }; | ||
this._short = { }; | ||
this._default = this.showHelp.bind(this); | ||
this._hooks = { }; | ||
this._width = 0; | ||
this._out = isFunction(stdout) ? stdout : console.log; | ||
this.option("help", "show this help information", this.showHelp.bind(this)); | ||
this.option("version", "show version information", this.showVersion.bind(this)); | ||
this.option("H", "help", "show this help information", this.showHelp.bind(this)); | ||
this.option("V", "version", "show version information", this.showVersion.bind(this)); | ||
return this; | ||
@@ -97,3 +99,3 @@ } | ||
*/ | ||
Parser.prototype.description = function(name, text) { | ||
Parser.prototype.description = function description(name, text) { | ||
if (!text) { | ||
@@ -111,7 +113,7 @@ this._description = name; | ||
* Adds Version information | ||
* @param {String} version | ||
* @param {String} versionNum | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.version = function(version) { | ||
this._version = version; | ||
Parser.prototype.version = function version(versionNum) { | ||
this._version = versionNum; | ||
return this; | ||
@@ -122,9 +124,16 @@ }; | ||
/** | ||
* Adds an option/command | ||
* @param {String} command | ||
* @param {String} description | ||
* @param {Function} [func] | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.option = function(command, description, func) { | ||
* Adds an option/command | ||
* @param {String} [alias] | ||
* @param {String} command | ||
* @param {String} description | ||
* @param {Function} [func] | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.option = function option(alias, command, description, func) { | ||
if ((!func) && alias && command && isFunction(description)) { | ||
func = description; | ||
description = command; | ||
command = alias; | ||
alias = undefined; | ||
} | ||
if (!(isFunction(func) && isString(description) && isString(command))) { | ||
@@ -140,2 +149,6 @@ return this; | ||
command = key + " " + tag; | ||
if (alias) { | ||
this._short[alias] = key; | ||
command = alias + ", " + command; | ||
} | ||
this._commands[key] = { | ||
@@ -157,2 +170,3 @@ description: description.trim(), | ||
* @param {Function} func | ||
* @return {Parser} this Parser instance | ||
*/ | ||
@@ -168,2 +182,16 @@ Parser.prototype.defaultOption = function defaultOption(func) { | ||
/** | ||
* Add a pre-run function | ||
* | ||
* @param {Function} func | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.prerun = function prerun(func) { | ||
if (func) { | ||
this._hooks.prerun = func; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Adds a bottom epilog | ||
@@ -184,21 +212,29 @@ * @param {String} epilog | ||
*/ | ||
Parser.prototype.parse = function(cmds) { | ||
Parser.prototype.parse = function parse(cmds) { | ||
var me = this; | ||
var args = isString(cmds) ? cmds.split(" ") : processArgv(); | ||
var context = { }; | ||
var command = args[0]; | ||
function exec(target, ctx) { | ||
if (me._hooks.prerun) { | ||
me._hooks.prerun.apply(ctx, ctx._); | ||
} | ||
target.apply(ctx, ctx._); | ||
} | ||
if (command === undefined || command === "" || command[0] === "-") { | ||
context = argv(args); | ||
context._option = "default"; | ||
this._default.apply(context, []); | ||
} else if (this._commands[command]) { | ||
exec(me._default, context); | ||
} else if (me._commands[command] || me._short[command]) { | ||
var target = me._commands[command] || me._commands[me._short[command]]; | ||
var option = args.shift(); | ||
context = argv(args); | ||
context._option = option; | ||
this._commands[command].func.apply(context, context._); | ||
exec(target.func, context); | ||
} else { | ||
var output = "INVALID OPTION: " + command; | ||
output += "\nTry \"help\" for a list of available commands"; | ||
this._out(output); | ||
me._out(output); | ||
} | ||
return this; | ||
return me; | ||
}; | ||
@@ -210,4 +246,5 @@ | ||
* passed to the output function | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.showHelp = function() { | ||
Parser.prototype.showHelp = function showHelp() { | ||
var output = " "; | ||
@@ -230,2 +267,3 @@ if (this._name) { | ||
this._out(output); | ||
return this; | ||
}; | ||
@@ -237,4 +275,5 @@ | ||
* function | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.showVersion = function() { | ||
Parser.prototype.showVersion = function showVersion() { | ||
var info = ""; | ||
@@ -246,2 +285,3 @@ if (this._name) { | ||
this._out(info); | ||
return this; | ||
}; | ||
@@ -248,0 +288,0 @@ |
{ | ||
"name": "simple-argparse", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Simple Argument parser for Command-line Applications", | ||
@@ -30,3 +30,3 @@ "homepage": "https://github.com/forfutureLLC/node-simple-argparse", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-eslint": "^16.0.0", | ||
"grunt-eslint": "^17.0.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
@@ -33,0 +33,0 @@ "istanbul": "^0.3.17", |
@@ -24,3 +24,3 @@ | ||
.version("0.3.0") | ||
.option("start", "starts application", startFunc) | ||
.option("s", "start", "starts application", startFunc) | ||
.epilog("See License at http://opensource.org/licenses/MIT") | ||
@@ -40,5 +40,5 @@ .parse(); | ||
help show this help information | ||
start starts application | ||
version show version information | ||
H, help show this help information | ||
s, start starts application | ||
V, version show version information | ||
@@ -78,4 +78,5 @@ See License at http://opensource.org/licenses/MIT | ||
* __Parser#option(command:String, description:String [, optionFunction:Function])__ | ||
* __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__: | ||
@@ -91,2 +92,6 @@ * a string that will be typed by user to fire the command | ||
* __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. | ||
* __Parser#epilog(epilog:String)__ | ||
@@ -93,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14706
249
194