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.

README.md 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Optionator
  2. <a name="optionator" />
  3. Optionator is a JavaScript option parsing and help generation library used by [eslint](http://eslint.org), [Grasp](http://graspjs.com), [LiveScript](http://livescript.net), [esmangle](https://github.com/estools/esmangle), [escodegen](https://github.com/estools/escodegen), and [many more](https://www.npmjs.com/browse/depended/optionator).
  4. For an online demo, check out the [Grasp online demo](http://www.graspjs.com/#demo).
  5. [About](#about) &middot; [Usage](#usage) &middot; [Settings Format](#settings-format) &middot; [Argument Format](#argument-format)
  6. ## Why?
  7. The problem with other option parsers, such as `yargs` or `minimist`, is they just accept all input, valid or not.
  8. With Optionator, if you mistype an option, it will give you an error (with a suggestion for what you meant).
  9. If you give the wrong type of argument for an option, it will give you an error rather than supplying the wrong input to your application.
  10. $ cmd --halp
  11. Invalid option '--halp' - perhaps you meant '--help'?
  12. $ cmd --count str
  13. Invalid value for option 'count' - expected type Int, received value: str.
  14. Other helpful features include reformatting the help text based on the size of the console, so that it fits even if the console is narrow, and accepting not just an array (eg. process.argv), but a string or object as well, making things like testing much easier.
  15. ## About
  16. Optionator uses [type-check](https://github.com/gkz/type-check) and [levn](https://github.com/gkz/levn) behind the scenes to cast and verify input according the specified types.
  17. MIT license. Version 0.8.2
  18. npm install optionator
  19. For updates on Optionator, [follow me on twitter](https://twitter.com/gkzahariev).
  20. ## Usage
  21. `require('optionator');` returns a function. It has one property, `VERSION`, the current version of the library as a string. This function is called with an object specifying your options and other information, see the [settings format section](#settings-format). This in turn returns an object with three properties, `parse`, `parseArgv`, `generateHelp`, and `generateHelpForOption`, which are all functions.
  22. ```js
  23. var optionator = require('optionator')({
  24. prepend: 'Usage: cmd [options]',
  25. append: 'Version 1.0.0',
  26. options: [{
  27. option: 'help',
  28. alias: 'h',
  29. type: 'Boolean',
  30. description: 'displays help'
  31. }, {
  32. option: 'count',
  33. alias: 'c',
  34. type: 'Int',
  35. description: 'number of things',
  36. example: 'cmd --count 2'
  37. }]
  38. });
  39. var options = optionator.parseArgv(process.argv);
  40. if (options.help) {
  41. console.log(optionator.generateHelp());
  42. }
  43. ...
  44. ```
  45. ### parse(input, parseOptions)
  46. `parse` processes the `input` according to your settings, and returns an object with the results.
  47. ##### arguments
  48. * input - `[String] | Object | String` - the input you wish to parse
  49. * parseOptions - `{slice: Int}` - all options optional
  50. - `slice` specifies how much to slice away from the beginning if the input is an array or string - by default `0` for string, `2` for array (works with `process.argv`)
  51. ##### returns
  52. `Object` - the parsed options, each key is a camelCase version of the option name (specified in dash-case), and each value is the processed value for that option. Positional values are in an array under the `_` key.
  53. ##### example
  54. ```js
  55. parse(['node', 't.js', '--count', '2', 'positional']); // {count: 2, _: ['positional']}
  56. parse('--count 2 positional'); // {count: 2, _: ['positional']}
  57. parse({count: 2, _:['positional']}); // {count: 2, _: ['positional']}
  58. ```
  59. ### parseArgv(input)
  60. `parseArgv` works exactly like `parse`, but only for array input and it slices off the first two elements.
  61. ##### arguments
  62. * input - `[String]` - the input you wish to parse
  63. ##### returns
  64. See "returns" section in "parse"
  65. ##### example
  66. ```js
  67. parseArgv(process.argv);
  68. ```
  69. ### generateHelp(helpOptions)
  70. `generateHelp` produces help text based on your settings.
  71. ##### arguments
  72. * helpOptions - `{showHidden: Boolean, interpolate: Object}` - all options optional
  73. - `showHidden` specifies whether to show options with `hidden: true` specified, by default it is `false`
  74. - `interpolate` specify data to be interpolated in `prepend` and `append` text, `{{key}}` is the format - eg. `generateHelp({interpolate:{version: '0.4.2'}})`, will change this `append` text: `Version {{version}}` to `Version 0.4.2`
  75. ##### returns
  76. `String` - the generated help text
  77. ##### example
  78. ```js
  79. generateHelp(); /*
  80. "Usage: cmd [options] positional
  81. -h, --help displays help
  82. -c, --count Int number of things
  83. Version 1.0.0
  84. "*/
  85. ```
  86. ### generateHelpForOption(optionName)
  87. `generateHelpForOption` produces expanded help text for the specified with `optionName` option. If an `example` was specified for the option, it will be displayed, and if a `longDescription` was specified, it will display that instead of the `description`.
  88. ##### arguments
  89. * optionName - `String` - the name of the option to display
  90. ##### returns
  91. `String` - the generated help text for the option
  92. ##### example
  93. ```js
  94. generateHelpForOption('count'); /*
  95. "-c, --count Int
  96. description: number of things
  97. example: cmd --count 2
  98. "*/
  99. ```
  100. ## Settings Format
  101. When your `require('optionator')`, you get a function that takes in a settings object. This object has the type:
  102. {
  103. prepend: String,
  104. append: String,
  105. options: [{heading: String} | {
  106. option: String,
  107. alias: [String] | String,
  108. type: String,
  109. enum: [String],
  110. default: String,
  111. restPositional: Boolean,
  112. required: Boolean,
  113. overrideRequired: Boolean,
  114. dependsOn: [String] | String,
  115. concatRepeatedArrays: Boolean | (Boolean, Object),
  116. mergeRepeatedObjects: Boolean,
  117. description: String,
  118. longDescription: String,
  119. example: [String] | String
  120. }],
  121. helpStyle: {
  122. aliasSeparator: String,
  123. typeSeparator: String,
  124. descriptionSeparator: String,
  125. initialIndent: Int,
  126. secondaryIndent: Int,
  127. maxPadFactor: Number
  128. },
  129. mutuallyExclusive: [[String | [String]]],
  130. concatRepeatedArrays: Boolean | (Boolean, Object), // deprecated, set in defaults object
  131. mergeRepeatedObjects: Boolean, // deprecated, set in defaults object
  132. positionalAnywhere: Boolean,
  133. typeAliases: Object,
  134. defaults: Object
  135. }
  136. All of the properties are optional (the `Maybe` has been excluded for brevities sake), except for having either `heading: String` or `option: String` in each object in the `options` array.
  137. ### Top Level Properties
  138. * `prepend` is an optional string to be placed before the options in the help text
  139. * `append` is an optional string to be placed after the options in the help text
  140. * `options` is a required array specifying your options and headings, the options and headings will be displayed in the order specified
  141. * `helpStyle` is an optional object which enables you to change the default appearance of some aspects of the help text
  142. * `mutuallyExclusive` is an optional array of arrays of either strings or arrays of strings. The top level array is a list of rules, each rule is a list of elements - each element can be either a string (the name of an option), or a list of strings (a group of option names) - there will be an error if more than one element is present
  143. * `concatRepeatedArrays` see description under the "Option Properties" heading - use at the top level is deprecated, if you want to set this for all options, use the `defaults` property
  144. * `mergeRepeatedObjects` see description under the "Option Properties" heading - use at the top level is deprecated, if you want to set this for all options, use the `defaults` property
  145. * `positionalAnywhere` is an optional boolean (defaults to `true`) - when `true` it allows positional arguments anywhere, when `false`, all arguments after the first positional one are taken to be positional as well, even if they look like a flag. For example, with `positionalAnywhere: false`, the arguments `--flag --boom 12 --crack` would have two positional arguments: `12` and `--crack`
  146. * `typeAliases` is an optional object, it allows you to set aliases for types, eg. `{Path: 'String'}` would allow you to use the type `Path` as an alias for the type `String`
  147. * `defaults` is an optional object following the option properties format, which specifies default values for all options. A default will be overridden if manually set. For example, you can do `default: { type: "String" }` to set the default type of all options to `String`, and then override that default in an individual option by setting the `type` property
  148. #### Heading Properties
  149. * `heading` a required string, the name of the heading
  150. #### Option Properties
  151. * `option` the required name of the option - use dash-case, without the leading dashes
  152. * `alias` is an optional string or array of strings which specify any aliases for the option
  153. * `type` is a required string in the [type check](https://github.com/gkz/type-check) [format](https://github.com/gkz/type-check#type-format), this will be used to cast the inputted value and validate it
  154. * `enum` is an optional array of strings, each string will be parsed by [levn](https://github.com/gkz/levn) - the argument value must be one of the resulting values - each potential value must validate against the specified `type`
  155. * `default` is a optional string, which will be parsed by [levn](https://github.com/gkz/levn) and used as the default value if none is set - the value must validate against the specified `type`
  156. * `restPositional` is an optional boolean - if set to `true`, everything after the option will be taken to be a positional argument, even if it looks like a named argument
  157. * `required` is an optional boolean - if set to `true`, the option parsing will fail if the option is not defined
  158. * `overrideRequired` is a optional boolean - if set to `true` and the option is used, and there is another option which is required but not set, it will override the need for the required option and there will be no error - this is useful if you have required options and want to use `--help` or `--version` flags
  159. * `concatRepeatedArrays` is an optional boolean or tuple with boolean and options object (defaults to `false`) - when set to `true` and an option contains an array value and is repeated, the subsequent values for the flag will be appended rather than overwriting the original value - eg. option `g` of type `[String]`: `-g a -g b -g c,d` will result in `['a','b','c','d']`
  160. You can supply an options object by giving the following value: `[true, options]`. The one currently supported option is `oneValuePerFlag`, this only allows one array value per flag. This is useful if your potential values contain a comma.
  161. * `mergeRepeatedObjects` is an optional boolean (defaults to `false`) - when set to `true` and an option contains an object value and is repeated, the subsequent values for the flag will be merged rather than overwriting the original value - eg. option `g` of type `Object`: `-g a:1 -g b:2 -g c:3,d:4` will result in `{a: 1, b: 2, c: 3, d: 4}`
  162. * `dependsOn` is an optional string or array of strings - if simply a string (the name of another option), it will make sure that that other option is set, if an array of strings, depending on whether `'and'` or `'or'` is first, it will either check whether all (`['and', 'option-a', 'option-b']`), or at least one (`['or', 'option-a', 'option-b']`) other options are set
  163. * `description` is an optional string, which will be displayed next to the option in the help text
  164. * `longDescription` is an optional string, it will be displayed instead of the `description` when `generateHelpForOption` is used
  165. * `example` is an optional string or array of strings with example(s) for the option - these will be displayed when `generateHelpForOption` is used
  166. #### Help Style Properties
  167. * `aliasSeparator` is an optional string, separates multiple names from each other - default: ' ,'
  168. * `typeSeparator` is an optional string, separates the type from the names - default: ' '
  169. * `descriptionSeparator` is an optional string , separates the description from the padded name and type - default: ' '
  170. * `initialIndent` is an optional int - the amount of indent for options - default: 2
  171. * `secondaryIndent` is an optional int - the amount of indent if wrapped fully (in addition to the initial indent) - default: 4
  172. * `maxPadFactor` is an optional number - affects the default level of padding for the names/type, it is multiplied by the average of the length of the names/type - default: 1.5
  173. ## Argument Format
  174. At the highest level there are two types of arguments: named, and positional.
  175. Name arguments of any length are prefixed with `--` (eg. `--go`), and those of one character may be prefixed with either `--` or `-` (eg. `-g`).
  176. There are two types of named arguments: boolean flags (eg. `--problemo`, `-p`) which take no value and result in a `true` if they are present, the falsey `undefined` if they are not present, or `false` if present and explicitly prefixed with `no` (eg. `--no-problemo`). Named arguments with values (eg. `--tseries 800`, `-t 800`) are the other type. If the option has a type `Boolean` it will automatically be made into a boolean flag. Any other type results in a named argument that takes a value.
  177. For more information about how to properly set types to get the value you want, take a look at the [type check](https://github.com/gkz/type-check) and [levn](https://github.com/gkz/levn) pages.
  178. You can group single character arguments that use a single `-`, however all except the last must be boolean flags (which take no value). The last may be a boolean flag, or an argument which takes a value - eg. `-ba 2` is equivalent to `-b -a 2`.
  179. Positional arguments are all those values which do not fall under the above - they can be anywhere, not just at the end. For example, in `cmd -b one -a 2 two` where `b` is a boolean flag, and `a` has the type `Number`, there are two positional arguments, `one` and `two`.
  180. Everything after an `--` is positional, even if it looks like a named argument.
  181. You may optionally use `=` to separate option names from values, for example: `--count=2`.
  182. If you specify the option `NUM`, then any argument using a single `-` followed by a number will be valid and will set the value of `NUM`. Eg. `-2` will be parsed into `NUM: 2`.
  183. If duplicate named arguments are present, the last one will be taken.
  184. ## Technical About
  185. `optionator` is written in [LiveScript](http://livescript.net/) - a language that compiles to JavaScript. It uses [levn](https://github.com/gkz/levn) to cast arguments to their specified type, and uses [type-check](https://github.com/gkz/type-check) to validate values. It also uses the [prelude.ls](http://preludels.com/) library.