simple-argparse
Advanced tools
Comparing version 0.0.0 to 0.1.0
331
index.js
@@ -11,3 +11,6 @@ /* | ||
// Module imports | ||
"use strict"; | ||
// npm-installed modules | ||
var argv = require("minimist"); | ||
@@ -17,11 +20,12 @@ | ||
/** | ||
* Shifts the process arguments removing the node executable and | ||
* filepath. leaving the rest or arguments | ||
* This function is _impure_. It relies on _process.argv_ | ||
* It also has _side_effects_. It manipulates _process.argv_ | ||
*/ | ||
* Shifts the process arguments removing the node executable and | ||
* filepath. leaving the rest or arguments | ||
* This function is _impure_. It relies on _process.argv_ | ||
* | ||
* @return {Array} arguments | ||
*/ | ||
function processArgv() { | ||
var args = process.argv; | ||
args.shift();// node | ||
args.shift();// filename/command-name | ||
var args = process.argv.slice(); | ||
args.shift(); // node | ||
args.shift(); // filename/command-name | ||
return args; | ||
@@ -32,8 +36,9 @@ } | ||
/** | ||
* Wraps padding space around some text, specfically by adding spaces | ||
* at the end of text | ||
* @param {String} text | ||
* @param {Number} width - width of column | ||
* @return {String} padded text | ||
*/ | ||
* Wraps padding space around some text, specfically by adding spaces | ||
* at the end of text | ||
* | ||
* @param {String} text | ||
* @param {Number} width - width of column | ||
* @return {String} padded text | ||
*/ | ||
function pad(text, width) { | ||
@@ -46,6 +51,9 @@ var space = (width - text.length) + 5; | ||
/** | ||
* Returns `true` if `func` is a Function. Otherwise `false` | ||
*/ | ||
function isFunction(func) { | ||
return typeof(func) === "function"; | ||
* Returns `true` if `variable` is a Function. Otherwise `false`. | ||
* | ||
* @param {*} variable | ||
* @return {Boolean} | ||
*/ | ||
function isFunction(variable) { | ||
return typeof variable === "function"; | ||
} | ||
@@ -55,6 +63,9 @@ | ||
/** | ||
* Returns `true` if `string` is a String. Otherwise `false` | ||
*/ | ||
function isString(string) { | ||
return typeof(string) === "string"; | ||
* Returns `true` if `variable` is a String. Otherwise `false` | ||
* | ||
* @param {*} variable | ||
* @return {Boolean} | ||
*/ | ||
function isString(variable) { | ||
return typeof variable === "string"; | ||
} | ||
@@ -64,150 +75,158 @@ | ||
/** | ||
* Parser class | ||
* @class | ||
* @param {Function} stdout - called with visual output | ||
* @return {Parser} | ||
*/ | ||
var Parser = (function() { | ||
/** | ||
* Parser Constructor | ||
* @param {Function} stdout - passed output | ||
*/ | ||
function Parser(stdout) { | ||
this._name; | ||
this._description; | ||
this._version = "0.0.0"; | ||
this._commands = {}; | ||
this._epilog; | ||
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)); | ||
* Parser | ||
* @constructor | ||
* @param {Function} stdout - passed output | ||
* @return {Parser} | ||
*/ | ||
function Parser(stdout) { | ||
this._name = null; | ||
this._description = null; | ||
this._version = "0.0.0"; | ||
this._epilog = null; | ||
this._commands = { }; | ||
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)); | ||
return this; | ||
} | ||
/** | ||
* Adds Name and/or Description to the Parser | ||
* @param {String} [name] | ||
* @param {String} text | ||
* @return {Parser} this Parser Instance | ||
*/ | ||
Parser.prototype.description = function(name, text) { | ||
if (!text) { | ||
this._description = name; | ||
} else { | ||
this._name = name; | ||
this._description = text; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Adds Name and/or Description to the Parser | ||
* @param {String} name (Optional) | ||
* @param {String} text | ||
* @param {Parser} this Parser Instance | ||
*/ | ||
Parser.prototype.description = function(name, text) { | ||
if (! text) { | ||
this._description = name; | ||
} else { | ||
this._name = name; | ||
this._description = text; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Adds Version information | ||
* @param {String} version | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.version = function(version) { | ||
this._version = version; | ||
return this; | ||
}; | ||
/** | ||
* Adds Version information | ||
* @param {String} version | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.version = function(version) { | ||
this._version = version; | ||
return this; | ||
}; | ||
/** | ||
* 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) { | ||
if (!(isFunction(func) && isString(description) && isString(command))) { | ||
return this; | ||
} | ||
var idx = command.search(/[\[<]/); | ||
var key = idx < 0 ? command : command.substring(0, idx); | ||
var tag = idx < 0 ? "" : command.substring(idx); | ||
key = key.trim(); | ||
key = key.replace(" ", "-"); | ||
tag = tag.trim(); | ||
command = key + " " + tag; | ||
this._commands[key] = { | ||
description: description.trim(), | ||
func: func, | ||
repr: command | ||
}; | ||
var length = command.length; | ||
if (length > this._width) this._width = length; | ||
return this; | ||
}; | ||
/** | ||
* Adds a bottom epilog | ||
* @param {String} epilog | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.epilog = function(epilog) { | ||
this._epilog = isString(epilog) ? epilog : undefined; | ||
/** | ||
* 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) { | ||
if (!(isFunction(func) && isString(description) && isString(command))) { | ||
return this; | ||
} | ||
var idx = command.search(/[\[<]/); | ||
var key = idx < 0 ? command : command.substring(0, idx); | ||
var tag = idx < 0 ? "" : command.substring(idx); | ||
key = key.trim(); | ||
key = key.replace(" ", "-"); | ||
tag = tag.trim(); | ||
command = key + " " + tag; | ||
this._commands[key] = { | ||
description: description.trim(), | ||
func: func, | ||
repr: command, | ||
}; | ||
var length = command.length; | ||
if (length > this._width) { | ||
this._width = length; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Parses a string for commands | ||
* @param {String} commandString | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.parse = function(cmds) { | ||
var args = isString(cmds) ? cmds.split(" ") : processArgv(); | ||
if (args.length === 0) { return this.showHelp(); } | ||
var command = args.shift(); | ||
args = argv(args); | ||
if (! this._commands[command]) { | ||
var output = "INVALID OPTION: " + command; | ||
output += "\nTry \"help\" for a list of available commands"; | ||
this._out(output); | ||
} else { | ||
this._commands[command].func.apply(args, args._); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Show help: name, description, options and epilog strings are | ||
* passed to the output function | ||
*/ | ||
Parser.prototype.showHelp = function() { | ||
var output = ""; | ||
if (this._name) { output += this._name + ": "; } | ||
output += (this._description || "") + "\n\n"; | ||
var commands = []; | ||
for (var command in this._commands) { | ||
commands.push(" " + | ||
pad(this._commands[command].repr, this._width) + | ||
this._commands[command].description); | ||
} | ||
commands = commands.sort(); | ||
output += commands.join("\n") + "\n"; | ||
if (this._epilog) output += "\n" + this._epilog; | ||
/** | ||
* Adds a bottom epilog | ||
* @param {String} epilog | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.epilog = function(epilog) { | ||
this._epilog = isString(epilog) ? epilog : undefined; | ||
return this; | ||
}; | ||
/** | ||
* Parses a string for commands | ||
* @param {String} commandString | ||
* @return {Parser} this Parser instance | ||
*/ | ||
Parser.prototype.parse = function(cmds) { | ||
var args = isString(cmds) ? cmds.split(" ") : processArgv(); | ||
if (args.length === 0) { | ||
return this.showHelp(); | ||
} | ||
var command = args.shift(); | ||
args = argv(args); | ||
if (!this._commands[command]) { | ||
var output = "INVALID OPTION: " + command; | ||
output += "\nTry \"help\" for a list of available commands"; | ||
this._out(output); | ||
}; | ||
} else { | ||
this._commands[command].func.apply(args, args._); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Show version: name and version strings are passed to the output | ||
* function | ||
*/ | ||
Parser.prototype.showVersion = function() { | ||
var info = ""; | ||
if (this._name) { info += this._name + " "; } | ||
info += this._version; | ||
this._out(info); | ||
}; | ||
return Parser; | ||
})(); | ||
/** | ||
* Show help: name, description, options and epilog strings are | ||
* passed to the output function | ||
*/ | ||
Parser.prototype.showHelp = function() { | ||
var output = " "; | ||
if (this._name) { | ||
output += this._name + ": "; | ||
} | ||
output += (this._description || "") + "\n\n"; | ||
var commands = []; | ||
for (var command in this._commands) { | ||
commands.push(" " + | ||
pad(this._commands[command].repr, this._width) + | ||
this._commands[command].description); | ||
} | ||
commands = commands.sort(); | ||
output += commands.join("\n") + "\n"; | ||
if (this._epilog) { | ||
output += "\n " + this._epilog; | ||
} | ||
this._out(output); | ||
}; | ||
/** | ||
* Exporting a new Parser by just `require()`ing the module | ||
* Parser may be also be accessed by require(..).Parser | ||
*/ | ||
exports = module.exports = (new Parser()); | ||
* Show version: name and version strings are passed to the output | ||
* function | ||
*/ | ||
Parser.prototype.showVersion = function() { | ||
var info = ""; | ||
if (this._name) { | ||
info += this._name + " "; | ||
} | ||
info += this._version; | ||
this._out(info); | ||
}; | ||
/** | ||
* Exporting a new Parser by just `require()`ing the module | ||
* Parser may be also be accessed by require(..).Parser | ||
*/ | ||
exports = module.exports = new Parser(); | ||
exports.Parser = Parser; |
{ | ||
"name": "simple-argparse", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Simple Argument parser for Command-line Applications", | ||
@@ -9,6 +9,6 @@ "homepage": "https://github.com/forfuture-dev/node-simple-argparse", | ||
"type": "git", | ||
"url": "https://github.com/forfuture-dev/node-simple-argparse.git" | ||
"url": "https://github.com/forfutureLLC/node-simple-argparse.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/forfuture-dev/node-simple-argparse/issues" | ||
"url": "https://github.com/forfutureLLC/node-simple-argparse/issues" | ||
}, | ||
@@ -25,15 +25,22 @@ "keywords": [ | ||
"dependencies": { | ||
"minimist": "1.1.0" | ||
"minimist": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.3.5", | ||
"mocha": "2.0.1", | ||
"should": "4.4.1" | ||
"grunt": "^0.4.5", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-eslint": "^16.0.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
"istanbul": "^0.3.17", | ||
"load-grunt-tasks": "^3.2.0", | ||
"should": "^7.0.2" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha test/test.*.js" | ||
"test": "grunt test" | ||
}, | ||
"directories": { | ||
"test": "tests" | ||
"test": "test" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
} | ||
} |
109
README.md
# simple-argparse | ||
Simple Argument parser for Command-line Applications | ||
> Simple Argument parser for Command-line Applications | ||
[![node](https://img.shields.io/node/v/node-simple-argparse.svg?style=flat-square)](https://www.npmjs.com/package/simple-argparse) [![npm](https://img.shields.io/npm/v/simple-argparse.svg?style=flat-square)](https://www.npmjs.com/package/simple-argparse) [![Travis](https://img.shields.io/travis/forfutureLLC/node-simple-argparse.svg?style=flat-square)](https://travis-ci.org/forfutureLLC/node-simple-argparse) [![Gemnasium](https://img.shields.io/gemnasium/forfutureLLC/node-simple-argparse.svg?style=flat-square)](https://gemnasium.com/forfutureLLC/node-simple-argparse) [![Coveralls](https://img.shields.io/coveralls/forfutureLLC/node-simple-argparse.svg?style=flat-square)](https://coveralls.io/github/forfutureLLC/node-simple-argparse?branch=master) | ||
## module information | ||
[![Build Status](https://travis-ci.org/forfuture-dev/node-simple-argparse.svg)](https://travis-ci.org/forfuture-dev/node-simple-argparse) [![Dependency Status](https://gemnasium.com/forfuture-dev/node-simple-argparse.svg)](https://gemnasium.com/forfuture-dev/node-simple-argparse) [![Coverage Status](https://img.shields.io/coveralls/forfuture-dev/node-simple-argparse.svg)](https://coveralls.io/r/forfuture-dev/node-simple-argparse) | ||
|Aspect|Detail| | ||
|------|-----:| | ||
|Version|0.0.0| | ||
|Node|0.11, 0.10, 0.8, 0.6| | ||
|Last Updated|22nd Dec, 2014| | ||
## installation | ||
@@ -25,5 +15,6 @@ | ||
## basic usage | ||
_Sample.js_: | ||
_sample.js_: | ||
@@ -36,3 +27,3 @@ ```js | ||
.epilog("See License at http://opensource.org/licenses/MIT") | ||
.parse() | ||
.parse(); | ||
@@ -44,28 +35,30 @@ function startFunc(host, port) { | ||
_Sample Output_: | ||
_sample output_: | ||
```bash | ||
⇒ node Sample.js | ||
Application Description | ||
Application Description | ||
help show this help information | ||
start starts application | ||
version show version information | ||
help show this help information | ||
start starts application | ||
version show version information | ||
See License at http://opensource.org/licenses/MIT | ||
See License at http://opensource.org/licenses/MIT | ||
``` | ||
The module exports a new Parser, that can be used immediately. If you | ||
wish to create more Parsers, you could: | ||
## 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`: | ||
```js | ||
var Parser = require("simple-argparse").Parser; | ||
var myParser = new myParser(); | ||
var myParser = new Parser(); | ||
``` | ||
While instantiating a parser, an output function may be registered with | ||
the parser other than the default `console.log` | ||
the parser other than the default `console.log`: | ||
```js | ||
var myOtherParser = new myParser(function(output) { | ||
var myOtherParser = new Parser(function(output) { | ||
socket.emit("commandComplete", output); | ||
@@ -75,28 +68,27 @@ }); | ||
A Parser has these methods: | ||
A `Parser` has these methods: | ||
1. __parser#description([name:String,] description:String)__ | ||
* __name__:(Optional) preferably refers to the name of your Application | ||
1. __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)__ | ||
* __Parser#version(version:String)__ | ||
* __version__: provides version information of your Application | ||
* defaults to "0.0.0" | ||
* __version__: provides version information of your Application. Defaults to `"0.0.0"` | ||
* __parser#option(command:String, description:String [, optionFunction:Function])__ | ||
* __Parser#option(command:String, description:String [, optionFunction:Function])__ | ||
* __command__: | ||
* a string that will be typed by user to fire command | ||
* a string that will be typed by user to fire the command | ||
* any spaces will be replaced by hyphens | ||
* __description__: information regarding this command | ||
* __description__: help information regarding this command | ||
* __optionFunction__:(Optional) See [Parsing](#parsing) below for more information. | ||
* __parser#epilog(epilog:String)__ | ||
* __epilog__: a string that will appear at the bottom of help information | ||
* __Parser#epilog(epilog:String)__ | ||
* __parser#parse([arguments:String])__ | ||
* __epilog__: a string that will appear at the bottom of the help information | ||
* __Parser#parse([arguments:String])__ | ||
* __arguments__:(Optional) | ||
@@ -106,10 +98,10 @@ * a string representing commands as typed in command-line | ||
* __parser#showHelp()__ | ||
* __Parser#showHelp()__ | ||
* shows the help information | ||
* is done by passing all the necessary data as string to the registered output function | ||
* __parser#showVersion()__ | ||
* __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 `.version()`. | ||
@@ -121,3 +113,3 @@ | ||
All arguments parsed by `.parse()` are processed using | ||
[minimist][minimist], and made available to the __option functions__ as | ||
[minimist][minimist], and made available to the __option functions__ as | ||
their `this` argument. | ||
@@ -127,3 +119,3 @@ | ||
Options that are __NOT__ perceived as options by __minimist__ are passed | ||
to the function as arguments. | ||
to the function as `arguments`. | ||
@@ -146,3 +138,3 @@ Consider the following example: | ||
}) | ||
.parse() | ||
.parse(); | ||
``` | ||
@@ -168,3 +160,3 @@ | ||
The __option function__ is optional. If it is left out, the option will | ||
be ignored. This may be useful for commands not yet implemented. | ||
be ignored. This may be useful for commands __not yet implemented__. | ||
@@ -174,26 +166,7 @@ | ||
The MIT License (MIT) | ||
__The MIT License (MIT)__ | ||
Copyright (c) 2014 Forfuture LLC <we@forfuture.co.ke> | ||
Copyright (c) 2014-2015 Forfuture LLC <we@forfuture.co.ke> | ||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation the | ||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
sell copies of the Software, and to permit persons to whom the Software | ||
is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
[minimist]:https://github.com/substack/minimist |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
11820
7
5
196
163
+ Addedminimist@1.2.8(transitive)
- Removedminimist@1.1.0(transitive)
Updatedminimist@^1.1.1