Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # mri [![Build Status](https://travis-ci.org/lukeed/mri.svg?branch=master)](https://travis-ci.org/lukeed/mri)
  2. > Quickly scan for CLI flags and arguments
  3. This is a [fast](#benchmarks) and lightweight alternative to [`minimist`](https://github.com/substack/minimist) and [`yargs-parser`](https://github.com/yargs/yargs-parser).
  4. It only exists because I find that I usually don't need most of what `minimist` and `yargs-parser` have to offer. However, `mri` is similar _enough_ that it might function as a "drop-in replacement" for you, too!
  5. See [Comparisons](#comparisons) for more info.
  6. ## Install
  7. ```sh
  8. $ npm install --save mri
  9. ```
  10. ## Usage
  11. ```sh
  12. $ demo-cli --foo --bar=baz -mtv -- hello world
  13. ```
  14. ```js
  15. const mri = require('mri');
  16. const argv = process.argv.slice(2);
  17. mri(argv);
  18. //=> { _: ['hello', 'world'], foo:true, bar:'baz', m:true, t:true, v:true }
  19. mri(argv, { boolean:['bar'] });
  20. //=> { _: ['baz', 'hello', 'world'], foo:true, bar:true, m:true, t:true, v:true }
  21. mri(argv, {
  22. alias: {
  23. b: 'bar',
  24. foo: ['f', 'fuz']
  25. }
  26. });
  27. //=> { _: ['hello', 'world'], foo:true, f:true, fuz:true, b:'baz', bar:'baz', m:true, t:true, v:true }
  28. ```
  29. ## API
  30. ### mri(args, options)
  31. Return: `Object`
  32. #### args
  33. Type: `Array`<br>
  34. Default: `[]`
  35. An array of arguments to parse. For CLI usage, send `process.argv.slice(2)`. See [`process.argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv) for info.
  36. #### options.alias
  37. Type: `Object`<br>
  38. Default: `{}`
  39. An object of keys whose values are `String`s or `Array<String>` of aliases. These will be added to the parsed output with matching values.
  40. #### options.boolean
  41. Type: `Array|String`<br>
  42. Default: `[]`
  43. A single key (or array of keys) that should be parsed as `Boolean`s.
  44. #### options.default
  45. Type: `Object`<br>
  46. Default: `{}`
  47. An `key:value` object of defaults. If a default is provided for a key, its type (`typeof`) will be used to cast parsed arguments.
  48. ```js
  49. mri(['--foo', 'bar']);
  50. //=> { _:[], foo:'bar' }
  51. mri(['--foo', 'bar'], {
  52. default: { foo:true, baz:'hello', bat:42 }
  53. });
  54. //=> { _:['bar'], foo:true, baz:'hello', bat:42 }
  55. ```
  56. > **Note:** Because `--foo` has a default of `true`, its output is cast to a Boolean. This means that `foo=true`, making `'bar'` an extra argument (`_` key).
  57. #### options.string
  58. Type: `Array|String`<br>
  59. Default: `[]`
  60. A single key (or array of keys) that should be parsed as `String`s.
  61. #### options.unknown
  62. Type: `Function`<br>
  63. Default: `undefined`
  64. Callback that is run when a parsed flag has not been defined as a known key or alias. Its only parameter is the unknown flag itself; eg `--foobar` or `-f`.
  65. Once an unknown flag is encountered, parsing will terminate, regardless of your return value.
  66. > **Note:** `mri` _only_ checks for unknown flags if `options.unknown` **and** `options.alias` are populated. Otherwise, everything will be accepted.
  67. ## Comparisons
  68. #### minimist
  69. - `mri` is 5x faster (see [benchmarks](#benchmarks))
  70. - Numerical values are cast as `Number`s when possible
  71. - A key (and its aliases) will always honor `opts.boolean` or `opts.string`
  72. - Short flag groups are treated as `Boolean`s by default:
  73. ```js
  74. minimist(['-abc', 'hello']);
  75. //=> { _:[], a:'', b:'', c:'hello' }
  76. mri(['-abc', 'hello']);
  77. //=> { _:[], a:true, b:true, c:'hello' }
  78. ```
  79. - The `opts.unknown` behaves differently:
  80. - Unlike `minimist`, `mri` will not continue continue parsing after encountering an unknown flag
  81. - Missing `options`:
  82. - `opts.stopEarly`
  83. - `opts['--']`
  84. - Ignores newlines (`\n`) within args (see [test](https://github.com/substack/minimist/blob/master/test/parse.js#L69-L80))
  85. - Ignores slashBreaks within args (see [test](https://github.com/substack/minimist/blob/master/test/parse.js#L147-L157))
  86. - Ignores dot-nested flags (see [test](https://github.com/substack/minimist/blob/master/test/parse.js#L180-L197))
  87. #### yargs-parser
  88. - `mri` is 40x faster (see [benchmarks](#benchmarks))
  89. - Numerical values are cast as `Number`s when possible
  90. - A key (and its aliases) will always honor `opts.boolean` or `opts.string`
  91. - Missing `options`:
  92. - `opts.array`
  93. - `opts.config`
  94. - `opts.coerce`
  95. - `opts.count`
  96. - `opts.envPrefix`
  97. - `opts.narg`
  98. - `opts.normalize`
  99. - `opts.configuration`
  100. - `opts.number`
  101. - `opts['--']`
  102. - Missing [`parser.detailed()`](https://github.com/yargs/yargs-parser#requireyargs-parserdetailedargs-opts) method
  103. - No [additional configuration](https://github.com/yargs/yargs-parser#configuration) object
  104. - Added [`options.unknown`](#optionsunknown) feature
  105. ## Benchmarks
  106. > Running Node.js v10.13.0
  107. ```
  108. minimist x 312,417 ops/sec ±0.85% (93 runs sampled)
  109. mri x 1,641,208 ops/sec ±0.24% (93 runs sampled)
  110. nopt x 910,276 ops/sec ±1.11% (88 runs sampled)
  111. yargs-parser x 40,943 ops/sec ±1.37% (93 runs sampled)
  112. ```
  113. ## License
  114. MIT © [Luke Edwards](https://lukeed.com)