| 1 |
/** |
| 2 |
* class Action |
| 3 |
* |
| 4 |
* Base class for all actions |
| 5 |
* Do not call in your code, use this class only for inherits your own action |
| 6 |
* |
| 7 |
* Information about how to convert command line strings to Javascript objects. |
| 8 |
* Action objects are used by an ArgumentParser to represent the information |
| 9 |
* needed to parse a single argument from one or more strings from the command |
| 10 |
* line. The keyword arguments to the Action constructor are also all attributes |
| 11 |
* of Action instances. |
| 12 |
* |
| 13 |
* ##### Allowed keywords: |
| 14 |
* |
| 15 |
* - `store` |
| 16 |
* - `storeConstant` |
| 17 |
* - `storeTrue` |
| 18 |
* - `storeFalse` |
| 19 |
* - `append` |
| 20 |
* - `appendConstant` |
| 21 |
* - `count` |
| 22 |
* - `help` |
| 23 |
* - `version` |
| 24 |
* |
| 25 |
* Information about action options see [[Action.new]] |
| 26 |
* |
| 27 |
* See also [original guide](http://docs.python.org/dev/library/argparse.html#action) |
| 28 |
* |
| 29 |
**/ |
| 30 |
|
| 31 |
'use strict'; |
| 32 |
|
| 33 |
|
| 34 |
// Constants |
| 35 |
var c = require('./const'); |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* new Action(options) |
| 40 |
* |
| 41 |
* Base class for all actions. Used only for inherits |
| 42 |
* |
| 43 |
* |
| 44 |
* ##### Options: |
| 45 |
* |
| 46 |
* - `optionStrings` A list of command-line option strings for the action. |
| 47 |
* - `dest` Attribute to hold the created object(s) |
| 48 |
* - `nargs` The number of command-line arguments that should be consumed. |
| 49 |
* By default, one argument will be consumed and a single value will be |
| 50 |
* produced. |
| 51 |
* - `constant` Default value for an action with no value. |
| 52 |
* - `defaultValue` The value to be produced if the option is not specified. |
| 53 |
* - `type` Cast to 'string'|'int'|'float'|'complex'|function (string). If |
| 54 |
* None, 'string'. |
| 55 |
* - `choices` The choices available. |
| 56 |
* - `required` True if the action must always be specified at the command |
| 57 |
* line. |
| 58 |
* - `help` The help describing the argument. |
| 59 |
* - `metavar` The name to be used for the option's argument with the help |
| 60 |
* string. If None, the 'dest' value will be used as the name. |
| 61 |
* |
| 62 |
* ##### nargs supported values: |
| 63 |
* |
| 64 |
* - `N` (an integer) consumes N arguments (and produces a list) |
| 65 |
* - `?` consumes zero or one arguments |
| 66 |
* - `*` consumes zero or more arguments (and produces a list) |
| 67 |
* - `+` consumes one or more arguments (and produces a list) |
| 68 |
* |
| 69 |
* Note: that the difference between the default and nargs=1 is that with the |
| 70 |
* default, a single value will be produced, while with nargs=1, a list |
| 71 |
* containing a single value will be produced. |
| 72 |
**/ |
| 73 |
var Action = module.exports = function Action(options) { |
| 74 |
options = options || {}; |
| 75 |
this.optionStrings = options.optionStrings || []; |
| 76 |
this.dest = options.dest; |
| 77 |
this.nargs = typeof options.nargs !== 'undefined' ? options.nargs : null; |
| 78 |
this.constant = typeof options.constant !== 'undefined' ? options.constant : null; |
| 79 |
this.defaultValue = options.defaultValue; |
| 80 |
this.type = typeof options.type !== 'undefined' ? options.type : null; |
| 81 |
this.choices = typeof options.choices !== 'undefined' ? options.choices : null; |
| 82 |
this.required = typeof options.required !== 'undefined' ? options.required : false; |
| 83 |
this.help = typeof options.help !== 'undefined' ? options.help : null; |
| 84 |
this.metavar = typeof options.metavar !== 'undefined' ? options.metavar : null; |
| 85 |
|
| 86 |
if (!(this.optionStrings instanceof Array)) { |
| 87 |
throw new Error('optionStrings should be an array'); |
| 88 |
} |
| 89 |
if (typeof this.required !== 'undefined' && typeof this.required !== 'boolean') { |
| 90 |
throw new Error('required should be a boolean'); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
/** |
| 95 |
* Action#getName -> String |
| 96 |
* |
| 97 |
* Tells action name |
| 98 |
**/ |
| 99 |
Action.prototype.getName = function () { |
| 100 |
if (this.optionStrings.length > 0) { |
| 101 |
return this.optionStrings.join('/'); |
| 102 |
} else if (this.metavar !== null && this.metavar !== c.SUPPRESS) { |
| 103 |
return this.metavar; |
| 104 |
} else if (typeof this.dest !== 'undefined' && this.dest !== c.SUPPRESS) { |
| 105 |
return this.dest; |
| 106 |
} |
| 107 |
return null; |
| 108 |
}; |
| 109 |
|
| 110 |
/** |
| 111 |
* Action#isOptional -> Boolean |
| 112 |
* |
| 113 |
* Return true if optional |
| 114 |
**/ |
| 115 |
Action.prototype.isOptional = function () { |
| 116 |
return !this.isPositional(); |
| 117 |
}; |
| 118 |
|
| 119 |
/** |
| 120 |
* Action#isPositional -> Boolean |
| 121 |
* |
| 122 |
* Return true if positional |
| 123 |
**/ |
| 124 |
Action.prototype.isPositional = function () { |
| 125 |
return (this.optionStrings.length === 0); |
| 126 |
}; |
| 127 |
|
| 128 |
/** |
| 129 |
* Action#call(parser, namespace, values, optionString) -> Void |
| 130 |
* - parser (ArgumentParser): current parser |
| 131 |
* - namespace (Namespace): namespace for output data |
| 132 |
* - values (Array): parsed values |
| 133 |
* - optionString (Array): input option string(not parsed) |
| 134 |
* |
| 135 |
* Call the action. Should be implemented in inherited classes |
| 136 |
* |
| 137 |
* ##### Example |
| 138 |
* |
| 139 |
* ActionCount.prototype.call = function (parser, namespace, values, optionString) { |
| 140 |
* namespace.set(this.dest, (namespace[this.dest] || 0) + 1); |
| 141 |
* }; |
| 142 |
* |
| 143 |
**/ |
| 144 |
Action.prototype.call = function () { |
| 145 |
throw new Error('.call() not defined');// Not Implemented error |
| 146 |
}; |