argparse
argparse copied to clipboard
'parents' is broken
Sample code:
ArgumentParser = require('argparse').ArgumentParser
parentParser = new ArgumentParser()
parentParser.addArgument([ '--x' ])
parser = new ArgumentParser({
parents: [parentParser]
})
Produces:
$ node test.js
/test/node_modules/argparse/lib/argument_parser.js:124
this._addContainerActions(parent);
^
TypeError: Cannot call method '_addContainerActions' of undefined
at /test/node_modules/argparse/lib/argument_parser.js:124:10
at Array.forEach (native)
at new ArgumentParser (/test/node_modules/argparse/lib/argument_parser.js:123:19)
at Object.<anonymous> (/test/bin/test.js:6:10)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)
I found 2 types of error in this 'parent' code, a need for a var that = this; to pass this object into a nested function, and use of obj.indexOf (for the Python in).
first in argument_parser.js
var ArgumentParser = module.exports = function ArgumentParser(options)
...
// add parent arguments and defaults
var that = this; // like coffeescript =>
options.parents.forEach(function (parent) {
that._addContainerActions(parent); // this to that
if (parent._defaults !== undefined) {
for (var defaultKey in parent._defaults) {
if (parent._defaults.hasOwnProperty(defaultKey)) {
that._defaults[defaultKey] = parent._defaults[defaultKey];
}
}
}
});
An alternative might be to use _.bind, or the native Function.bind.
In action_container.js
ActionContainer.prototype._addContainerActions = function (container) {
// collect groups by titles
var titleGroupMap = {};
this._actionGroups.forEach(function (group) {
// if (titleGroupMap.indexOf(group.title) >= 0) { // wrong, titleGroupMap is obj, not array
// if group.title in title_group_map:
if (_.has(titleGroupMap, group.title)) { // correct test
throw new Error(_.str.sprintf('Cannot merge actions - two groups are named "%(group.title)s".', group));
}
titleGroupMap[group.title] = group;
});
...
// map each action to its group
var groupMap = {};
var that = this; // as in coffeescript =>
container._actionGroups.forEach(function (group) {
// if a group with the title exists, use that, otherwise
// create a new group matching the container's group
// if (titleGroupMap.indexOf(group.title) < 0) { // wrong translation of python 'in'
if (!_.has(titleGroupMap, group.title)) { // correction
titleGroupMap[group.title] = that.addArgumentGroup({ // correction, this to that
title: group.title,
description: group.description
});
}
With these corrections the Python 'parent' example runs:
parent_parser = new ArgumentParser({addHelp:false});
// note addHelp:false to prevent duplication of the -h option
parent_parser.addArgument(['--parent'], {type:'int',description:'parent', });
foo_parser = new ArgumentParser({parents:[parent_parser],description:'child1', });
foo_parser.addArgument(['foo']);
args = foo_parser.parseArgs(['--parent', '2', 'XXX']);
console.log(args);
// Namespace(foo='XXX', parent=2)
bar_parser = new ArgumentParser({parents:[parent_parser],description:'child2', });
bar_parser.addArgument(['--bar']);
args = bar_parser.parseArgs(['--bar', 'YYY']);
console.log(args)
// Namespace(bar='YYY', parent=None)
However, I don't think these examples fully test the 'group' feature.
thanks