react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / lib / action.js
80713 views/**1* class Action2*3* Base class for all actions4* Do not call in your code, use this class only for inherits your own action5*6* Information about how to convert command line strings to Javascript objects.7* Action objects are used by an ArgumentParser to represent the information8* needed to parse a single argument from one or more strings from the command9* line. The keyword arguments to the Action constructor are also all attributes10* of Action instances.11*12* #####Alowed keywords:13*14* - `store`15* - `storeConstant`16* - `storeTrue`17* - `storeFalse`18* - `append`19* - `appendConstant`20* - `count`21* - `help`22* - `version`23*24* Information about action options see [[Action.new]]25*26* See also [original guide](http://docs.python.org/dev/library/argparse.html#action)27*28**/2930'use strict';313233// Constants34var $$ = require('./const');353637/**38* new Action(options)39*40* Base class for all actions. Used only for inherits41*42*43* ##### Options:44*45* - `optionStrings` A list of command-line option strings for the action.46* - `dest` Attribute to hold the created object(s)47* - `nargs` The number of command-line arguments that should be consumed.48* By default, one argument will be consumed and a single value will be49* produced.50* - `constant` Default value for an action with no value.51* - `defaultValue` The value to be produced if the option is not specified.52* - `type` Cast to 'string'|'int'|'float'|'complex'|function (string). If53* None, 'string'.54* - `choices` The choices available.55* - `required` True if the action must always be specified at the command56* line.57* - `help` The help describing the argument.58* - `metavar` The name to be used for the option's argument with the help59* string. If None, the 'dest' value will be used as the name.60*61* ##### nargs supported values:62*63* - `N` (an integer) consumes N arguments (and produces a list)64* - `?` consumes zero or one arguments65* - `*` consumes zero or more arguments (and produces a list)66* - `+` consumes one or more arguments (and produces a list)67*68* Note: that the difference between the default and nargs=1 is that with the69* default, a single value will be produced, while with nargs=1, a list70* containing a single value will be produced.71**/72var Action = module.exports = function Action(options) {73options = options || {};74this.optionStrings = options.optionStrings || [];75this.dest = options.dest;76this.nargs = options.nargs !== undefined ? options.nargs : null;77this.constant = options.constant !== undefined ? options.constant : null;78this.defaultValue = options.defaultValue;79this.type = options.type !== undefined ? options.type : null;80this.choices = options.choices !== undefined ? options.choices : null;81this.required = options.required !== undefined ? options.required: false;82this.help = options.help !== undefined ? options.help : null;83this.metavar = options.metavar !== undefined ? options.metavar : null;8485if (!(this.optionStrings instanceof Array)) {86throw new Error('optionStrings should be an array');87}88if (this.required !== undefined && typeof(this.required) !== 'boolean') {89throw new Error('required should be a boolean');90}91};9293/**94* Action#getName -> String95*96* Tells action name97**/98Action.prototype.getName = function () {99if (this.optionStrings.length > 0) {100return this.optionStrings.join('/');101} else if (this.metavar !== null && this.metavar !== $$.SUPPRESS) {102return this.metavar;103} else if (this.dest !== undefined && this.dest !== $$.SUPPRESS) {104return this.dest;105}106return null;107};108109/**110* Action#isOptional -> Boolean111*112* Return true if optional113**/114Action.prototype.isOptional = function () {115return !this.isPositional();116};117118/**119* Action#isPositional -> Boolean120*121* Return true if positional122**/123Action.prototype.isPositional = function () {124return (this.optionStrings.length === 0);125};126127/**128* Action#call(parser, namespace, values, optionString) -> Void129* - parser (ArgumentParser): current parser130* - namespace (Namespace): namespace for output data131* - values (Array): parsed values132* - optionString (Array): input option string(not parsed)133*134* Call the action. Should be implemented in inherited classes135*136* ##### Example137*138* ActionCount.prototype.call = function (parser, namespace, values, optionString) {139* namespace.set(this.dest, (namespace[this.dest] || 0) + 1);140* };141*142**/143Action.prototype.call = function () {144throw new Error('.call() not defined');// Not Implemented error145};146147148