exclusive.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. var util = require('util');
  15. var ArgumentGroup = require('./group');
  16. /**
  17. * new MutuallyExclusiveGroup(container, options)
  18. * - container (object): main container
  19. * - options (object): options.required -> true/false
  20. *
  21. * `required` could be an argument itself, but making it a property of
  22. * the options argument is more consistent with the JS adaptation of the Python)
  23. **/
  24. var MutuallyExclusiveGroup = module.exports = function MutuallyExclusiveGroup(container, options) {
  25. var required;
  26. options = options || {};
  27. required = options.required || false;
  28. ArgumentGroup.call(this, container);
  29. this.required = required;
  30. };
  31. util.inherits(MutuallyExclusiveGroup, ArgumentGroup);
  32. MutuallyExclusiveGroup.prototype._addAction = function (action) {
  33. var msg;
  34. if (action.required) {
  35. msg = 'mutually exclusive arguments must be optional';
  36. throw new Error(msg);
  37. }
  38. action = this._container._addAction(action);
  39. this._groupActions.push(action);
  40. return action;
  41. };
  42. MutuallyExclusiveGroup.prototype._removeAction = function (action) {
  43. this._container._removeAction(action);
  44. this._groupActions.remove(action);
  45. };