command-line-args
Advanced tools
Comparing version 1.0.1 to 1.0.2
#!/usr/bin/env node | ||
"use strict"; | ||
var cliArgs = require("../"); | ||
var os = require("os"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var ansi = require("ansi-escape-sequences"); | ||
'use strict' | ||
var cliArgs = require('../') | ||
var os = require('os') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var ansi = require('ansi-escape-sequences') | ||
var tmpPath = path.join(os.tmpDir(), Date.now() + "-cla.js"); | ||
var tmpPath = path.join(os.tmpDir(), Date.now() + '-cla.js') | ||
process.stdin | ||
.pipe(fs.createWriteStream(tmpPath)) | ||
.on("close", parseCla); | ||
function parseCla(){ | ||
var cliOptions = require(tmpPath); | ||
fs.unlinkSync(tmpPath); | ||
var cli = cliArgs(cliOptions); | ||
try { | ||
console.log(cli.parse()); | ||
} catch(err){ | ||
halt(err.message); | ||
} | ||
.pipe(fs.createWriteStream(tmpPath)) | ||
.on('close', parseCla) | ||
function parseCla () { | ||
var cliOptions = require(tmpPath) | ||
fs.unlinkSync(tmpPath) | ||
var cli = cliArgs(cliOptions) | ||
try { | ||
console.log(cli.parse()) | ||
} catch (err) { | ||
halt(err.message) | ||
} | ||
} | ||
function halt(msg){ | ||
console.error(ansi.format(msg, "red")); | ||
process.exit(1); | ||
function halt (msg) { | ||
console.error(ansi.format(msg, 'red')) | ||
process.exit(1) | ||
} |
module.exports = [ | ||
{ name: "hot", alias: "h", type: Boolean }, | ||
{ name: "discount", alias: "d", type: Boolean }, | ||
{ name: "courses", alias: "c" , type: Number } | ||
]; | ||
{ name: 'hot', alias: 'h', type: Boolean }, | ||
{ name: 'discount', alias: 'd', type: Boolean }, | ||
{ name: 'courses', alias: 'c', type: Number } | ||
] |
module.exports = [ | ||
{ name: "files", type: String, multiple: true, value: [ "one.js" ] }, | ||
{ name: "max", type: Number, value: 3 } | ||
]; | ||
{ name: 'files', type: String, multiple: true, value: [ 'one.js' ] }, | ||
{ name: 'max', type: Number, value: 3 } | ||
] |
module.exports = [ | ||
{ name: "verbose", group: "standard" }, | ||
{ name: "help", group: [ "standard", "main" ] }, | ||
{ name: "compress", group: [ "server", "main" ] }, | ||
{ name: "static", group: "server" }, | ||
{ name: "debug" } | ||
]; | ||
{ name: 'verbose', group: 'standard' }, | ||
{ name: 'help', group: [ 'standard', 'main' ] }, | ||
{ name: 'compress', group: [ 'server', 'main' ] }, | ||
{ name: 'static', group: 'server' }, | ||
{ name: 'debug' } | ||
] |
module.exports = [ | ||
{ name: "files", type: String, multiple: true } | ||
]; | ||
{ name: 'files', type: String, multiple: true } | ||
] |
module.exports = [ | ||
{ name: "file" }, | ||
{ name: "verbose" }, | ||
{ name: "depth"} | ||
]; | ||
{ name: 'file' }, | ||
{ name: 'verbose' }, | ||
{ name: 'depth' } | ||
] |
@@ -1,12 +0,12 @@ | ||
var fs = require("fs"); | ||
var fs = require('fs') | ||
function FileDetails(filename){ | ||
if (!(this instanceof FileDetails)) return new FileDetails(filename); | ||
this.filename = filename; | ||
this.exists = fs.existsSync(filename); | ||
function FileDetails (filename) { | ||
if (!(this instanceof FileDetails)) return new FileDetails(filename) | ||
this.filename = filename | ||
this.exists = fs.existsSync(filename) | ||
} | ||
module.exports = [ | ||
{ name: "file", type: FileDetails }, | ||
{ name: "depth", type: Number } | ||
]; | ||
{ name: 'file', type: FileDetails }, | ||
{ name: 'depth', type: Number } | ||
] |
module.exports = [ | ||
{ name: "help", alias: "h", type: Boolean, description: "Display this usage guide." }, | ||
{ name: "src", type: String, multiple: true, defaultOption: true, description: "The input files to process", typeLabel: "<files>" }, | ||
{ name: "timeout", alias: "t", type: Number, description: "Timeout value in ms", typeLabel: "<ms>" }, | ||
{ name: "log", alias: "l", type: Boolean, description: "info, warn or error" } | ||
]; | ||
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this usage guide.' }, | ||
{ name: 'src', type: String, multiple: true, defaultOption: true, description: 'The input files to process', typeLabel: '<files>' }, | ||
{ name: 'timeout', alias: 't', type: Number, description: 'Timeout value in ms', typeLabel: '<ms>' }, | ||
{ name: 'log', alias: 'l', type: Boolean, description: 'info, warn or error' } | ||
] |
@@ -1,32 +0,32 @@ | ||
var cliArgs = require("../"); | ||
var testValue = require("test-value"); | ||
var fs = require("fs"); | ||
var cliArgs = require('../') | ||
var testValue = require('test-value') | ||
var fs = require('fs') | ||
var cli = cliArgs([ | ||
{ name: "help", type: Boolean }, | ||
{ name: "files", type: String, multiple: true, defaultOption: true }, | ||
{ name: "log-level", type: String } | ||
]); | ||
{ name: 'help', type: Boolean }, | ||
{ name: 'files', type: String, multiple: true, defaultOption: true }, | ||
{ name: 'log-level', type: String } | ||
]) | ||
var options = cli.parse(); | ||
var options = cli.parse() | ||
var usageForm = {}; | ||
var usageForm = {} | ||
usageForm.main = { | ||
files: function(files){ | ||
return files && files.every(fs.existsSync); | ||
}, | ||
"log-level": [ "info", "warn", "error", undefined ] | ||
}; | ||
files: function (files) { | ||
return files && files.every(fs.existsSync) | ||
}, | ||
'log-level': [ 'info', 'warn', 'error', undefined ] | ||
} | ||
usageForm.help = { | ||
help: true | ||
}; | ||
help: true | ||
} | ||
var valid = testValue(options, [ usageForm.main, usageForm.help ]); | ||
var valid = testValue(options, [ usageForm.main, usageForm.help ]) | ||
if (!valid){ | ||
// exit here | ||
if (!valid) { | ||
// exit here | ||
} | ||
console.log(valid, options); | ||
console.log(valid, options) |
@@ -1,10 +0,9 @@ | ||
"use strict"; | ||
var a = require("array-tools"); | ||
var o = require("object-tools"); | ||
var util = require("util"); | ||
var Definitions = require("./definitions"); | ||
var option = require("./option"); | ||
var cliUsage = require("command-line-usage"); | ||
var findReplace = require("find-replace"); | ||
var t = require("typical"); | ||
'use strict' | ||
var a = require('array-tools') | ||
var o = require('object-tools') | ||
var Definitions = require('./definitions') | ||
var option = require('./option') | ||
var cliUsage = require('command-line-usage') | ||
var findReplace = require('find-replace') | ||
var t = require('typical') | ||
@@ -16,3 +15,3 @@ /** | ||
*/ | ||
module.exports = CommandLineArgs; | ||
module.exports = CommandLineArgs | ||
@@ -27,3 +26,3 @@ /** | ||
```js | ||
var commandLineArgs = require("command-line-args"); | ||
var commandLineArgs = require("command-line-args") | ||
var cli = commandLineArgs([ | ||
@@ -33,8 +32,8 @@ { name: "file" }, | ||
{ name: "depth"} | ||
]); | ||
]) | ||
``` | ||
*/ | ||
function CommandLineArgs(definitions){ | ||
if (!(this instanceof CommandLineArgs)) return new CommandLineArgs(definitions); | ||
this.definitions = new Definitions(definitions); | ||
function CommandLineArgs (definitions) { | ||
if (!(this instanceof CommandLineArgs)) return new CommandLineArgs(definitions) | ||
this.definitions = new Definitions(definitions) | ||
} | ||
@@ -52,107 +51,106 @@ | ||
*/ | ||
CommandLineArgs.prototype.parse = function(argv){ | ||
var self = this; | ||
/* if no argv supplied, assume we are parsing process.argv */ | ||
argv = argv || process.argv; | ||
if (argv === process.argv){ | ||
argv.splice(0, 2); | ||
} else { | ||
argv = a.arrayify(argv); | ||
} | ||
CommandLineArgs.prototype.parse = function (argv) { | ||
var self = this | ||
/* expand --option=name style args */ | ||
var optEquals = option.optEquals; | ||
if (argv.some(optEquals.test.bind(optEquals))){ | ||
var expandedArgs = []; | ||
argv.forEach(function(arg){ | ||
var matches = arg.match(optEquals.re); | ||
if (matches){ | ||
expandedArgs.push(matches[1], matches[2]); | ||
} else { | ||
expandedArgs.push(arg); | ||
} | ||
}); | ||
argv = expandedArgs; | ||
} | ||
/* if no argv supplied, assume we are parsing process.argv */ | ||
argv = argv || process.argv | ||
if (argv === process.argv) { | ||
argv.splice(0, 2) | ||
} else { | ||
argv = a.arrayify(argv) | ||
} | ||
/* expand getopt-style combined options */ | ||
var combinedArg = option.combined; | ||
var hasGetopt = argv.some(combinedArg.test.bind(combinedArg)); | ||
if (hasGetopt){ | ||
findReplace(argv, combinedArg.re, function(arg){ | ||
arg = arg.slice(1); | ||
return arg.split("").map(function(letter){ | ||
return "-" + letter; | ||
}); | ||
}); | ||
} | ||
/* expand --option=name style args */ | ||
var optEquals = option.optEquals | ||
if (argv.some(optEquals.test.bind(optEquals))) { | ||
var expandedArgs = [] | ||
argv.forEach(function (arg) { | ||
var matches = arg.match(optEquals.re) | ||
if (matches) { | ||
expandedArgs.push(matches[1], matches[2]) | ||
} else { | ||
expandedArgs.push(arg) | ||
} | ||
}) | ||
argv = expandedArgs | ||
} | ||
/* validate input */ | ||
var invalidMessage = this.definitions.validate(argv); | ||
if (invalidMessage){ | ||
throw Error(invalidMessage); | ||
} | ||
/* expand getopt-style combined options */ | ||
var combinedArg = option.combined | ||
var hasGetopt = argv.some(combinedArg.test.bind(combinedArg)) | ||
if (hasGetopt) { | ||
findReplace(argv, combinedArg.re, function (arg) { | ||
arg = arg.slice(1) | ||
return arg.split('').map(function (letter) { | ||
return '-' + letter | ||
}) | ||
}) | ||
} | ||
/* create output initialised with default values */ | ||
var output = this.definitions.createOutput(); | ||
var def; | ||
/* walk argv building the output */ | ||
argv.forEach(function(item){ | ||
if (option.isOption(item)){ | ||
def = self.definitions.get(item); | ||
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue()) | ||
if (def.isBoolean()) { | ||
outputSet(output, def.name, true); | ||
def = null; | ||
} | ||
/* validate input */ | ||
var invalidMessage = this.definitions.validate(argv) | ||
if (invalidMessage) { | ||
throw Error(invalidMessage) | ||
} | ||
} else { | ||
var value = item; | ||
if (!def){ | ||
def = self.definitions.getDefault(); | ||
if (!def) return; | ||
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue()); | ||
} | ||
/* create output initialised with default values */ | ||
var output = this.definitions.createOutput() | ||
var def | ||
var outputValue = def.type ? def.type(value) : value; | ||
outputSet(output, def.name, outputValue); | ||
/* walk argv building the output */ | ||
argv.forEach(function (item) { | ||
if (option.isOption(item)) { | ||
def = self.definitions.get(item) | ||
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue()) | ||
if (def.isBoolean()) { | ||
outputSet(output, def.name, true) | ||
def = null | ||
} | ||
} else { | ||
var value = item | ||
if (!def) { | ||
def = self.definitions.getDefault() | ||
if (!def) return | ||
if (!t.isDefined(output[def.name])) outputSet(output, def.name, def.getInitialValue()) | ||
} | ||
if (!def.multiple) def = null; | ||
} | ||
}); | ||
/* clear _initial flags */ | ||
o.each(output, function(value, key){ | ||
if (Array.isArray(value) && value._initial) delete value._initial; | ||
}); | ||
var outputValue = def.type ? def.type(value) : value | ||
outputSet(output, def.name, outputValue) | ||
/* group the output values */ | ||
if (this.definitions.isGrouped()){ | ||
var grouped = { | ||
_all: output, | ||
}; | ||
if (!def.multiple) def = null | ||
} | ||
}) | ||
this.definitions.whereGrouped().forEach(function(def){ | ||
a.arrayify(def.group).forEach(function(groupName){ | ||
grouped[groupName] = grouped[groupName] || {}; | ||
if (t.isDefined(output[def.name])){ | ||
grouped[groupName][def.name] = output[def.name]; | ||
} | ||
}); | ||
}); | ||
/* clear _initial flags */ | ||
o.each(output, function (value, key) { | ||
if (Array.isArray(value) && value._initial) delete value._initial | ||
}) | ||
this.definitions.whereNotGrouped().forEach(function(def){ | ||
if (t.isDefined(output[def.name])){ | ||
if (!grouped._none) grouped._none = {}; | ||
grouped._none[def.name] = output[def.name]; | ||
} | ||
}); | ||
return grouped; | ||
} else { | ||
return output; | ||
/* group the output values */ | ||
if (this.definitions.isGrouped()) { | ||
var grouped = { | ||
_all: output | ||
} | ||
}; | ||
this.definitions.whereGrouped().forEach(function (def) { | ||
a.arrayify(def.group).forEach(function (groupName) { | ||
grouped[groupName] = grouped[groupName] || {} | ||
if (t.isDefined(output[def.name])) { | ||
grouped[groupName][def.name] = output[def.name] | ||
} | ||
}) | ||
}) | ||
this.definitions.whereNotGrouped().forEach(function (def) { | ||
if (t.isDefined(output[def.name])) { | ||
if (!grouped._none) grouped._none = {} | ||
grouped._none[def.name] = output[def.name] | ||
} | ||
}) | ||
return grouped | ||
} else { | ||
return output | ||
} | ||
} | ||
/** | ||
@@ -164,16 +162,16 @@ Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use. | ||
*/ | ||
CommandLineArgs.prototype.getUsage = function(options){ | ||
return cliUsage(this.definitions.val(), options); | ||
}; | ||
CommandLineArgs.prototype.getUsage = function (options) { | ||
return cliUsage(this.definitions.val(), options) | ||
} | ||
function outputSet(output, property, value){ | ||
if (output[property] && output[property]._initial){ | ||
output[property] = []; | ||
delete output[property]._initial; | ||
} | ||
if (Array.isArray(output[property])){ | ||
output[property].push(value); | ||
} else { | ||
output[property] = value; | ||
} | ||
}; | ||
function outputSet (output, property, value) { | ||
if (output[property] && output[property]._initial) { | ||
output[property] = [] | ||
delete output[property]._initial | ||
} | ||
if (Array.isArray(output[property])) { | ||
output[property].push(value) | ||
} else { | ||
output[property] = value | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict' | ||
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
module.exports = OptionDefinition; | ||
module.exports = OptionDefinition | ||
@@ -15,221 +15,221 @@ /** | ||
*/ | ||
function OptionDefinition(definition){ | ||
/** | ||
* The only required definition property is `name`, so the simplest working example is | ||
* ```js | ||
* [ | ||
* { name: "file" }, | ||
* { name: "verbose" }, | ||
* { name: "depth"} | ||
* ] | ||
* ``` | ||
* | ||
* In this case, the value of each option will be either a Boolean or string. | ||
* | ||
* | # | Command line args | .parse() output | | ||
* | --- | -------------------- | ------------ | | ||
* | 1 | `--file` | `{ file: true }` | | ||
* | 2 | `--file lib.js --verbose` | `{ file: "lib.js", verbose: true }` | | ||
* | 3 | `--verbose very` | `{ verbose: "very" }` | | ||
* | 4 | `--depth 2` | `{ depth: "2" }` | | ||
* | ||
* @type {string} | ||
*/ | ||
this.name = definition.name; | ||
function OptionDefinition (definition) { | ||
/** | ||
* The only required definition property is `name`, so the simplest working example is | ||
* ```js | ||
* [ | ||
* { name: "file" }, | ||
* { name: "verbose" }, | ||
* { name: "depth"} | ||
* ] | ||
* ``` | ||
* | ||
* In this case, the value of each option will be either a Boolean or string. | ||
* | ||
* | # | Command line args | .parse() output | | ||
* | --- | -------------------- | ------------ | | ||
* | 1 | `--file` | `{ file: true }` | | ||
* | 2 | `--file lib.js --verbose` | `{ file: "lib.js", verbose: true }` | | ||
* | 3 | `--verbose very` | `{ verbose: "very" }` | | ||
* | 4 | `--depth 2` | `{ depth: "2" }` | | ||
* | ||
* @type {string} | ||
*/ | ||
this.name = definition.name | ||
/** | ||
* The `type` value is a setter function (you receive the output from this), enabling you to be specific about the type and value received. | ||
* | ||
* You can use a class, if you like: | ||
* | ||
* ```js | ||
* var fs = require("fs"); | ||
* | ||
* function FileDetails(filename){ | ||
* if (!(this instanceof FileDetails)) return new FileDetails(filename); | ||
* this.filename = filename; | ||
* this.exists = fs.existsSync(filename); | ||
* } | ||
* | ||
* module.exports = [ | ||
* { name: "file", type: FileDetails }, | ||
* { name: "depth", type: Number } | ||
* ]; | ||
* ``` | ||
* | ||
* | # | Command line args| .parse() output | | ||
* | --- | ----------------- | ------------ | | ||
* | 1 | `--file asdf.txt` | `{ file: { filename: 'asdf.txt', exists: false } }` | | ||
* | ||
* The `--depth` option expects a `Number`. If no value was set, you will receive `null`. | ||
* | ||
* | # | Command line args | .parse() output | | ||
* | --- | ----------------- | ------------ | | ||
* | 2 | `--depth` | `{ depth: null }` | | ||
* | 3 | `--depth 2` | `{ depth: 2 }` | | ||
* | ||
* @type {function} | ||
*/ | ||
this.type = definition.type; | ||
/** | ||
* The `type` value is a setter function (you receive the output from this), enabling you to be specific about the type and value received. | ||
* | ||
* You can use a class, if you like: | ||
* | ||
* ```js | ||
* var fs = require("fs") | ||
* | ||
* function FileDetails(filename){ | ||
* if (!(this instanceof FileDetails)) return new FileDetails(filename) | ||
* this.filename = filename | ||
* this.exists = fs.existsSync(filename) | ||
* } | ||
* | ||
* module.exports = [ | ||
* { name: "file", type: FileDetails }, | ||
* { name: "depth", type: Number } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line args| .parse() output | | ||
* | --- | ----------------- | ------------ | | ||
* | 1 | `--file asdf.txt` | `{ file: { filename: 'asdf.txt', exists: false } }` | | ||
* | ||
* The `--depth` option expects a `Number`. If no value was set, you will receive `null`. | ||
* | ||
* | # | Command line args | .parse() output | | ||
* | --- | ----------------- | ------------ | | ||
* | 2 | `--depth` | `{ depth: null }` | | ||
* | 3 | `--depth 2` | `{ depth: 2 }` | | ||
* | ||
* @type {function} | ||
*/ | ||
this.type = definition.type | ||
/** | ||
* getopt-style short option names. Must be a single character. | ||
* | ||
* ```js | ||
* [ | ||
* { name: "hot", alias: "h", type: Boolean }, | ||
* { name: "discount", alias: "d", type: Boolean }, | ||
* { name: "courses", alias: "c" , type: Number } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `-hcd` | `{ hot: true, courses: null, discount: true }` | | ||
* | 2 | `-hdc 3` | `{ hot: true, discount: true, courses: 3 }` | | ||
* | ||
* @type {string} | ||
*/ | ||
this.alias = definition.alias; | ||
/** | ||
* getopt-style short option names. Must be a single character. | ||
* | ||
* ```js | ||
* [ | ||
* { name: "hot", alias: "h", type: Boolean }, | ||
* { name: "discount", alias: "d", type: Boolean }, | ||
* { name: "courses", alias: "c" , type: Number } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `-hcd` | `{ hot: true, courses: null, discount: true }` | | ||
* | 2 | `-hdc 3` | `{ hot: true, discount: true, courses: 3 }` | | ||
* | ||
* @type {string} | ||
*/ | ||
this.alias = definition.alias | ||
/** | ||
* Set this flag if the option takes a list of values. You will receive an array of values passed through the `type` function (if specified). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true } | ||
* ]; | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `--files one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 2 | `--files one.js --files two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 3 | `--files *` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | ||
* @type {boolean} | ||
*/ | ||
this.multiple = definition.multiple; | ||
/** | ||
* Set this flag if the option takes a list of values. You will receive an array of values passed through the `type` function (if specified). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `--files one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 2 | `--files one.js --files two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 3 | `--files *` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | ||
* @type {boolean} | ||
*/ | ||
this.multiple = definition.multiple | ||
/** | ||
* Any unclaimed command-line args will be set on this option. This flag is typically set on the most commonly-used option to make for more concise usage (i.e. `$ myapp *.js` instead of `$ myapp --files *.js`). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true, defaultOption: true } | ||
* ]; | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `--files one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 2 | `one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 3 | `*` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | ||
* @type {boolean} | ||
*/ | ||
this.defaultOption = definition.defaultOption; | ||
/** | ||
* Any unclaimed command-line args will be set on this option. This flag is typically set on the most commonly-used option to make for more concise usage (i.e. `$ myapp *.js` instead of `$ myapp --files *.js`). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true, defaultOption: true } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | `--files one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 2 | `one.js two.js` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | 3 | `*` | `{ files: [ 'one.js', 'two.js' ] }` | | ||
* | ||
* @type {boolean} | ||
*/ | ||
this.defaultOption = definition.defaultOption | ||
/** | ||
* An initial value for the option. | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true, defaultValue: [ "one.js" ] }, | ||
* { name: "max", type: Number, defaultValue: 3 } | ||
* ]; | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | | `{ files: [ 'one.js' ], max: 3 }` | | ||
* | 2 | `--files two.js` | `{ files: [ 'two.js' ], max: 3 }` | | ||
* | 3 | `--max 4` | `{ files: [ 'one.js' ], max: 4 }` | | ||
* | ||
* @type {*} | ||
*/ | ||
this.defaultValue = definition.defaultValue; | ||
/** | ||
* An initial value for the option. | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "files", type: String, multiple: true, defaultValue: [ "one.js" ] }, | ||
* { name: "max", type: Number, defaultValue: 3 } | ||
* ] | ||
* ``` | ||
* | ||
* | # | Command line | .parse() output | | ||
* | --- | ------------ | ------------ | | ||
* | 1 | | `{ files: [ 'one.js' ], max: 3 }` | | ||
* | 2 | `--files two.js` | `{ files: [ 'two.js' ], max: 3 }` | | ||
* | 3 | `--max 4` | `{ files: [ 'one.js' ], max: 4 }` | | ||
* | ||
* @type {*} | ||
*/ | ||
this.defaultValue = definition.defaultValue | ||
/** | ||
* When your app has a large amount of options it makes sense to organise them in groups. | ||
* | ||
* There are two automatic groups: `_all` (contains all options) and `_none` (contains options without a `group` specified in their definition). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "verbose", group: "standard" }, | ||
* { name: "help", group: [ "standard", "main" ] }, | ||
* { name: "compress", group: [ "server", "main" ] }, | ||
* { name: "static", group: "server" }, | ||
* { name: "debug" } | ||
* ]; | ||
* ``` | ||
* | ||
*<table> | ||
* <tr> | ||
* <th>#</th><th>Command Line</th><th>.parse() output</th> | ||
* </tr> | ||
* <tr> | ||
* <td>1</td><td><code>--verbose</code></td><td><pre><code> | ||
*{ | ||
* _all: { verbose: true }, | ||
* standard: { verbose: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>2</td><td><code>--debug</code></td><td><pre><code> | ||
*{ | ||
* _all: { debug: true }, | ||
* _none: { debug: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>3</td><td><code>--verbose --debug --compress</code></td><td><pre><code> | ||
*{ | ||
* _all: { | ||
* verbose: true, | ||
* debug: true, | ||
* compress: true | ||
* }, | ||
* standard: { verbose: true }, | ||
* server: { compress: true }, | ||
* main: { compress: true }, | ||
* _none: { debug: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>4</td><td><code>--compress</code></td><td><pre><code> | ||
*{ | ||
* _all: { compress: true }, | ||
* server: { compress: true }, | ||
* main: { compress: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
*</table> | ||
* | ||
* @type {string|string[]} | ||
*/ | ||
this.group = definition.group; | ||
/** | ||
* When your app has a large amount of options it makes sense to organise them in groups. | ||
* | ||
* There are two automatic groups: `_all` (contains all options) and `_none` (contains options without a `group` specified in their definition). | ||
* | ||
* ```js | ||
* module.exports = [ | ||
* { name: "verbose", group: "standard" }, | ||
* { name: "help", group: [ "standard", "main" ] }, | ||
* { name: "compress", group: [ "server", "main" ] }, | ||
* { name: "static", group: "server" }, | ||
* { name: "debug" } | ||
* ] | ||
* ``` | ||
* | ||
*<table> | ||
* <tr> | ||
* <th>#</th><th>Command Line</th><th>.parse() output</th> | ||
* </tr> | ||
* <tr> | ||
* <td>1</td><td><code>--verbose</code></td><td><pre><code> | ||
*{ | ||
* _all: { verbose: true }, | ||
* standard: { verbose: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>2</td><td><code>--debug</code></td><td><pre><code> | ||
*{ | ||
* _all: { debug: true }, | ||
* _none: { debug: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>3</td><td><code>--verbose --debug --compress</code></td><td><pre><code> | ||
*{ | ||
* _all: { | ||
* verbose: true, | ||
* debug: true, | ||
* compress: true | ||
* }, | ||
* standard: { verbose: true }, | ||
* server: { compress: true }, | ||
* main: { compress: true }, | ||
* _none: { debug: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>4</td><td><code>--compress</code></td><td><pre><code> | ||
*{ | ||
* _all: { compress: true }, | ||
* server: { compress: true }, | ||
* main: { compress: true } | ||
*} | ||
*</code></pre></td> | ||
* </tr> | ||
*</table> | ||
* | ||
* @type {string|string[]} | ||
*/ | ||
this.group = definition.group | ||
/* pick up any remaining properties */ | ||
for (var prop in definition){ | ||
if (!this[prop]) this[prop] = definition[prop]; | ||
} | ||
/* pick up any remaining properties */ | ||
for (var prop in definition) { | ||
if (!this[prop]) this[prop] = definition[prop] | ||
} | ||
} | ||
OptionDefinition.prototype.getInitialValue = function(){ | ||
if (this.multiple){ | ||
return []; | ||
} else if (this.isBoolean() || !this.type){ | ||
return true; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
OptionDefinition.prototype.isBoolean = function(){ | ||
return this.type === Boolean; | ||
}; | ||
OptionDefinition.prototype.getInitialValue = function () { | ||
if (this.multiple) { | ||
return [] | ||
} else if (this.isBoolean() || !this.type) { | ||
return true | ||
} else { | ||
return null | ||
} | ||
} | ||
OptionDefinition.prototype.isBoolean = function () { | ||
return this.type === Boolean | ||
} |
@@ -1,6 +0,7 @@ | ||
"use strict"; | ||
var a = require("array-tools"); | ||
var util = require("util"); | ||
var option = require("./option"); | ||
var Definition = require("./definition"); | ||
'use strict' | ||
var a = require('array-tools') | ||
var util = require('util') | ||
var option = require('./option') | ||
var Definition = require('./definition') | ||
var t = require('typical') | ||
@@ -11,80 +12,80 @@ /** | ||
*/ | ||
module.exports = Definitions; | ||
module.exports = Definitions | ||
function Definitions(definitions){ | ||
definitions = a.arrayify(definitions); | ||
a.call(this, definitions.map(function(def){ | ||
return new Definition(def); | ||
})); | ||
function Definitions (definitions) { | ||
definitions = a.arrayify(definitions) | ||
a.call(this, definitions.map(function (def) { | ||
return new Definition(def) | ||
})) | ||
} | ||
util.inherits(Definitions, a); | ||
util.inherits(Definitions, a) | ||
Definitions.prototype.validate = function(argv){ | ||
var self = this; | ||
var someHaveNoName = this.some(function(def){ | ||
return !def.name; | ||
}); | ||
if (someHaveNoName){ | ||
return "Invalid option definitions: the `name` property is required on each definition"; | ||
Definitions.prototype.validate = function (argv) { | ||
var self = this | ||
var someHaveNoName = this.some(function (def) { | ||
return !def.name | ||
}) | ||
if (someHaveNoName) { | ||
return 'Invalid option definitions: the `name` property is required on each definition' | ||
} | ||
var someDontHaveFunctionType = this.some(function (def) { | ||
return def.type && typeof def.type !== 'function' | ||
}) | ||
if (someDontHaveFunctionType) { | ||
return 'Invalid option definitions: the `type` property must be a setter fuction (default: `Boolean`)' | ||
} | ||
var invalidOption | ||
var optionWithoutDefinition = a(argv).where(option.isOption.bind(option)).exists(function (arg) { | ||
if (self.get(arg) === undefined) { | ||
invalidOption = arg | ||
return true | ||
} | ||
}) | ||
if (optionWithoutDefinition) { | ||
return 'Invalid option: ' + invalidOption | ||
} | ||
} | ||
var someDontHaveFunctionType = this.some(function(def){ | ||
return def.type && typeof def.type !== "function"; | ||
}); | ||
if (someDontHaveFunctionType){ | ||
return "Invalid option definitions: the `type` property must be a setter fuction (default: `Boolean`)"; | ||
Definitions.prototype.createOutput = function () { | ||
var output = {} | ||
this.forEach(function (def) { | ||
if (t.isDefined(def.defaultValue)) output[def.name] = def.defaultValue | ||
if (Array.isArray(output[def.name])) { | ||
output[def.name]._initial = true | ||
} | ||
var invalidOption; | ||
var optionWithoutDefinition = a(argv).where(option.isOption.bind(option)).exists(function(arg){ | ||
if (self.get(arg) === undefined){ | ||
invalidOption = arg; | ||
return true; | ||
} | ||
}); | ||
if (optionWithoutDefinition){ | ||
return "Invalid option: " + invalidOption; | ||
} | ||
}; | ||
}) | ||
return output | ||
} | ||
Definitions.prototype.createOutput = function(){ | ||
var output = {}; | ||
this.forEach(function(def){ | ||
if (def.defaultValue) output[def.name] = def.defaultValue; | ||
if (Array.isArray(output[def.name])){ | ||
output[def.name]._initial = true; | ||
} | ||
}); | ||
return output; | ||
}; | ||
Definitions.prototype.get = function (arg) { | ||
return option.short.test(arg) | ||
? this.findWhere({ alias: option.short.name(arg) }) | ||
: this.findWhere({ name: option.long.name(arg) }) | ||
} | ||
Definitions.prototype.get = function(arg){ | ||
return option.short.test(arg) | ||
? this.findWhere({ alias: option.short.name(arg) }) | ||
: this.findWhere({ name: option.long.name(arg) }); | ||
}; | ||
Definitions.prototype.getDefault = function () { | ||
return this.findWhere({ defaultOption: true }) | ||
} | ||
Definitions.prototype.getDefault = function(){ | ||
return this.findWhere({ defaultOption: true }); | ||
}; | ||
Definitions.prototype.isGrouped = function () { | ||
return this.some(function (def) { | ||
return def.group | ||
}) | ||
} | ||
Definitions.prototype.isGrouped = function(){ | ||
return this.some(function(def){ | ||
return def.group; | ||
}); | ||
}; | ||
Definitions.prototype.whereGrouped = function () { | ||
return this.where(containsValidGroup).val() | ||
} | ||
Definitions.prototype.whereNotGrouped = function () { | ||
return this.where(function (def) { | ||
return !containsValidGroup(def) | ||
}).val() | ||
} | ||
Definitions.prototype.whereGrouped = function(){ | ||
return this.where(containsValidGroup).val(); | ||
}; | ||
Definitions.prototype.whereNotGrouped = function(){ | ||
return this.where(function(def){ | ||
return !containsValidGroup(def); | ||
}).val(); | ||
}; | ||
function containsValidGroup(def){ | ||
return a.arrayify(def.group).some(function(group){ | ||
return group; | ||
}); | ||
function containsValidGroup (def) { | ||
return a.arrayify(def.group).some(function (group) { | ||
return group | ||
}) | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict' | ||
@@ -8,22 +8,22 @@ /** | ||
function Arg(re){ | ||
this.re = re; | ||
function Arg (re) { | ||
this.re = re | ||
} | ||
Arg.prototype.name = function(arg){ | ||
return arg.match(this.re)[1]; | ||
}; | ||
Arg.prototype.test = function(arg){ | ||
return this.re.test(arg); | ||
}; | ||
Arg.prototype.name = function (arg) { | ||
return arg.match(this.re)[1] | ||
} | ||
Arg.prototype.test = function (arg) { | ||
return this.re.test(arg) | ||
} | ||
var option = { | ||
short: new Arg(/^-(\w)$/), | ||
long: new Arg(/^--([\w-]+)/), | ||
combined: new Arg(/^-(\w{2,})$/), | ||
isOption: function(arg){ | ||
return this.short.test(arg) || this.long.test(arg); | ||
}, | ||
optEquals: new Arg(/^(--[\w-]+)=(.*)/) | ||
}; | ||
short: new Arg(/^-(\w)$/), | ||
long: new Arg(/^--([\w-]+)/), | ||
combined: new Arg(/^-(\w{2,})$/), | ||
isOption: function (arg) { | ||
return this.short.test(arg) || this.long.test(arg) | ||
}, | ||
optEquals: new Arg(/^(--[\w-]+)=(.*)/) | ||
} | ||
module.exports = option; | ||
module.exports = option |
{ | ||
"name": "command-line-args", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A library to collect command-line args and generate a usage guide.", | ||
@@ -9,4 +9,5 @@ "repository": "https://github.com/75lb/command-line-args.git", | ||
"scripts": { | ||
"test": "tape test/*.js", | ||
"docs": "jsdoc2md -l off -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
"test": "standard && tape test/*.js", | ||
"docs": "jsdoc2md -l off -t jsdoc2md/README.hbs lib/*.js > README.md; echo", | ||
"cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf coverage; echo" | ||
}, | ||
@@ -28,3 +29,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"coveralls": "^2.11.4", | ||
"istanbul": "^0.3.21", | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"standard": "^5.3.1", | ||
"tape": "^4", | ||
@@ -31,0 +35,0 @@ "test-value": "^1.0.0" |
[![view on npm](http://img.shields.io/npm/v/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
[![npm module downloads per month](http://img.shields.io/npm/dm/command-line-args.svg)](https://www.npmjs.org/package/command-line-args) | ||
[![Build Status](https://travis-ci.org/75lb/command-line-args.svg?branch=rewrite)](https://travis-ci.org/75lb/command-line-args) | ||
[![Coverage Status](https://coveralls.io/repos/75lb/command-line-args/badge.svg?branch=master&service=github)](https://coveralls.io/github/75lb/command-line-args?branch=master) | ||
[![Dependency Status](https://david-dm.org/75lb/command-line-args.svg)](https://david-dm.org/75lb/command-line-args) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
@@ -114,3 +116,3 @@ # command-line-args | ||
```js | ||
var commandLineArgs = require("command-line-args"); | ||
var commandLineArgs = require("command-line-args") | ||
var cli = commandLineArgs([ | ||
@@ -120,3 +122,3 @@ { name: "file" }, | ||
{ name: "depth"} | ||
]); | ||
]) | ||
``` | ||
@@ -192,8 +194,8 @@ <a name="module_command-line-args--CommandLineArgs+parse"></a> | ||
```js | ||
var fs = require("fs"); | ||
var fs = require("fs") | ||
function FileDetails(filename){ | ||
if (!(this instanceof FileDetails)) return new FileDetails(filename); | ||
this.filename = filename; | ||
this.exists = fs.existsSync(filename); | ||
if (!(this instanceof FileDetails)) return new FileDetails(filename) | ||
this.filename = filename | ||
this.exists = fs.existsSync(filename) | ||
} | ||
@@ -204,3 +206,3 @@ | ||
{ name: "depth", type: Number } | ||
]; | ||
] | ||
``` | ||
@@ -212,3 +214,3 @@ | ||
The `--depth` option expects a `Number`. If no value was set, you will receive `null`. | ||
The `--depth` option expects a `Number`. If no value was set, you will receive `null`. | ||
@@ -246,3 +248,3 @@ | # | Command line args | .parse() output | | ||
{ name: "files", type: String, multiple: true } | ||
]; | ||
] | ||
``` | ||
@@ -264,3 +266,3 @@ | ||
{ name: "files", type: String, multiple: true, defaultOption: true } | ||
]; | ||
] | ||
``` | ||
@@ -283,3 +285,3 @@ | ||
{ name: "max", type: Number, defaultValue: 3 } | ||
]; | ||
] | ||
``` | ||
@@ -296,3 +298,3 @@ | ||
### option.group : <code>string</code> | <code>Array.<string></code> | ||
When your app has a large amount of options it makes sense to organise them in groups. | ||
When your app has a large amount of options it makes sense to organise them in groups. | ||
@@ -308,3 +310,3 @@ There are two automatic groups: `_all` (contains all options) and `_none` (contains options without a `group` specified in their definition). | ||
{ name: "debug" } | ||
]; | ||
] | ||
``` | ||
@@ -318,5 +320,5 @@ | ||
<td>1</td><td><code>--verbose</code></td><td><pre><code> | ||
{ | ||
_all: { verbose: true }, | ||
standard: { verbose: true } | ||
{ | ||
_all: { verbose: true }, | ||
standard: { verbose: true } | ||
} | ||
@@ -327,5 +329,5 @@ </code></pre></td> | ||
<td>2</td><td><code>--debug</code></td><td><pre><code> | ||
{ | ||
_all: { debug: true }, | ||
_none: { debug: true } | ||
{ | ||
_all: { debug: true }, | ||
_none: { debug: true } | ||
} | ||
@@ -336,12 +338,12 @@ </code></pre></td> | ||
<td>3</td><td><code>--verbose --debug --compress</code></td><td><pre><code> | ||
{ | ||
_all: { | ||
verbose: true, | ||
debug: true, | ||
compress: true | ||
}, | ||
standard: { verbose: true }, | ||
server: { compress: true }, | ||
main: { compress: true }, | ||
_none: { debug: true } | ||
{ | ||
_all: { | ||
verbose: true, | ||
debug: true, | ||
compress: true | ||
}, | ||
standard: { verbose: true }, | ||
server: { compress: true }, | ||
main: { compress: true }, | ||
_none: { debug: true } | ||
} | ||
@@ -352,6 +354,6 @@ </code></pre></td> | ||
<td>4</td><td><code>--compress</code></td><td><pre><code> | ||
{ | ||
_all: { compress: true }, | ||
server: { compress: true }, | ||
main: { compress: true } | ||
{ | ||
_all: { compress: true }, | ||
server: { compress: true }, | ||
main: { compress: true } | ||
} | ||
@@ -358,0 +360,0 @@ </code></pre></td> |
@@ -1,34 +0,34 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "verbose", alias: "v" }, | ||
{ name: "colour", alias: "c" }, | ||
{ name: "number", alias: "n" }, | ||
{ name: "dry-run", alias: "d" } | ||
]; | ||
{ name: 'verbose', alias: 'v' }, | ||
{ name: 'colour', alias: 'c' }, | ||
{ name: 'number', alias: 'n' }, | ||
{ name: 'dry-run', alias: 'd' } | ||
] | ||
test("alias: one boolean", function(t){ | ||
var argv = [ "-v" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
verbose: true | ||
}); | ||
t.end(); | ||
}); | ||
test('alias: one boolean', function (t) { | ||
var argv = [ '-v' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
verbose: true | ||
}) | ||
t.end() | ||
}) | ||
test("alias: one --this-type boolean", function(t){ | ||
var argv = [ "-d" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
"dry-run": true | ||
}); | ||
t.end(); | ||
}); | ||
test('alias: one --this-type boolean', function (t) { | ||
var argv = [ '-d' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
'dry-run': true | ||
}) | ||
t.end() | ||
}) | ||
test("alias: one boolean, one string", function(t){ | ||
var argv = [ "-v", "-c" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
verbose: true, | ||
colour: true | ||
}); | ||
t.end(); | ||
}); | ||
test('alias: one boolean, one string', function (t) { | ||
var argv = [ '-v', '-c' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
verbose: true, | ||
colour: true | ||
}) | ||
t.end() | ||
}) |
@@ -1,41 +0,40 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
test('bad input: throws when no definition.name specified', function (t) { | ||
var optionDefinitions = [ | ||
{ something: 'one' }, | ||
{ something: 'two' } | ||
] | ||
var argv = [ '--one', '--two' ] | ||
t.throws(function () { | ||
cliArgs(optionDefinitions).parse(argv) | ||
}, /invalid/i) | ||
t.end() | ||
}) | ||
test("bad input: throws when no definition.name specified", function(t){ | ||
var optionDefinitions = [ | ||
{ something: "one" }, | ||
{ something: "two" }, | ||
]; | ||
var argv = [ "--one", "--two" ]; | ||
t.throws(function(){ | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
}, /invalid/i); | ||
t.end(); | ||
}); | ||
test('bad input: handles missing option value', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'colour', type: String }, | ||
{ name: 'files' } | ||
] | ||
t.deepEqual(cliArgs(optionDefinitions).parse([ '--colour' ]), { | ||
colour: null | ||
}) | ||
t.deepEqual(cliArgs(optionDefinitions).parse([ '--colour', '--files', 'yeah' ]), { | ||
colour: null, | ||
files: 'yeah' | ||
}) | ||
t.end() | ||
}) | ||
test("bad input: handles missing option value", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "colour", type: String }, | ||
{ name: "files" } | ||
]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse([ "--colour" ]), { | ||
colour: null | ||
}); | ||
t.deepEqual(cliArgs(optionDefinitions).parse([ "--colour", "--files", "yeah" ]), { | ||
colour: null, | ||
files: "yeah" | ||
}); | ||
t.end(); | ||
}); | ||
test("handles arrays with relative paths", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "colours", type: String, multiple: true } | ||
]; | ||
var argv = [ "--colours", "../what", "../ever" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
colours: [ "../what", "../ever" ] | ||
}); | ||
t.end(); | ||
}); | ||
test('handles arrays with relative paths', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'colours', type: String, multiple: true } | ||
] | ||
var argv = [ '--colours', '../what', '../ever' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
colours: [ '../what', '../ever' ] | ||
}) | ||
t.end() | ||
}) |
@@ -1,79 +0,79 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
test("defaultOption: string", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "files", defaultOption: true } | ||
]; | ||
var argv = [ "file1", "file2" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
files: "file2" | ||
}); | ||
t.end(); | ||
}); | ||
test('defaultOption: string', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'files', defaultOption: true } | ||
] | ||
var argv = [ 'file1', 'file2' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
files: 'file2' | ||
}) | ||
t.end() | ||
}) | ||
test("defaultOption: multiple string", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "files", defaultOption: true, multiple: true } | ||
]; | ||
var argv = [ "file1", "file2" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
files: [ "file1", "file2" ] | ||
}); | ||
t.end(); | ||
}); | ||
test('defaultOption: multiple string', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'files', defaultOption: true, multiple: true } | ||
] | ||
var argv = [ 'file1', 'file2' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
files: [ 'file1', 'file2' ] | ||
}) | ||
t.end() | ||
}) | ||
test("defaultOption: after a boolean", function(t){ | ||
var cli = cliArgs([ | ||
{ name: "one", type: Boolean }, | ||
{ name: "two", defaultOption: true } | ||
]); | ||
t.deepEqual( | ||
cli.parse([ "--one", "sfsgf" ]), | ||
{ one: true, two: "sfsgf" } | ||
); | ||
test('defaultOption: after a boolean', function (t) { | ||
var cli = cliArgs([ | ||
{ name: 'one', type: Boolean }, | ||
{ name: 'two', defaultOption: true } | ||
]) | ||
t.deepEqual( | ||
cli.parse([ '--one', 'sfsgf' ]), | ||
{ one: true, two: 'sfsgf' } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test("defaultOption: multiple defaultOption values between other arg/value pairs", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "one" }, | ||
{ name: "two" }, | ||
{ name: "files", defaultOption: true, multiple: true } | ||
]; | ||
var argv = [ "--one", "1", "file1", "file2", "--two", "2" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
one: "1", | ||
two: "2", | ||
files: [ "file1", "file2" ] | ||
}); | ||
t.end(); | ||
}); | ||
test('defaultOption: multiple defaultOption values between other arg/value pairs', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'one' }, | ||
{ name: 'two' }, | ||
{ name: 'files', defaultOption: true, multiple: true } | ||
] | ||
var argv = [ '--one', '1', 'file1', 'file2', '--two', '2' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
one: '1', | ||
two: '2', | ||
files: [ 'file1', 'file2' ] | ||
}) | ||
t.end() | ||
}) | ||
test("defaultOption: multiple defaultOption values between other arg/value pairs 2", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "one", type: Boolean }, | ||
{ name: "two" }, | ||
{ name: "files", defaultOption: true, multiple: true } | ||
]; | ||
var argv = [ "file0", "--one", "file1", "--files", "file2", "--two", "2", "file3" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
one: true, | ||
two: "2", | ||
files: [ "file0", "file1", "file2", "file3" ] | ||
}); | ||
t.end(); | ||
}); | ||
test('defaultOption: multiple defaultOption values between other arg/value pairs 2', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'one', type: Boolean }, | ||
{ name: 'two' }, | ||
{ name: 'files', defaultOption: true, multiple: true } | ||
] | ||
var argv = [ 'file0', '--one', 'file1', '--files', 'file2', '--two', '2', 'file3' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
one: true, | ||
two: '2', | ||
files: [ 'file0', 'file1', 'file2', 'file3' ] | ||
}) | ||
t.end() | ||
}) | ||
test("defaultOption: floating args present but no defaultOption", function(t){ | ||
var cli = cliArgs([ | ||
{ name: "one", type: Boolean } | ||
]); | ||
t.deepEqual( | ||
cli.parse([ "aaa", "--one", "aaa", "aaa" ]), | ||
{ one: true } | ||
); | ||
test('defaultOption: floating args present but no defaultOption', function (t) { | ||
var cli = cliArgs([ | ||
{ name: 'one', type: Boolean } | ||
]) | ||
t.deepEqual( | ||
cli.parse([ 'aaa', '--one', 'aaa', 'aaa' ]), | ||
{ one: true } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,37 +0,51 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
test("default value", function(t){ | ||
t.deepEqual(cliArgs([ { name: "one" }, { name: "two", defaultValue: "two" } ]).parse([ "--one", "1" ]), { | ||
one: "1", | ||
two: "two" | ||
}); | ||
t.deepEqual(cliArgs([{ name: "two", defaultValue: "two" }]).parse([]), { | ||
two: "two" | ||
}); | ||
t.deepEqual(cliArgs([{ name: "two", defaultValue: "two" }]).parse([ "--two", "zwei" ]), { | ||
two: "zwei" | ||
}); | ||
t.deepEqual( | ||
cliArgs([{ name: "two", multiple: true, defaultValue: ["two", "zwei"] }]).parse([ "--two", "duo" ]), | ||
{ two: [ "duo" ] } | ||
); | ||
test('default value', function (t) { | ||
t.deepEqual(cliArgs([ { name: 'one' }, { name: 'two', defaultValue: 'two' } ]).parse([ '--one', '1' ]), { | ||
one: '1', | ||
two: 'two' | ||
}) | ||
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }]).parse([]), { | ||
two: 'two' | ||
}) | ||
t.deepEqual(cliArgs([{ name: 'two', defaultValue: 'two' }]).parse([ '--two', 'zwei' ]), { | ||
two: 'zwei' | ||
}) | ||
t.deepEqual( | ||
cliArgs([{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }]).parse([ '--two', 'duo' ]), | ||
{ two: [ 'duo' ] } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test("default value", function(t){ | ||
var defs = [{ name: "two", multiple: true, defaultValue: ["two", "zwei"] }]; | ||
var result = cliArgs(defs).parse([ ]); | ||
t.deepEqual(result, { two: [ "two", "zwei" ] }); | ||
t.end(); | ||
}); | ||
test('default value', function (t) { | ||
var defs = [{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }] | ||
var result = cliArgs(defs).parse([]) | ||
t.deepEqual(result, { two: [ 'two', 'zwei' ] }) | ||
t.end() | ||
}) | ||
test("default value: array as defaultOption", function(t){ | ||
var defs = [ | ||
{ name: "two", multiple: true, defaultValue: ["two", "zwei"], defaultOption: true } | ||
]; | ||
var argv = [ "duo" ]; | ||
t.deepEqual(cliArgs(defs).parse(argv), { two: [ "duo" ] }); | ||
t.end(); | ||
}); | ||
test('default value: array as defaultOption', function (t) { | ||
var defs = [ | ||
{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'], defaultOption: true } | ||
] | ||
var argv = [ 'duo' ] | ||
t.deepEqual(cliArgs(defs).parse(argv), { two: [ 'duo' ] }) | ||
t.end() | ||
}) | ||
test('default value: falsy default values', function (t) { | ||
var defs = [ | ||
{ name: 'one', defaultValue: 0 }, | ||
{ name: 'two', defaultValue: false } | ||
] | ||
var argv = [] | ||
t.deepEqual(cliArgs(defs).parse(argv), { | ||
one: 0, | ||
two: false | ||
}) | ||
t.end() | ||
}) |
@@ -1,10 +0,10 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
test("detect process.argv: should automatically remove first two argv items", function(t){ | ||
process.argv = [ "node", "filename", "--one", "eins" ]; | ||
t.deepEqual(cliArgs({ name: "one" }).parse(process.argv), { | ||
one: "eins" | ||
}); | ||
t.end(); | ||
}); | ||
test('detect process.argv: should automatically remove first two argv items', function (t) { | ||
process.argv = [ 'node', 'filename', '--one', 'eins' ] | ||
t.deepEqual(cliArgs({ name: 'one' }).parse(process.argv), { | ||
one: 'eins' | ||
}) | ||
t.end() | ||
}) |
@@ -1,26 +0,26 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "flagA", alias: "a" }, | ||
{ name: "flagB", alias: "b" }, | ||
{ name: "three", alias: "c" } | ||
]; | ||
{ name: 'flagA', alias: 'a' }, | ||
{ name: 'flagB', alias: 'b' }, | ||
{ name: 'three', alias: 'c' } | ||
] | ||
test("getOpts: two flags, one option", function(t){ | ||
var argv = [ "-abc", "yeah" ]; | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
flagA: true, | ||
flagB: true, | ||
three: "yeah" | ||
}); | ||
t.end(); | ||
}); | ||
test('getOpts: two flags, one option', function (t) { | ||
var argv = [ '-abc', 'yeah' ] | ||
t.deepEqual(cliArgs(optionDefinitions).parse(argv), { | ||
flagA: true, | ||
flagB: true, | ||
three: 'yeah' | ||
}) | ||
t.end() | ||
}) | ||
test("getOpts: non-existent options", function(t){ | ||
var argv = [ "-sdf" ]; | ||
t.throws(function(){ | ||
cliArgs(optionDefinitions).parse(argv); | ||
}, /invalid/i); | ||
t.end(); | ||
}); | ||
test('getOpts: non-existent options', function (t) { | ||
var argv = [ '-sdf' ] | ||
t.throws(function () { | ||
cliArgs(optionDefinitions).parse(argv) | ||
}, /invalid/i) | ||
t.end() | ||
}) |
@@ -1,60 +0,60 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", group: "a" }, | ||
{ name: "two", group: "a" }, | ||
{ name: "three", group: "b" }, | ||
]; | ||
{ name: 'one', group: 'a' }, | ||
{ name: 'two', group: 'a' }, | ||
{ name: 'three', group: 'b' } | ||
] | ||
test("groups", function(t){ | ||
var cli = cliArgs(optionDefinitions); | ||
t.deepEqual(cli.parse([ "--one", "1", "--two", "2", "--three", "3" ]), { | ||
a: { | ||
one: "1", | ||
two: "2" | ||
}, | ||
b: { | ||
three: "3" | ||
}, | ||
_all: { | ||
one: "1", | ||
two: "2", | ||
three: "3" | ||
} | ||
}); | ||
test('groups', function (t) { | ||
var cli = cliArgs(optionDefinitions) | ||
t.deepEqual(cli.parse([ '--one', '1', '--two', '2', '--three', '3' ]), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
b: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
} | ||
}) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test("groups: multiple and _none", function(t){ | ||
var optionDefinitions = [ | ||
{ name: "one", group: ["a", "f"] }, | ||
{ name: "two", group: ["a", "g"] }, | ||
{ name: "three" }, | ||
]; | ||
test('groups: multiple and _none', function (t) { | ||
var optionDefinitions = [ | ||
{ name: 'one', group: ['a', 'f'] }, | ||
{ name: 'two', group: ['a', 'g'] }, | ||
{ name: 'three' } | ||
] | ||
var cli = cliArgs(optionDefinitions); | ||
t.deepEqual(cli.parse([ "--one", "1", "--two", "2", "--three", "3" ]), { | ||
a: { | ||
one: "1", | ||
two: "2" | ||
}, | ||
f: { | ||
one: "1", | ||
}, | ||
g: { | ||
two: "2" | ||
}, | ||
_none: { | ||
three: "3" | ||
}, | ||
_all: { | ||
one: "1", | ||
two: "2", | ||
three: "3" | ||
} | ||
}); | ||
var cli = cliArgs(optionDefinitions) | ||
t.deepEqual(cli.parse([ '--one', '1', '--two', '2', '--three', '3' ]), { | ||
a: { | ||
one: '1', | ||
two: '2' | ||
}, | ||
f: { | ||
one: '1' | ||
}, | ||
g: { | ||
two: '2' | ||
}, | ||
_none: { | ||
three: '3' | ||
}, | ||
_all: { | ||
one: '1', | ||
two: '2', | ||
three: '3' | ||
} | ||
}) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,27 +0,27 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", type: Number } | ||
]; | ||
{ name: 'one', type: Number } | ||
] | ||
test("missing def: set a value without option definition", function(t){ | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one", "1" ]), | ||
{ one: 1 } | ||
); | ||
test('missing def: set a value without option definition', function (t) { | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one', '1' ]), | ||
{ one: 1 } | ||
) | ||
t.throws(function(){ | ||
cliArgs(optionDefinitions).parse([ "--one", "--two" ]); | ||
}, /invalid/i); | ||
t.throws(function () { | ||
cliArgs(optionDefinitions).parse([ '--one', '--two' ]) | ||
}, /invalid/i) | ||
t.throws(function(){ | ||
cliArgs(optionDefinitions).parse([ "--one", "2", "--two", "two" ]); | ||
}, /invalid/i); | ||
t.throws(function () { | ||
cliArgs(optionDefinitions).parse([ '--one', '2', '--two', 'two' ]) | ||
}, /invalid/i) | ||
t.throws(function(){ | ||
cliArgs(optionDefinitions).parse([ "-a", "2" ]); | ||
}, /invalid/i); | ||
t.throws(function () { | ||
cliArgs(optionDefinitions).parse([ '-a', '2' ]) | ||
}, /invalid/i) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,20 +0,20 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", alias: "1" }, | ||
{ name: "two", alias: "2" }, | ||
{ name: "three", alias: "3" }, | ||
{ name: "four", alias: "4" } | ||
]; | ||
{ name: 'one', alias: '1' }, | ||
{ name: 'two', alias: '2' }, | ||
{ name: 'three', alias: '3' }, | ||
{ name: 'four', alias: '4' } | ||
] | ||
test("name-alias-mix: one of each", function(t){ | ||
var argv = [ "--one", "-2", "--three" ]; | ||
var cli = cliArgs(optionDefinitions); | ||
var result = cli.parse(argv); | ||
t.strictEqual(result.one, true); | ||
t.strictEqual(result.two, true); | ||
t.strictEqual(result.three, true); | ||
t.strictEqual(result.four, undefined); | ||
t.end(); | ||
}); | ||
test('name-alias-mix: one of each', function (t) { | ||
var argv = [ '--one', '-2', '--three' ] | ||
var cli = cliArgs(optionDefinitions) | ||
var result = cli.parse(argv) | ||
t.strictEqual(result.one, true) | ||
t.strictEqual(result.two, true) | ||
t.strictEqual(result.three, true) | ||
t.strictEqual(result.four, undefined) | ||
t.end() | ||
}) |
@@ -1,18 +0,18 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one" }, | ||
{ name: "two" }, | ||
{ name: "three" } | ||
]; | ||
{ name: 'one' }, | ||
{ name: 'two' }, | ||
{ name: 'three' } | ||
] | ||
test("name-alias-mix: one of each", function(t){ | ||
var argv = [ "--one=1", "--two", "2", "--three=3" ]; | ||
var cli = cliArgs(optionDefinitions); | ||
var result = cli.parse(argv); | ||
t.strictEqual(result.one, "1"); | ||
t.strictEqual(result.two, "2"); | ||
t.strictEqual(result.three, "3"); | ||
t.end(); | ||
}); | ||
test('name-alias-mix: one of each', function (t) { | ||
var argv = [ '--one=1', '--two', '2', '--three=3' ] | ||
var cli = cliArgs(optionDefinitions) | ||
var result = cli.parse(argv) | ||
t.strictEqual(result.one, '1') | ||
t.strictEqual(result.two, '2') | ||
t.strictEqual(result.three, '3') | ||
t.end() | ||
}) |
@@ -1,15 +0,15 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "array", type: Boolean, multiple: true }, | ||
]; | ||
{ name: 'array', type: Boolean, multiple: true } | ||
] | ||
test("number multiple: 1", function(t){ | ||
var argv = [ "--array", "--array", "--array" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
array: [ true, true, true ] | ||
}); | ||
t.end(); | ||
}); | ||
test('number multiple: 1', function (t) { | ||
var argv = [ '--array', '--array', '--array' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
array: [ true, true, true ] | ||
}) | ||
t.end() | ||
}) |
@@ -1,28 +0,28 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", type: Boolean } | ||
]; | ||
{ name: 'one', type: Boolean } | ||
] | ||
test("type-boolean: different values", function(t){ | ||
var cli = cliArgs(optionDefinitions); | ||
t.deepEqual( | ||
cli.parse([ "--one" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cli.parse([ "--one", "true" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cli.parse([ "--one", "false" ]), | ||
{ one: true } | ||
); | ||
t.deepEqual( | ||
cli.parse([ "--one", "sfsgf" ]), | ||
{ one: true } | ||
); | ||
test('type-boolean: different values', function (t) { | ||
var cli = cliArgs(optionDefinitions) | ||
t.deepEqual( | ||
cli.parse([ '--one' ]), | ||
{ one: true } | ||
) | ||
t.deepEqual( | ||
cli.parse([ '--one', 'true' ]), | ||
{ one: true } | ||
) | ||
t.deepEqual( | ||
cli.parse([ '--one', 'false' ]), | ||
{ one: true } | ||
) | ||
t.deepEqual( | ||
cli.parse([ '--one', 'sfsgf' ]), | ||
{ one: true } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,25 +0,23 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ { name: "one", type: "string" } ]; | ||
test('type-invalid: invalid type values', function (t) { | ||
var argv = [ '--one', 'something' ] | ||
t.throws(function () { | ||
cliArgs([ { name: 'one', type: 'string' } ]).parse(argv) | ||
}, /invalid/i) | ||
test("type-invalid: invalid type values", function(t){ | ||
var argv = [ "--one", "something" ]; | ||
t.throws(function(){ | ||
cliArgs([ { name: "one", type: "string" } ]).parse(argv) | ||
}, /invalid/i); | ||
t.throws(function(){ | ||
cliArgs([ { name: "one", type: 234 } ]).parse(argv) | ||
}, /invalid/i); | ||
t.throws(function () { | ||
cliArgs([ { name: 'one', type: 234 } ]).parse(argv) | ||
}, /invalid/i) | ||
t.throws(function(){ | ||
cliArgs([ { name: "one", type: {} } ]).parse(argv) | ||
}, /invalid/i); | ||
t.throws(function () { | ||
cliArgs([ { name: 'one', type: {} } ]).parse(argv) | ||
}, /invalid/i) | ||
t.doesNotThrow(function(){ | ||
cliArgs([ { name: "one", type: function(){} } ]).parse(argv) | ||
}, /invalid/i); | ||
t.doesNotThrow(function () { | ||
cliArgs([ { name: 'one', type: function () {} } ]).parse(argv) | ||
}, /invalid/i) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,54 +0,54 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one" }, | ||
{ name: "two" } | ||
]; | ||
{ name: 'one' }, | ||
{ name: 'two' } | ||
] | ||
test("name: no argv values", function(t){ | ||
var argv = [ ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, {}); | ||
t.end(); | ||
}); | ||
test('name: no argv values', function (t) { | ||
var argv = [] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, {}) | ||
t.end() | ||
}) | ||
test("name: just names, no values", function(t){ | ||
var argv = [ "--one", "--two" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
one: true, | ||
two: true | ||
}); | ||
t.end(); | ||
}); | ||
test('name: just names, no values', function (t) { | ||
var argv = [ '--one', '--two' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
one: true, | ||
two: true | ||
}) | ||
t.end() | ||
}) | ||
test("name: just names, no values, unpassed value", function(t){ | ||
var argv = [ "--one", "--two" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
one: true, | ||
two: true | ||
}); | ||
t.end(); | ||
}); | ||
test('name: just names, no values, unpassed value', function (t) { | ||
var argv = [ '--one', '--two' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
one: true, | ||
two: true | ||
}) | ||
t.end() | ||
}) | ||
test("name: just names, one value, one unpassed value", function(t){ | ||
var argv = [ "--one", "one", "--two" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
one: "one", | ||
two: true | ||
}); | ||
t.end(); | ||
}); | ||
test('name: just names, one value, one unpassed value', function (t) { | ||
var argv = [ '--one', 'one', '--two' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
one: 'one', | ||
two: true | ||
}) | ||
t.end() | ||
}) | ||
test("name: just names, two values", function(t){ | ||
var argv = [ "--one", "one", "--two", "two" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
one: "one", | ||
two: "two" | ||
}); | ||
t.end(); | ||
}); | ||
test('name: just names, two values', function (t) { | ||
var argv = [ '--one', 'one', '--two', 'two' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
one: 'one', | ||
two: 'two' | ||
}) | ||
t.end() | ||
}) |
@@ -1,31 +0,30 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "array", type: Number, multiple: true }, | ||
]; | ||
{ name: 'array', type: Number, multiple: true } | ||
] | ||
test('number multiple: 1', function (t) { | ||
var argv = [ '--array', '1', '2', '3' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
array: [ 1, 2, 3 ] | ||
}) | ||
t.notDeepEqual(result, { | ||
array: [ '1', '2', '3' ] | ||
}) | ||
t.end() | ||
}) | ||
test("number multiple: 1", function(t){ | ||
var argv = [ "--array", "1", "2", "3" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
array: [ 1, 2, 3 ] | ||
}); | ||
t.notDeepEqual(result, { | ||
array: [ "1", "2", "3" ] | ||
}); | ||
t.end(); | ||
}); | ||
test("number multiple: 2", function(t){ | ||
var argv = [ "--array", "1", "--array", "2", "--array", "3" ]; | ||
var result = cliArgs(optionDefinitions).parse(argv); | ||
t.deepEqual(result, { | ||
array: [ 1, 2, 3 ] | ||
}); | ||
t.notDeepEqual(result, { | ||
array: [ "1", "2", "3" ] | ||
}); | ||
t.end(); | ||
}); | ||
test('number multiple: 2', function (t) { | ||
var argv = [ '--array', '1', '--array', '2', '--array', '3' ] | ||
var result = cliArgs(optionDefinitions).parse(argv) | ||
t.deepEqual(result, { | ||
array: [ 1, 2, 3 ] | ||
}) | ||
t.notDeepEqual(result, { | ||
array: [ '1', '2', '3' ] | ||
}) | ||
t.end() | ||
}) |
@@ -1,19 +0,19 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", type: Number } | ||
]; | ||
{ name: 'one', type: Number } | ||
] | ||
test("type-number: different values", function(t){ | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one", "1" ]), | ||
{ one: 1 } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one" ]), | ||
{ one: null } | ||
); | ||
test('type-number: different values', function (t) { | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one', '1' ]), | ||
{ one: 1 } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one' ]), | ||
{ one: null } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,25 +0,25 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "file", multiple: true, type: function(file){ | ||
return file; | ||
}} | ||
]; | ||
{ name: 'file', multiple: true, type: function (file) { | ||
return file | ||
}} | ||
] | ||
test("type-other-multiple: different values", function(t){ | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--file", "one.js" ]), | ||
{ file: [ "one.js" ] } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--file", "one.js", "two.js" ]), | ||
{ file: [ "one.js", "two.js" ] } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--file" ]), | ||
{ file: [] } | ||
); | ||
test('type-other-multiple: different values', function (t) { | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--file', 'one.js' ]), | ||
{ file: [ 'one.js' ] } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--file', 'one.js', 'two.js' ]), | ||
{ file: [ 'one.js', 'two.js' ] } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--file' ]), | ||
{ file: [] } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,21 +0,21 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "file", type: function(file){ | ||
return file; | ||
}} | ||
]; | ||
{ name: 'file', type: function (file) { | ||
return file | ||
}} | ||
] | ||
test("type-other: different values", function(t){ | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--file", "one.js" ]), | ||
{ file: "one.js" } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--file" ]), | ||
{ file: null } | ||
); | ||
test('type-other: different values', function (t) { | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--file', 'one.js' ]), | ||
{ file: 'one.js' } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--file' ]), | ||
{ file: null } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,23 +0,23 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", type: String } | ||
]; | ||
{ name: 'one', type: String } | ||
] | ||
test("type-string: different values", function(t){ | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one", "yeah" ]), | ||
{ one: "yeah" } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one" ]), | ||
{ one: null } | ||
); | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ "--one", "3" ]), | ||
{ one: "3" } | ||
); | ||
test('type-string: different values', function (t) { | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one', 'yeah' ]), | ||
{ one: 'yeah' } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one' ]), | ||
{ one: null } | ||
) | ||
t.deepEqual( | ||
cliArgs(optionDefinitions).parse([ '--one', '3' ]), | ||
{ one: '3' } | ||
) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -1,13 +0,13 @@ | ||
var test = require("tape"); | ||
var cliArgs = require("../"); | ||
var test = require('tape') | ||
var cliArgs = require('../') | ||
var optionDefinitions = [ | ||
{ name: "one", type: Boolean } | ||
]; | ||
{ name: 'one', type: Boolean } | ||
] | ||
test("type-boolean: different values", function(t){ | ||
var cli = cliArgs(optionDefinitions); | ||
var usage = cli.getUsage({ title: "test" }); | ||
t.ok(/test/.test(usage)); | ||
t.end(); | ||
}); | ||
test('type-boolean: different values', function (t) { | ||
var cli = cliArgs(optionDefinitions) | ||
var usage = cli.getUsage({ title: 'test' }) | ||
t.ok(/test/.test(usage)) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
1108
356
50181
6
39
1