2 * class ActionContainer
4 * Action container. Parent for [[ArgumentParser]] and [[ArgumentGroup]]
9 var format = require('util').format;
10 var _ = require('lodash');
13 var $$ = require('./const');
16 var ActionHelp = require('./action/help');
17 var ActionAppend = require('./action/append');
18 var ActionAppendConstant = require('./action/append/constant');
19 var ActionCount = require('./action/count');
20 var ActionStore = require('./action/store');
21 var ActionStoreConstant = require('./action/store/constant');
22 var ActionStoreTrue = require('./action/store/true');
23 var ActionStoreFalse = require('./action/store/false');
24 var ActionVersion = require('./action/version');
25 var ActionSubparsers = require('./action/subparsers');
28 var argumentErrorHelper = require('./argument/error');
33 * new ActionContainer(options)
35 * Action container. Parent for [[ArgumentParser]] and [[ArgumentGroup]]
39 * - `description` -- A description of what the program does
40 * - `prefixChars` -- Characters that prefix optional arguments
41 * - `argumentDefault` -- The default value for all arguments
42 * - `conflictHandler` -- The conflict handler to use for duplicate arguments
44 var ActionContainer = module.exports = function ActionContainer(options) {
45 options = options || {};
47 this.description = options.description;
48 this.argumentDefault = options.argumentDefault;
49 this.prefixChars = options.prefixChars || '';
50 this.conflictHandler = options.conflictHandler;
53 this._registries = {};
56 this.register('action', null, ActionStore);
57 this.register('action', 'store', ActionStore);
58 this.register('action', 'storeConst', ActionStoreConstant);
59 this.register('action', 'storeTrue', ActionStoreTrue);
60 this.register('action', 'storeFalse', ActionStoreFalse);
61 this.register('action', 'append', ActionAppend);
62 this.register('action', 'appendConst', ActionAppendConstant);
63 this.register('action', 'count', ActionCount);
64 this.register('action', 'help', ActionHelp);
65 this.register('action', 'version', ActionVersion);
66 this.register('action', 'parsers', ActionSubparsers);
68 // raise an exception if the conflict handler is invalid
73 this._optionStringActions = {};
76 this._actionGroups = [];
77 this._mutuallyExclusiveGroups = [];
82 // determines whether an "option" looks like a negative number
84 this._regexpNegativeNumber = new RegExp('^[-]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$');
86 // whether or not there are any optionals that look like negative
87 // numbers -- uses a list so it can be shared and edited
88 this._hasNegativeNumberOptionals = [];
91 // Groups must be required, then ActionContainer already defined
92 var ArgumentGroup = require('./argument/group');
93 var MutuallyExclusiveGroup = require('./argument/exclusive');
96 // Registration methods
100 * ActionContainer#register(registryName, value, object) -> Void
101 * - registryName (String) : object type action|type
102 * - value (string) : keyword
103 * - object (Object|Function) : handler
107 ActionContainer.prototype.register = function (registryName, value, object) {
108 this._registries[registryName] = this._registries[registryName] || {};
109 this._registries[registryName][value] = object;
112 ActionContainer.prototype._registryGet = function (registryName, value, defaultValue) {
113 if (3 > arguments.length) {
116 return this._registries[registryName][value] || defaultValue;
120 // Namespace default accessor methods
124 * ActionContainer#setDefaults(options) -> Void
125 * - options (object):hash of options see [[Action.new]]
129 ActionContainer.prototype.setDefaults = function (options) {
130 options = options || {};
131 for (var property in options) {
132 this._defaults[property] = options[property];
135 // if these defaults match any existing arguments, replace the previous
136 // default on the object with the new one
137 this._actions.forEach(function (action) {
138 if (action.dest in options) {
139 action.defaultValue = options[action.dest];
145 * ActionContainer#getDefault(dest) -> Mixed
146 * - dest (string): action destination
148 * Return action default value
150 ActionContainer.prototype.getDefault = function (dest) {
151 var result = (_.has(this._defaults, dest)) ? this._defaults[dest] : null;
153 this._actions.forEach(function (action) {
154 if (action.dest === dest && _.has(action, 'defaultValue')) {
155 result = action.defaultValue;
162 // Adding argument actions
166 * ActionContainer#addArgument(args, options) -> Object
167 * - args (Array): array of argument keys
168 * - options (Object): action objects see [[Action.new]]
171 * - addArgument([-f, --foo], {action:'store', defaultValue=1, ...})
172 * - addArgument(['bar'], action: 'store', nargs:1, ...})
174 ActionContainer.prototype.addArgument = function (args, options) {
176 options = options || {};
178 if (!_.isArray(args)) {
179 throw new TypeError('addArgument first argument should be an array');
181 if (!_.isObject(options) || _.isArray(options)) {
182 throw new TypeError('addArgument second argument should be a hash');
185 // if no positional args are supplied or only one is supplied and
186 // it doesn't look like an option string, parse a positional argument
187 if (!args || args.length === 1 && this.prefixChars.indexOf(args[0][0]) < 0) {
188 if (args && !!options.dest) {
189 throw new Error('dest supplied twice for positional argument');
191 options = this._getPositional(args, options);
193 // otherwise, we're adding an optional argument
195 options = this._getOptional(args, options);
198 // if no default was supplied, use the parser-level default
199 if (_.isUndefined(options.defaultValue)) {
200 var dest = options.dest;
201 if (_.has(this._defaults, dest)) {
202 options.defaultValue = this._defaults[dest];
203 } else if (!_.isUndefined(this.argumentDefault)) {
204 options.defaultValue = this.argumentDefault;
208 // create the action object, and add it to the parser
209 var ActionClass = this._popActionClass(options);
210 if (! _.isFunction(ActionClass)) {
211 throw new Error(format('Unknown action "%s".', ActionClass));
213 var action = new ActionClass(options);
215 // throw an error if the action type is not callable
216 var typeFunction = this._registryGet('type', action.type, action.type);
217 if (!_.isFunction(typeFunction)) {
218 throw new Error(format('"%s" is not callable', typeFunction));
221 return this._addAction(action);
225 * ActionContainer#addArgumentGroup(options) -> ArgumentGroup
226 * - options (Object): hash of options see [[ArgumentGroup.new]]
228 * Create new arguments groups
230 ActionContainer.prototype.addArgumentGroup = function (options) {
231 var group = new ArgumentGroup(this, options);
232 this._actionGroups.push(group);
237 * ActionContainer#addMutuallyExclusiveGroup(options) -> ArgumentGroup
238 * - options (Object): {required: false}
240 * Create new mutual exclusive groups
242 ActionContainer.prototype.addMutuallyExclusiveGroup = function (options) {
243 var group = new MutuallyExclusiveGroup(this, options);
244 this._mutuallyExclusiveGroups.push(group);
248 ActionContainer.prototype._addAction = function (action) {
251 // resolve any conflicts
252 this._checkConflict(action);
254 // add to actions list
255 this._actions.push(action);
256 action.container = this;
258 // index the action by any option strings it has
259 action.optionStrings.forEach(function (optionString) {
260 self._optionStringActions[optionString] = action;
263 // set the flag if any option strings look like negative numbers
264 action.optionStrings.forEach(function (optionString) {
265 if (optionString.match(self._regexpNegativeNumber)) {
266 if (!_.any(self._hasNegativeNumberOptionals)) {
267 self._hasNegativeNumberOptionals.push(true);
272 // return the created action
276 ActionContainer.prototype._removeAction = function (action) {
277 var actionIndex = this._actions.indexOf(action);
278 if (actionIndex >= 0) {
279 this._actions.splice(actionIndex, 1);
283 ActionContainer.prototype._addContainerActions = function (container) {
284 // collect groups by titles
285 var titleGroupMap = {};
286 this._actionGroups.forEach(function (group) {
287 if (titleGroupMap[group.title]) {
288 throw new Error(format('Cannot merge actions - two groups are named "%s".', group.title));
290 titleGroupMap[group.title] = group;
293 // map each action to its group
295 function actionHash(action) {
296 // unique (hopefully?) string suitable as dictionary key
297 return action.getName();
299 container._actionGroups.forEach(function (group) {
300 // if a group with the title exists, use that, otherwise
301 // create a new group matching the container's group
302 if (!titleGroupMap[group.title]) {
303 titleGroupMap[group.title] = this.addArgumentGroup({
305 description: group.description
309 // map the actions to their new group
310 group._groupActions.forEach(function (action) {
311 groupMap[actionHash(action)] = titleGroupMap[group.title];
315 // add container's mutually exclusive groups
316 // NOTE: if add_mutually_exclusive_group ever gains title= and
317 // description= then this code will need to be expanded as above
319 container._mutuallyExclusiveGroups.forEach(function (group) {
320 mutexGroup = this.addMutuallyExclusiveGroup({
321 required: group.required
323 // map the actions to their new mutex group
324 group._groupActions.forEach(function (action) {
325 groupMap[actionHash(action)] = mutexGroup;
327 }, this); // forEach takes a 'this' argument
329 // add all actions to this container or their group
330 container._actions.forEach(function (action) {
331 var key = actionHash(action);
332 if (!!groupMap[key]) {
333 groupMap[key]._addAction(action);
337 this._addAction(action);
342 ActionContainer.prototype._getPositional = function (dest, options) {
343 if (_.isArray(dest)) {
344 dest = _.first(dest);
346 // make sure required is not specified
347 if (options.required) {
348 throw new Error('"required" is an invalid argument for positionals.');
351 // mark positional arguments as required if at least one is
353 if (options.nargs !== $$.OPTIONAL && options.nargs !== $$.ZERO_OR_MORE) {
354 options.required = true;
356 if (options.nargs === $$.ZERO_OR_MORE && options.defaultValue === undefined) {
357 options.required = true;
360 // return the keyword arguments with no option strings
362 options.optionStrings = [];
366 ActionContainer.prototype._getOptional = function (args, options) {
367 var prefixChars = this.prefixChars;
368 var optionStrings = [];
369 var optionStringsLong = [];
371 // determine short and long option strings
372 args.forEach(function (optionString) {
373 // error on strings that don't start with an appropriate prefix
374 if (prefixChars.indexOf(optionString[0]) < 0) {
375 throw new Error(format('Invalid option string "%s": must start with a "%s".',
381 // strings starting with two prefix characters are long options
382 optionStrings.push(optionString);
383 if (optionString.length > 1 && prefixChars.indexOf(optionString[1]) >= 0) {
384 optionStringsLong.push(optionString);
388 // infer dest, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
389 var dest = options.dest || null;
393 var optionStringDest = optionStringsLong.length ? optionStringsLong[0] :optionStrings[0];
394 dest = _.trim(optionStringDest, this.prefixChars);
396 if (dest.length === 0) {
398 format('dest= is required for options like "%s"', optionStrings.join(', '))
401 dest = dest.replace(/-/g, '_');
404 // return the updated keyword arguments
406 options.optionStrings = optionStrings;
411 ActionContainer.prototype._popActionClass = function (options, defaultValue) {
412 defaultValue = defaultValue || null;
414 var action = (options.action || defaultValue);
415 delete options.action;
417 var actionClass = this._registryGet('action', action, action);
421 ActionContainer.prototype._getHandler = function () {
422 var handlerString = this.conflictHandler;
423 var handlerFuncName = "_handleConflict" + _.capitalize(handlerString);
424 var func = this[handlerFuncName];
425 if (typeof func === 'undefined') {
426 var msg = "invalid conflict resolution value: " + handlerString;
427 throw new Error(msg);
433 ActionContainer.prototype._checkConflict = function (action) {
434 var optionStringActions = this._optionStringActions;
435 var conflictOptionals = [];
437 // find all options that conflict with this option
438 // collect pairs, the string, and an existing action that it conflicts with
439 action.optionStrings.forEach(function (optionString) {
440 var conflOptional = optionStringActions[optionString];
441 if (typeof conflOptional !== 'undefined') {
442 conflictOptionals.push([optionString, conflOptional]);
446 if (conflictOptionals.length > 0) {
447 var conflictHandler = this._getHandler();
448 conflictHandler.call(this, action, conflictOptionals);
452 ActionContainer.prototype._handleConflictError = function (action, conflOptionals) {
453 var conflicts = _.map(conflOptionals, function (pair) {return pair[0]; });
454 conflicts = conflicts.join(', ');
455 throw argumentErrorHelper(
457 format('Conflicting option string(s): %s', conflicts)
461 ActionContainer.prototype._handleConflictResolve = function (action, conflOptionals) {
462 // remove all conflicting options
464 conflOptionals.forEach(function (pair) {
465 var optionString = pair[0];
466 var conflictingAction = pair[1];
467 // remove the conflicting option string
468 var i = conflictingAction.optionStrings.indexOf(optionString);
470 conflictingAction.optionStrings.splice(i, 1);
472 delete self._optionStringActions[optionString];
473 // if the option now has no option string, remove it from the
474 // container holding it
475 if (conflictingAction.optionStrings.length === 0) {
476 conflictingAction.container._removeAction(conflictingAction);