exclusive.js (1589B)
1 /** internal 2 * class MutuallyExclusiveGroup 3 * 4 * Group arguments. 5 * By default, ArgumentParser groups command-line arguments 6 * into “positional arguments” and “optional arguments” 7 * when displaying help messages. When there is a better 8 * conceptual grouping of arguments than this default one, 9 * appropriate groups can be created using the addArgumentGroup() method 10 * 11 * This class inherited from [[ArgumentContainer]] 12 **/ 13 'use strict'; 14 15 var util = require('util'); 16 17 var ArgumentGroup = require('./group'); 18 19 /** 20 * new MutuallyExclusiveGroup(container, options) 21 * - container (object): main container 22 * - options (object): options.required -> true/false 23 * 24 * `required` could be an argument itself, but making it a property of 25 * the options argument is more consistent with the JS adaptation of the Python) 26 **/ 27 var MutuallyExclusiveGroup = module.exports = function MutuallyExclusiveGroup(container, options) { 28 var required; 29 options = options || {}; 30 required = options.required || false; 31 ArgumentGroup.call(this, container); 32 this.required = required; 33 34 }; 35 util.inherits(MutuallyExclusiveGroup, ArgumentGroup); 36 37 38 MutuallyExclusiveGroup.prototype._addAction = function (action) { 39 var msg; 40 if (action.required) { 41 msg = 'mutually exclusive arguments must be optional'; 42 throw new Error(msg); 43 } 44 action = this._container._addAction(action); 45 this._groupActions.push(action); 46 return action; 47 }; 48 49 50 MutuallyExclusiveGroup.prototype._removeAction = function (action) { 51 this._container._removeAction(action); 52 this._groupActions.remove(action); 53 }; 54