Ohm-Management - Projektarbeit B-ME
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.

action.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * class Action
  3. *
  4. * Base class for all actions
  5. * Do not call in your code, use this class only for inherits your own action
  6. *
  7. * Information about how to convert command line strings to Javascript objects.
  8. * Action objects are used by an ArgumentParser to represent the information
  9. * needed to parse a single argument from one or more strings from the command
  10. * line. The keyword arguments to the Action constructor are also all attributes
  11. * of Action instances.
  12. *
  13. * ##### Allowed keywords:
  14. *
  15. * - `store`
  16. * - `storeConstant`
  17. * - `storeTrue`
  18. * - `storeFalse`
  19. * - `append`
  20. * - `appendConstant`
  21. * - `count`
  22. * - `help`
  23. * - `version`
  24. *
  25. * Information about action options see [[Action.new]]
  26. *
  27. * See also [original guide](http://docs.python.org/dev/library/argparse.html#action)
  28. *
  29. **/
  30. 'use strict';
  31. // Constants
  32. var c = require('./const');
  33. /**
  34. * new Action(options)
  35. *
  36. * Base class for all actions. Used only for inherits
  37. *
  38. *
  39. * ##### Options:
  40. *
  41. * - `optionStrings` A list of command-line option strings for the action.
  42. * - `dest` Attribute to hold the created object(s)
  43. * - `nargs` The number of command-line arguments that should be consumed.
  44. * By default, one argument will be consumed and a single value will be
  45. * produced.
  46. * - `constant` Default value for an action with no value.
  47. * - `defaultValue` The value to be produced if the option is not specified.
  48. * - `type` Cast to 'string'|'int'|'float'|'complex'|function (string). If
  49. * None, 'string'.
  50. * - `choices` The choices available.
  51. * - `required` True if the action must always be specified at the command
  52. * line.
  53. * - `help` The help describing the argument.
  54. * - `metavar` The name to be used for the option's argument with the help
  55. * string. If None, the 'dest' value will be used as the name.
  56. *
  57. * ##### nargs supported values:
  58. *
  59. * - `N` (an integer) consumes N arguments (and produces a list)
  60. * - `?` consumes zero or one arguments
  61. * - `*` consumes zero or more arguments (and produces a list)
  62. * - `+` consumes one or more arguments (and produces a list)
  63. *
  64. * Note: that the difference between the default and nargs=1 is that with the
  65. * default, a single value will be produced, while with nargs=1, a list
  66. * containing a single value will be produced.
  67. **/
  68. var Action = module.exports = function Action(options) {
  69. options = options || {};
  70. this.optionStrings = options.optionStrings || [];
  71. this.dest = options.dest;
  72. this.nargs = typeof options.nargs !== 'undefined' ? options.nargs : null;
  73. this.constant = typeof options.constant !== 'undefined' ? options.constant : null;
  74. this.defaultValue = options.defaultValue;
  75. this.type = typeof options.type !== 'undefined' ? options.type : null;
  76. this.choices = typeof options.choices !== 'undefined' ? options.choices : null;
  77. this.required = typeof options.required !== 'undefined' ? options.required : false;
  78. this.help = typeof options.help !== 'undefined' ? options.help : null;
  79. this.metavar = typeof options.metavar !== 'undefined' ? options.metavar : null;
  80. if (!(this.optionStrings instanceof Array)) {
  81. throw new Error('optionStrings should be an array');
  82. }
  83. if (typeof this.required !== 'undefined' && typeof this.required !== 'boolean') {
  84. throw new Error('required should be a boolean');
  85. }
  86. };
  87. /**
  88. * Action#getName -> String
  89. *
  90. * Tells action name
  91. **/
  92. Action.prototype.getName = function () {
  93. if (this.optionStrings.length > 0) {
  94. return this.optionStrings.join('/');
  95. } else if (this.metavar !== null && this.metavar !== c.SUPPRESS) {
  96. return this.metavar;
  97. } else if (typeof this.dest !== 'undefined' && this.dest !== c.SUPPRESS) {
  98. return this.dest;
  99. }
  100. return null;
  101. };
  102. /**
  103. * Action#isOptional -> Boolean
  104. *
  105. * Return true if optional
  106. **/
  107. Action.prototype.isOptional = function () {
  108. return !this.isPositional();
  109. };
  110. /**
  111. * Action#isPositional -> Boolean
  112. *
  113. * Return true if positional
  114. **/
  115. Action.prototype.isPositional = function () {
  116. return (this.optionStrings.length === 0);
  117. };
  118. /**
  119. * Action#call(parser, namespace, values, optionString) -> Void
  120. * - parser (ArgumentParser): current parser
  121. * - namespace (Namespace): namespace for output data
  122. * - values (Array): parsed values
  123. * - optionString (Array): input option string(not parsed)
  124. *
  125. * Call the action. Should be implemented in inherited classes
  126. *
  127. * ##### Example
  128. *
  129. * ActionCount.prototype.call = function (parser, namespace, values, optionString) {
  130. * namespace.set(this.dest, (namespace[this.dest] || 0) + 1);
  131. * };
  132. *
  133. **/
  134. Action.prototype.call = function () {
  135. throw new Error('.call() not defined');// Not Implemented error
  136. };