Browse files

Merge pull request #42 from hpaulj/corrected_zip

Mismatch between py zip() and _.zip()
  • Loading branch information...
2 parents e702d5d + 93dee3b commit 403a8bb114da99642eaf491da03df795faa20a50 @puzrin puzrin committed Jan 21, 2013
Showing with 20 additions and 4 deletions.
  1. +5 −4 lib/argument_parser.js
  2. +15 −0 test/base.js
View
9 lib/argument_parser.js
@@ -489,17 +489,18 @@ ArgumentParser.prototype._parseKnownArgs = function (argStrings, namespace) {
// slice off the appropriate arg strings for each Positional
// and add the Positional and its args to the list
- if (argCounts.length > 0) {
- _.zip(positionals, argCounts).forEach(function (item) {
+ _.zip(positionals, argCounts).forEach(function (item) {
var action = item[0];
var argCount = item[1];
+ if (argCount === undefined) {
+ return;
+ }
var args = argStrings.slice(startIndex, startIndex + argCount);
startIndex += argCount;
takeAction(action, args);
});
- }
-
+
// slice off the Positionals that we just parsed and return the
// index at which the Positionals' string args stopped
positionals = positionals.slice(argCounts.length);
View
15 test/base.js
@@ -165,5 +165,20 @@ describe('base', function () {
assert.equal(parser.getDefault('goo'), 42);
assert.equal(parser.getDefault('help'), require('../lib/const').SUPPRESS);
});
+
+ it("should handle mixed positional and optional args", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['x']);
+ parser.addArgument(['y']);
+
+ args = parser.parseArgs(['X', 'Y']);
+ assert.deepEqual(args, {"foo": null, "x": "X", "y": "Y"});
+ args = parser.parseArgs(['-f', 'A', 'X', 'Y']);
+ assert.deepEqual(args, {"foo": "A", "x": "X", "y": "Y"});
+ args = parser.parseArgs(['X', '-f', 'A', 'Y']);
+ assert.deepEqual(args, {"foo": "A", "x": "X", "y": "Y"});
+ // was giving: Error: _mocha: error: Unrecognized arguments: X.
+ });
});

0 comments on commit 403a8bb

Please sign in to comment.