2 * class ActionSubparsers
4 * Support the creation of such sub-commands with the addSubparsers()
6 * This class inherited from [[Action]]
10 var util = require('util');
11 var format = require('util').format;
14 var Action = require('../action');
17 var c = require('../const');
20 var argumentErrorHelper = require('../argument/error');
24 * new ChoicesPseudoAction(name, help)
26 * Create pseudo action for correct help text
29 function ChoicesPseudoAction(name, help) {
36 Action.call(this, options);
39 util.inherits(ChoicesPseudoAction, Action);
42 * new ActionSubparsers(options)
43 * - options (object): options hash see [[Action.new]]
46 function ActionSubparsers(options) {
47 options = options || {};
48 options.dest = options.dest || c.SUPPRESS;
49 options.nargs = c.PARSER;
51 this.debug = (options.debug === true);
53 this._progPrefix = options.prog;
54 this._parserClass = options.parserClass;
55 this._nameParserMap = {};
56 this._choicesActions = [];
58 options.choices = this._nameParserMap;
59 Action.call(this, options);
62 util.inherits(ActionSubparsers, Action);
65 * ActionSubparsers#addParser(name, options) -> ArgumentParser
66 * - name (string): sub-command name
67 * - options (object): see [[ArgumentParser.new]]
70 * addParser supports an additional aliases option,
71 * which allows multiple strings to refer to the same subparser.
72 * This example, like svn, aliases co as a shorthand for checkout
75 ActionSubparsers.prototype.addParser = function (name, options) {
80 options = options || {};
82 options.debug = (this.debug === true);
84 // set program from the existing prefix
86 options.prog = this._progPrefix + ' ' + name;
89 var aliases = options.aliases || [];
91 // create a pseudo-action to hold the choice help
92 if (!!options.help || typeof options.help === 'string') {
93 var help = options.help;
96 var choiceAction = new ChoicesPseudoAction(name, help);
97 this._choicesActions.push(choiceAction);
100 // create the parser and add it to the map
101 parser = new this._parserClass(options);
102 this._nameParserMap[name] = parser;
104 // make parser available under aliases also
105 aliases.forEach(function (alias) {
106 self._nameParserMap[alias] = parser;
112 ActionSubparsers.prototype._getSubactions = function () {
113 return this._choicesActions;
117 * ActionSubparsers#call(parser, namespace, values, optionString) -> Void
118 * - parser (ArgumentParser): current parser
119 * - namespace (Namespace): namespace for output data
120 * - values (Array): parsed values
121 * - optionString (Array): input option string(not parsed)
123 * Call the action. Parse input aguments
125 ActionSubparsers.prototype.call = function (parser, namespace, values) {
126 var parserName = values[0];
127 var argStrings = values.slice(1);
129 // set the parser name if requested
130 if (this.dest !== c.SUPPRESS) {
131 namespace[this.dest] = parserName;
135 if (this._nameParserMap[parserName]) {
136 parser = this._nameParserMap[parserName];
138 throw argumentErrorHelper(format(
139 'Unknown parser "%s" (choices: [%s]).',
141 Object.keys(this._nameParserMap).join(', ')
145 // parse all the remaining options into the namespace
146 parser.parseArgs(argStrings, namespace);
149 module.exports = ActionSubparsers;