Permalink
Browse files

Command-line args should allow any order.

  • Loading branch information...
1 parent 430937e commit c5bc006d825acd7dc815a936ce94eec4d468367c @mde mde committed Jan 11, 2012
Showing with 67 additions and 60 deletions.
  1. +6 −5 lib/parseargs.js
  2. +4 −5 lib/program.js
  3. +57 −50 tests/parseargs.js
View
@@ -92,7 +92,6 @@ parseargs.Parser.prototype = new function () {
// If the opt requires a value, see if we can get a value from the
// next arg, or infer true from no-arg -- if it's followed by another
// opt, throw an error
- cmds = [];
if (argItem.expectValue) {
opts[argItem.full] = _trueOrNextVal(argParts, args);
if (!opts[argItem.full]) {
@@ -105,7 +104,7 @@ parseargs.Parser.prototype = new function () {
}
}
else {
- cmds.push(arg);
+ cmds.unshift(arg);
}
}
@@ -123,9 +122,11 @@ parseargs.Parser.prototype = new function () {
}
- this.opts = opts
- this.envVars = envVars;
- this.taskNames = taskNames;
+ return {
+ opts: opts
+ , envVars: envVars
+ , taskNames: taskNames
+ };
};
};
View
@@ -112,11 +112,10 @@ Program.prototype = new (function () {
};
this.parseArgs = function (args) {
- var parser = new parseargs.Parser(optsReg);
- parser.parse(args);
- this.opts = parser.opts;
- this.taskNames = parser.taskNames;
- this.envVars = parser.envVars;
+ var result = (new parseargs.Parser(optsReg)).parse(args);
+ this.opts = result.opts;
+ this.taskNames = result.taskNames;
+ this.envVars = result.envVars;
};
this.preemptiveOption = function () {
View
@@ -30,87 +30,94 @@ var parseargs = require('../lib/parseargs')
}
]
, p = new parseargs.Parser(optsReg)
- , z = function (s) { return s.split(' '); };
+ , z = function (s) { return s.split(' '); }
+ , res;
// Long preemptive opt and val with equal-sign, ignore further opts
-p.parse(z('--tasks=foo --jakefile=asdf'));
-assert.equal('foo', p.opts.tasks);
-assert.equal(undefined, p.opts.jakefile);
+res = p.parse(z('--tasks=foo --jakefile=asdf'));
+assert.equal('foo', res.opts.tasks);
+assert.equal(undefined, res.opts.jakefile);
// Long preemptive opt and val without equal-sign, ignore further opts
-p.parse(z('--tasks foo --jakefile=asdf'));
-assert.equal('foo', p.opts.tasks);
-assert.equal(undefined, p.opts.jakefile);
+res = p.parse(z('--tasks foo --jakefile=asdf'));
+assert.equal('foo', res.opts.tasks);
+assert.equal(undefined, res.opts.jakefile);
// Long preemptive opt and no val, ignore further opts
-p.parse(z('--tasks --jakefile=asdf'));
-assert.equal(true, p.opts.tasks);
-assert.equal(undefined, p.opts.jakefile);
+res = p.parse(z('--tasks --jakefile=asdf'));
+assert.equal(true, res.opts.tasks);
+assert.equal(undefined, res.opts.jakefile);
// Preemptive opt with no val, should be true
-p.parse(z('-T'));
-assert.equal(true, p.opts.tasks);
+res = p.parse(z('-T'));
+assert.equal(true, res.opts.tasks);
// Preemptive opt with no val, should be true and ignore further opts
-p.parse(z('-T -f'));
-assert.equal(true, p.opts.tasks);
-assert.equal(undefined, p.opts.jakefile);
+res = p.parse(z('-T -f'));
+assert.equal(true, res.opts.tasks);
+assert.equal(undefined, res.opts.jakefile);
// Preemptive opt with val, should be val
-p.parse(z('-T zoobie -f foo/bar/baz'));
-assert.equal('zoobie', p.opts.tasks);
-assert.equal(undefined, p.opts.jakefile);
+res = p.parse(z('-T zoobie -f foo/bar/baz'));
+assert.equal('zoobie', res.opts.tasks);
+assert.equal(undefined, res.opts.jakefile);
// -f expects a value, -t does not (howdy is task-name)
-p.parse(z('-f zoobie -t howdy'));
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(true, p.opts.trace);
-assert.equal('howdy', p.taskNames[0]);
+res = p.parse(z('-f zoobie -t howdy'));
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(true, res.opts.trace);
+assert.equal('howdy', res.taskNames[0]);
+
+// Different order, -f expects a value, -t does not (howdy is task-name)
+res = p.parse(z('-f zoobie howdy -t'));
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(true, res.opts.trace);
+assert.equal('howdy', res.taskNames[0]);
// -f expects a value, -t does not (foo=bar is env var)
-p.parse(z('-f zoobie -t foo=bar'));
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(true, p.opts.trace);
-assert.equal('bar', p.envVars.foo);
-assert.equal(undefined, p.taskName);
+res = p.parse(z('-f zoobie -t foo=bar'));
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(true, res.opts.trace);
+assert.equal('bar', res.envVars.foo);
+assert.equal(undefined, res.taskName);
// -f expects a value, -t does not (foo=bar is env-var, task-name follows)
-p.parse(z('-f zoobie -t howdy foo=bar'));
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(true, p.opts.trace);
-assert.equal('bar', p.envVars.foo);
-assert.equal('howdy', p.taskNames[0]);
+res = p.parse(z('-f zoobie -t howdy foo=bar'));
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(true, res.opts.trace);
+assert.equal('bar', res.envVars.foo);
+assert.equal('howdy', res.taskNames[0]);
// -t does not expect a value, -f does (throw howdy away)
-p.parse(z('-t howdy -f zoobie'));
-assert.equal(true, p.opts.trace);
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(undefined, p.taskName);
+res = p.parse(z('-t howdy -f zoobie'));
+assert.equal(true, res.opts.trace);
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(undefined, res.taskName);
// --trace does not expect a value, -f does (throw howdy away)
-p.parse(z('--trace howdy --jakefile zoobie'));
-assert.equal(true, p.opts.trace);
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(undefined, p.taskName);
+res = p.parse(z('--trace howdy --jakefile zoobie'));
+assert.equal(true, res.opts.trace);
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(undefined, res.taskName);
// --trace does not expect a value, -f does (throw howdy away)
-p.parse(z('--trace=howdy --jakefile=zoobie'));
-assert.equal(true, p.opts.trace);
-assert.equal('zoobie', p.opts.jakefile);
-assert.equal(undefined, p.taskName);
+res = p.parse(z('--trace=howdy --jakefile=zoobie'));
+assert.equal(true, res.opts.trace);
+assert.equal('zoobie', res.opts.jakefile);
+assert.equal(undefined, res.taskName);
/*
// Task-name with positional args
-p.parse(z('foo:bar[asdf,qwer]'));
+res = p.parse(z('foo:bar[asdf,qwer]'));
assert.equal('asdf', p.taskArgs[0]);
assert.equal('qwer', p.taskArgs[1]);
// Opts, env vars, task-name with positional args
-p.parse(z('-f ./tests/Jakefile -t default[asdf,qwer] foo=bar'));
-assert.equal('./tests/Jakefile', p.opts.jakefile);
-assert.equal(true, p.opts.trace);
-assert.equal('bar', p.envVars.foo);
-assert.equal('default', p.taskName);
+res = p.parse(z('-f ./tests/Jakefile -t default[asdf,qwer] foo=bar'));
+assert.equal('./tests/Jakefile', res.opts.jakefile);
+assert.equal(true, res.opts.trace);
+assert.equal('bar', res.envVars.foo);
+assert.equal('default', res.taskName);
assert.equal('asdf', p.taskArgs[0]);
assert.equal('qwer', p.taskArgs[1]);
*/

0 comments on commit c5bc006

Please sign in to comment.