4 * Base class for all actions
5 * Do not call in your code, use this class only for inherits your own action
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.
13 * ##### Allowed keywords:
25 * Information about action options see [[Action.new]]
27 * See also [original guide](http://docs.python.org/dev/library/argparse.html#action)
35 var c = require('./const');
41 * Base class for all actions. Used only for inherits
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
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
55 * - `choices` The choices available.
56 * - `required` True if the action must always be specified at the command
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.
62 * ##### nargs supported values:
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)
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.
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;
86 if (!(this.optionStrings instanceof Array)) {
87 throw new Error('optionStrings should be an array');
89 if (typeof this.required !== 'undefined' && typeof this.required !== 'boolean') {
90 throw new Error('required should be a boolean');
95 * Action#getName -> String
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) {
104 } else if (typeof this.dest !== 'undefined' && this.dest !== c.SUPPRESS) {
111 * Action#isOptional -> Boolean
113 * Return true if optional
115 Action.prototype.isOptional = function () {
116 return !this.isPositional();
120 * Action#isPositional -> Boolean
122 * Return true if positional
124 Action.prototype.isPositional = function () {
125 return (this.optionStrings.length === 0);
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)
135 * Call the action. Should be implemented in inherited classes
139 * ActionCount.prototype.call = function (parser, namespace, values, optionString) {
140 * namespace.set(this.dest, (namespace[this.dest] || 0) + 1);
144 Action.prototype.call = function () {
145 throw new Error('.call() not defined');// Not Implemented error