command-line-args
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -6,7 +6,7 @@ var w = require("wodge"); | ||
/** | ||
@returns {Object} | ||
docs TODO | ||
*/ | ||
function parse(optionDefinitions, argv){ | ||
var shortArg = /^-\w/, | ||
longArg = /^--\w+/, | ||
var shortArg = /^-(\w)/, | ||
longArg = /^--(\w+)/, | ||
output = {}, | ||
@@ -16,34 +16,34 @@ defaultValues = [], | ||
argv = argv || process.argv.slice(2); | ||
argv = argv ? argv.slice(0) : process.argv.slice(2); | ||
while (typeof(arg = argv.shift()) !== "undefined"){ | ||
if (longArg.test(arg)){ | ||
var optionName = arg.replace(/^--/, ""); | ||
var optionDefinition = w.findWhere(optionDefinitions, { name: optionName }); | ||
if (optionDefinition){ | ||
if(optionDefinition.type === "boolean" || optionDefinition.type === Boolean){ | ||
output[optionDefinition.name] = true; | ||
} else if (argv.length) { | ||
output[optionDefinition.name] = argv.shift(); | ||
function setOutput(optionName){ | ||
var option = w.findWhere(optionDefinitions, { name: optionName }) | ||
|| w.findWhere(optionDefinitions, { alias: optionName }); | ||
if (option){ | ||
if(option.type === Boolean){ | ||
output[option.name] = true; | ||
} else if (argv.length) { | ||
if (typeof option.type === "function"){ | ||
if (option.type === Array){ | ||
output[option.name] = spliceWhile(argv, /^\w+/); | ||
} else { | ||
output[option.name] = option.type(argv.shift()); | ||
} | ||
} else { | ||
throw new Error("weird case"); | ||
output[option.name] = argv.shift(); | ||
} | ||
} else { | ||
throw new Error("Unexpected option: " + optionName); | ||
console.error(option); | ||
throw new Error("weird case"); | ||
} | ||
} else { | ||
throw new Error("Unexpected option: " + optionName); | ||
} | ||
}; | ||
while (typeof(arg = argv.shift()) !== "undefined"){ | ||
if (longArg.test(arg)){ | ||
setOutput(arg.match(longArg)[1]); | ||
} else if (shortArg.test(arg)){ | ||
var optionName = arg.replace(/^-/, ""); | ||
var optionDefinition = w.findWhere(optionDefinitions, { alias: optionName }); | ||
if (optionDefinition){ | ||
if(optionDefinition.type === "boolean" || optionDefinition.type === Boolean){ | ||
output[optionDefinition.name] = true; | ||
} else if (argv.length) { | ||
output[optionDefinition.name] = argv.shift(); | ||
} else { | ||
throw new Error("weird case"); | ||
} | ||
} else { | ||
throw new Error("Unexpected option: " + optionName); | ||
} | ||
setOutput(arg.match(shortArg)[1]); | ||
} else { | ||
@@ -71,1 +71,12 @@ defaultValues.push(arg); | ||
} | ||
function spliceWhile(array, test){ | ||
for (var i = 0; i < array.length; i++){ | ||
if (!test.test(array[i])) break; | ||
} | ||
return array.splice(0, i); | ||
} | ||
// var a = [ "--colours", "green", "red", "yellow", "--tramps", "mike", "colin" ]; | ||
// // a.shift(); | ||
// console.dir(spliceWhile(a, /^\w+/)) |
{ | ||
"name": "command-line-args", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Parse command line options", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/75lb/command-line-args.git", |
[![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=master)](https://travis-ci.org/75lb/command-line-args) | ||
[![Dependency Status](https://david-dm.org/75lb/command-line-args.svg)](https://david-dm.org/75lb/command-line-args) | ||
@@ -4,0 +5,0 @@ |
var test = require("tap").test; | ||
var parse = require("../lib/command-line-args"); | ||
var expectArgs = [ | ||
var optionDefinitions = [ | ||
{ name: "verbose", alias: "v", type: Boolean }, | ||
@@ -9,7 +9,17 @@ { name: "dry", alias: "d", type: Boolean }, | ||
{ name: "number", alias: "n", type: Number }, | ||
{ name: "files", defaultOption: true } | ||
{ name: "files", defaultOption: true }, | ||
{ name: "colours", type: Array }, | ||
{ name: "tramps", type: Array } | ||
]; | ||
// test("boolean flag option doesn't exist", function(t){ | ||
// var argv = [ "-verbose" ]; | ||
// }); | ||
test("throws on unrecognised option", function(t){ | ||
var argv = [ "-files", "clive" ]; | ||
t.throws( | ||
function(){ | ||
parse(optionDefinitions, argv) | ||
}, | ||
new Error("Unexpected option: f"), | ||
"throw test" | ||
); | ||
t.end(); | ||
}); |
@@ -5,7 +5,9 @@ var test = require("tap").test; | ||
var optionDefinitions = [ | ||
{ name: "verbose", alias: "v", type: "boolean" }, | ||
{ name: "verbose", alias: "v", type: Boolean }, | ||
{ name: "dry", alias: "d", type: Boolean }, | ||
{ name: "colour", alias: "c" }, | ||
{ name: "number", alias: "n", type: Number }, | ||
{ name: "files", defaultOption: true } | ||
{ name: "files", defaultOption: true }, | ||
{ name: "colours", type: Array }, | ||
{ name: "tramps", type: Array } | ||
]; | ||
@@ -29,1 +31,29 @@ | ||
}); | ||
test("one boolean, one string, one number", function(t){ | ||
var argv = [ "--verbose", "--colour", "red", "--number", "3" ]; | ||
var result = parse(optionDefinitions, argv); | ||
t.equal(result.verbose, true); | ||
t.equal(result.colour, "red"); | ||
t.equal(result.number, 3); | ||
t.end(); | ||
}); | ||
test("one array", function(t){ | ||
var argv = [ "--colours", "green", "red", "yellow" ]; | ||
var result = parse(optionDefinitions, argv); | ||
t.deepEqual(result, { | ||
colours: [ "green", "red", "yellow" ] | ||
}); | ||
t.end(); | ||
}); | ||
test("two arrays", function(t){ | ||
var argv = [ "--colours", "green", "red", "yellow", "--tramps", "mike", "colin" ]; | ||
var result = parse(optionDefinitions, argv); | ||
t.deepEqual(result, { | ||
colours: [ "green", "red", "yellow" ], | ||
tramps: [ "mike", "colin" ] | ||
}); | ||
t.end(); | ||
}); |
@@ -5,3 +5,3 @@ var test = require("tap").test; | ||
var optionDefinitions = [ | ||
{ name: "verbose", alias: "v", type: "boolean" }, | ||
{ name: "verbose", alias: "v", type: Boolean }, | ||
{ name: "dry", alias: "d", type: Boolean }, | ||
@@ -15,9 +15,8 @@ { name: "colour", alias: "c" }, | ||
var argv = [ "--verbose", "-d", "--colour", "red", "--number", 3 ]; | ||
t.deepEqual(parse(optionDefinitions, argv), { | ||
verbose: true, | ||
dry: true, | ||
colour: "red", | ||
number: 3 | ||
}); | ||
var result = parse(optionDefinitions, argv); | ||
t.equal(result.verbose, true); | ||
t.equal(result.dry, true); | ||
t.equal(result.colour, "red"); | ||
t.equal(result.number, 3); | ||
t.end(); | ||
}); |
@@ -5,3 +5,3 @@ var test = require("tap").test; | ||
var optionDefinitions = [ | ||
{ name: "verbose", alias: "v", type: "boolean" }, | ||
{ name: "verbose", alias: "v", type: Boolean }, | ||
{ name: "dry", alias: "d", type: Boolean }, | ||
@@ -8,0 +8,0 @@ { name: "colour", alias: "c" }, |
7728
9
206
11