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.

53 lines
1.5 KiB

4 years ago
  1. /*:nodoc:*
  2. * class ActionAppend
  3. *
  4. * This action stores a list, and appends each argument value to the list.
  5. * This is useful to allow an option to be specified multiple times.
  6. * This class inherided from [[Action]]
  7. *
  8. **/
  9. 'use strict';
  10. var util = require('util');
  11. var Action = require('../action');
  12. // Constants
  13. var c = require('../const');
  14. /*:nodoc:*
  15. * new ActionAppend(options)
  16. * - options (object): options hash see [[Action.new]]
  17. *
  18. * Note: options.nargs should be optional for constants
  19. * and more then zero for other
  20. **/
  21. var ActionAppend = module.exports = function ActionAppend(options) {
  22. options = options || {};
  23. if (this.nargs <= 0) {
  24. throw new Error('nargs for append actions must be > 0; if arg ' +
  25. 'strings are not supplying the value to append, ' +
  26. 'the append const action may be more appropriate');
  27. }
  28. if (!!this.constant && this.nargs !== c.OPTIONAL) {
  29. throw new Error('nargs must be OPTIONAL to supply const');
  30. }
  31. Action.call(this, options);
  32. };
  33. util.inherits(ActionAppend, Action);
  34. /*:nodoc:*
  35. * ActionAppend#call(parser, namespace, values, optionString) -> Void
  36. * - parser (ArgumentParser): current parser
  37. * - namespace (Namespace): namespace for output data
  38. * - values (Array): parsed values
  39. * - optionString (Array): input option string(not parsed)
  40. *
  41. * Call the action. Save result in namespace object
  42. **/
  43. ActionAppend.prototype.call = function (parser, namespace, values) {
  44. var items = (namespace[this.dest] || []).slice();
  45. items.push(values);
  46. namespace.set(this.dest, items);
  47. };