You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

4 years ago
  1. /** internal
  2. * class ArgumentGroup
  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 ActionContainer = require('../action_container');
  16. /**
  17. * new ArgumentGroup(container, options)
  18. * - container (object): main container
  19. * - options (object): hash of group options
  20. *
  21. * #### options
  22. * - **prefixChars** group name prefix
  23. * - **argumentDefault** default argument value
  24. * - **title** group title
  25. * - **description** group description
  26. *
  27. **/
  28. var ArgumentGroup = module.exports = function ArgumentGroup(container, options) {
  29. options = options || {};
  30. // add any missing keyword arguments by checking the container
  31. options.conflictHandler = (options.conflictHandler || container.conflictHandler);
  32. options.prefixChars = (options.prefixChars || container.prefixChars);
  33. options.argumentDefault = (options.argumentDefault || container.argumentDefault);
  34. ActionContainer.call(this, options);
  35. // group attributes
  36. this.title = options.title;
  37. this._groupActions = [];
  38. // share most attributes with the container
  39. this._container = container;
  40. this._registries = container._registries;
  41. this._actions = container._actions;
  42. this._optionStringActions = container._optionStringActions;
  43. this._defaults = container._defaults;
  44. this._hasNegativeNumberOptionals = container._hasNegativeNumberOptionals;
  45. this._mutuallyExclusiveGroups = container._mutuallyExclusiveGroups;
  46. };
  47. util.inherits(ArgumentGroup, ActionContainer);
  48. ArgumentGroup.prototype._addAction = function (action) {
  49. // Parent add action
  50. action = ActionContainer.prototype._addAction.call(this, action);
  51. this._groupActions.push(action);
  52. return action;
  53. };
  54. ArgumentGroup.prototype._removeAction = function (action) {
  55. // Parent remove action
  56. ActionContainer.prototype._removeAction.call(this, action);
  57. var actionIndex = this._groupActions.indexOf(action);
  58. if (actionIndex >= 0) {
  59. this._groupActions.splice(actionIndex, 1);
  60. }
  61. };