react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / lib / action / subparsers.js
80728 views/** internal1* class ActionSubparsers2*3* Support the creation of such sub-commands with the addSubparsers()4*5* This class inherited from [[Action]]6**/7'use strict';89var util = require('util');10var format = require('util').format;11var _ = require('lodash');121314var Action = require('../action');1516// Constants17var $$ = require('../const');1819// Errors20var argumentErrorHelper = require('../argument/error');212223/*:nodoc:*24* new ChoicesPseudoAction(name, help)25*26* Create pseudo action for correct help text27*28**/29var ChoicesPseudoAction = function (name, help) {30var options = {31optionStrings: [],32dest: name,33help: help34};3536Action.call(this, options);37};38util.inherits(ChoicesPseudoAction, Action);3940/**41* new ActionSubparsers(options)42* - options (object): options hash see [[Action.new]]43*44**/45var ActionSubparsers = module.exports = function ActionSubparsers(options) {46options = options || {};47options.dest = options.dest || $$.SUPPRESS;48options.nargs = $$.PARSER;4950this.debug = (options.debug === true);5152this._progPrefix = options.prog;53this._parserClass = options.parserClass;54this._nameParserMap = {};55this._choicesActions = [];5657options.choices = this._nameParserMap;58Action.call(this, options);59};60util.inherits(ActionSubparsers, Action);6162/*:nodoc:*63* ActionSubparsers#addParser(name, options) -> ArgumentParser64* - name (string): sub-command name65* - options (object): see [[ArgumentParser.new]]66*67* Note:68* addParser supports an additional aliases option,69* which allows multiple strings to refer to the same subparser.70* This example, like svn, aliases co as a shorthand for checkout71*72**/73ActionSubparsers.prototype.addParser = function (name, options) {74var parser;7576var self = this;7778options = options || {};7980options.debug = (this.debug === true);8182// set program from the existing prefix83if (!options.prog) {84options.prog = this._progPrefix + ' ' + name;85}8687var aliases = options.aliases || [];8889// create a pseudo-action to hold the choice help90if (!!options.help || _.isString(options.help)) {91var help = options.help;92delete options.help;9394var choiceAction = new ChoicesPseudoAction(name, help);95this._choicesActions.push(choiceAction);96}9798// create the parser and add it to the map99parser = new this._parserClass(options);100this._nameParserMap[name] = parser;101102// make parser available under aliases also103aliases.forEach(function (alias) {104self._nameParserMap[alias] = parser;105});106107return parser;108};109110ActionSubparsers.prototype._getSubactions = function () {111return this._choicesActions;112};113114/*:nodoc:*115* ActionSubparsers#call(parser, namespace, values, optionString) -> Void116* - parser (ArgumentParser): current parser117* - namespace (Namespace): namespace for output data118* - values (Array): parsed values119* - optionString (Array): input option string(not parsed)120*121* Call the action. Parse input aguments122**/123ActionSubparsers.prototype.call = function (parser, namespace, values) {124var parserName = values[0];125var argStrings = values.slice(1);126127// set the parser name if requested128if (this.dest !== $$.SUPPRESS) {129namespace[this.dest] = parserName;130}131132// select the parser133if (!!this._nameParserMap[parserName]) {134parser = this._nameParserMap[parserName];135} else {136throw argumentErrorHelper(format(137'Unknown parser "%s" (choices: [%s]).',138parserName,139_.keys(this._nameParserMap).join(', ')140));141}142143// parse all the remaining options into the namespace144parser.parseArgs(argStrings, namespace);145};146147148149150