command-line-args
Advanced tools
Comparing version 1.0.0-0 to 1.0.0-1
@@ -55,10 +55,13 @@ "use strict"; | ||
/* initialise output structure */ | ||
var output = this.definitions.applyInitialValues(); | ||
/* create output initialised with default values */ | ||
var output = this.definitions.createOutput(); | ||
var def; | ||
output = argv.reduce(function(output, item){ | ||
/* walk argv building the output */ | ||
argv.forEach(function(item){ | ||
if (option.isOption(item)){ | ||
var arg = item; | ||
def = self.definitions.get(arg); | ||
output[def.name] = getDefaultOptionValue(def); | ||
if (!t.isDefined(output[def.name])) output.set(def.name, def.getInitialValue()) | ||
if (def.isBoolean()) def = null; | ||
@@ -69,15 +72,12 @@ } else { | ||
def = self.definitions.getDefault(); | ||
output[def.name] = getDefaultOptionValue(def); | ||
if (!def) return; | ||
if (!t.isDefined(output[def.name])) output.set(def.name, def.getInitialValue()); | ||
} | ||
var outputValue = def.type ? def.type(value) : value; | ||
output.set(def.name, outputValue); | ||
if (Array.isArray(output[def.name])){ | ||
output[def.name].push(outputValue); | ||
} else { | ||
output[def.name] = outputValue; | ||
} | ||
if (!def.multiple) def = null; | ||
} | ||
return output; | ||
}, output); | ||
}); | ||
@@ -118,11 +118,1 @@ /* group the output values */ | ||
}; | ||
function getDefaultOptionValue(def, output){ | ||
if (def.multiple){ | ||
return []; | ||
} else if (def.type === Boolean || !def.type){ | ||
return true; | ||
} else { | ||
return null; | ||
} | ||
} |
@@ -25,3 +25,4 @@ "use strict"; | ||
/** | ||
@type {string} - a single character | ||
a single character | ||
@type {string} | ||
*/ | ||
@@ -41,2 +42,7 @@ this.alias = definition.alias; | ||
/** | ||
@type {boolean} | ||
*/ | ||
this.defaultOption = definition.defaultOption; | ||
/** | ||
@type {string|string[]} | ||
@@ -46,1 +52,14 @@ */ | ||
} | ||
Definition.prototype.getInitialValue = function(){ | ||
if (this.multiple){ | ||
return []; | ||
} else if (this.type === Boolean || !this.type){ | ||
return true; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
Definition.prototype.isBoolean = function(){ | ||
return this.type === Boolean; | ||
}; |
@@ -5,2 +5,4 @@ "use strict"; | ||
var option = require("./option"); | ||
var Definition = require("./definition"); | ||
var Output = require("./output"); | ||
@@ -13,3 +15,6 @@ /** | ||
function Definitions(definitions){ | ||
a.call(this, definitions); | ||
definitions = a.arrayify(definitions); | ||
a.call(this, definitions.map(function(def){ | ||
return new Definition(def); | ||
})); | ||
} | ||
@@ -46,4 +51,4 @@ util.inherits(Definitions, a); | ||
Definitions.prototype.applyInitialValues = function(){ | ||
var output = {}; | ||
Definitions.prototype.createOutput = function(){ | ||
var output = new Output(); | ||
this.forEach(function(def){ | ||
@@ -50,0 +55,0 @@ if (def.value) output[def.name] = def.value; |
{ | ||
"name": "command-line-args", | ||
"version": "1.0.0-0", | ||
"version": "1.0.0-1", | ||
"description": "Command-line parser, usage text producer", | ||
@@ -10,3 +10,3 @@ "repository": "https://github.com/75lb/command-line-args.git", | ||
"test": "tape test/*.js", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/command-line-args.js > README.md; echo" | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
}, | ||
@@ -13,0 +13,0 @@ "keywords": [ |
@@ -26,2 +26,15 @@ [![view on npm](http://img.shields.io/npm/v/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
## API Reference | ||
## Modules | ||
<dl> | ||
<dt><a href="#module_command-line-args">command-line-args</a></dt> | ||
<dd></dd> | ||
<dt><a href="#module_definition">definition</a></dt> | ||
<dd></dd> | ||
<dt><a href="#module_definitions">definitions</a></dt> | ||
<dd></dd> | ||
<dt><a href="#module_option">option</a></dt> | ||
<dd></dd> | ||
</dl> | ||
<a name="module_command-line-args"></a> | ||
## command-line-args | ||
<a name="exp_module_command-line-args--CliArgs"></a> | ||
@@ -36,5 +49,49 @@ ### CliArgs(definitions, argv) ⇒ <code>object</code> ⏏ | ||
<a name="module_definition"></a> | ||
## definition | ||
* [definition](#module_definition) | ||
* [Definition](#exp_module_definition--Definition) ⏏ | ||
* [.name](#module_definition--Definition+name) : <code>string</code> | ||
* [.type](#module_definition--Definition+type) : <code>function</code> | ||
* [.alias](#module_definition--Definition+alias) : <code>string</code> | ||
* [.multiple](#module_definition--Definition+multiple) : <code>boolean</code> | ||
* [.value](#module_definition--Definition+value) : <code>boolean</code> | ||
* [.defaultOption](#module_definition--Definition+defaultOption) : <code>boolean</code> | ||
* [.group](#module_definition--Definition+group) : <code>string</code> | <code>Array.<string></code> | ||
<a name="exp_module_definition--Definition"></a> | ||
### Definition ⏏ | ||
Option Definition | ||
**Kind**: Exported class | ||
<a name="module_definition--Definition+name"></a> | ||
#### definition.name : <code>string</code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+type"></a> | ||
#### definition.type : <code>function</code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+alias"></a> | ||
#### definition.alias : <code>string</code> | ||
a single character | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+multiple"></a> | ||
#### definition.multiple : <code>boolean</code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+value"></a> | ||
#### definition.value : <code>boolean</code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+defaultOption"></a> | ||
#### definition.defaultOption : <code>boolean</code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definition--Definition+group"></a> | ||
#### definition.group : <code>string</code> | <code>Array.<string></code> | ||
**Kind**: instance property of <code>[Definition](#exp_module_definition--Definition)</code> | ||
<a name="module_definitions"></a> | ||
## definitions | ||
<a name="module_option"></a> | ||
## option | ||
* * * | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown). |
@@ -18,6 +18,7 @@ var test = require("tape"); | ||
}); | ||
t.deepEqual(cliArgs([{ name: "two", multiple: true, value: ["two", "zwei"] }]).parse([ "--two", "zwei" ]), { | ||
two: [ "zwei" ] | ||
}); | ||
t.deepEqual( | ||
cliArgs([{ name: "two", multiple: true, value: ["two", "zwei"] }]).parse([ "--two", "duo" ]), | ||
{ two: [ "two", "zwei", "duo" ] } | ||
); | ||
t.end(); | ||
}); |
@@ -9,16 +9,17 @@ var test = require("tape"); | ||
test("type-boolean: different values", function(t){ | ||
var cli = cliArgs(optionDefinitions); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one" ]), | ||
cli.parse([ "--one" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one true" ]), | ||
cli.parse([ "--one", "true" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one false" ]), | ||
cli.parse([ "--one", "false" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one sfsgf" ]), | ||
cli.parse([ "--one", "sfsgf" ]), | ||
{ one: true } | ||
@@ -25,0 +26,0 @@ ); |
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
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
32465
35
856
96
0