Giter VIP home page Giter VIP logo

Comments (2)

hpaulj avatar hpaulj commented on September 26, 2024

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.

from argparse.

shkuropat avatar shkuropat commented on September 26, 2024

thanks

from argparse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.