| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | # minimist |
| 2 | |
| 3 | parse argument options |
| 4 | |
| 5 | This module is the guts of optimist's argument parser without all the |
| 6 | fanciful decoration. |
| 7 | |
| 8 | # example |
| 9 | |
| 10 | ``` js |
| 11 | var argv = require('minimist')(process.argv.slice(2)); |
| 12 | console.log(argv); |
| 13 | ``` |
| 14 | |
| 15 | ``` |
| 16 | $ node example/parse.js -a beep -b boop |
| 17 | { _: [], a: 'beep', b: 'boop' } |
| 18 | ``` |
| 19 | |
| 20 | ``` |
| 21 | $ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz |
| 22 | { _: [ 'foo', 'bar', 'baz' ], |
| 23 | x: 3, |
| 24 | y: 4, |
| 25 | n: 5, |
| 26 | a: true, |
| 27 | b: true, |
| 28 | c: true, |
| 29 | beep: 'boop' } |
| 30 | ``` |
| 31 | |
| 32 | # security |
| 33 | |
| 34 | Previous versions had a prototype pollution bug that could cause privilege |
| 35 | escalation in some circumstances when handling untrusted user input. |
| 36 | |
| 37 | Please use version 1.2.3 or later: https://snyk.io/vuln/SNYK-JS-MINIMIST-559764 |
| 38 | |
| 39 | # methods |
| 40 | |
| 41 | ``` js |
| 42 | var parseArgs = require('minimist') |
| 43 | ``` |
| 44 | |
| 45 | ## var argv = parseArgs(args, opts={}) |
| 46 | |
| 47 | Return an argument object `argv` populated with the array arguments from `args`. |
| 48 | |
| 49 | `argv._` contains all the arguments that didn't have an option associated with |
| 50 | them. |
| 51 | |
| 52 | Numeric-looking arguments will be returned as numbers unless `opts.string` or |
| 53 | `opts.boolean` is set for that argument name. |
| 54 | |
| 55 | Any arguments after `'--'` will not be parsed and will end up in `argv._`. |
| 56 | |
| 57 | options can be: |
| 58 | |
| 59 | * `opts.string` - a string or array of strings argument names to always treat as |
| 60 | strings |
| 61 | * `opts.boolean` - a boolean, string or array of strings to always treat as |
| 62 | booleans. if `true` will treat all double hyphenated arguments without equal signs |
| 63 | as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`) |
| 64 | * `opts.alias` - an object mapping string names to strings or arrays of string |
| 65 | argument names to use as aliases |
| 66 | * `opts.default` - an object mapping string argument names to default values |
| 67 | * `opts.stopEarly` - when true, populate `argv._` with everything after the |
| 68 | first non-option |
| 69 | * `opts['--']` - when true, populate `argv._` with everything before the `--` |
| 70 | and `argv['--']` with everything after the `--`. Here's an example: |
| 71 | |
| 72 | ``` |
| 73 | > require('./')('one two three -- four five --six'.split(' '), { '--': true }) |
| 74 | { _: [ 'one', 'two', 'three' ], |
| 75 | '--': [ 'four', 'five', '--six' ] } |
| 76 | ``` |
| 77 | |
| 78 | Note that with `opts['--']` set, parsing for arguments still stops after the |
| 79 | `--`. |
| 80 | |
| 81 | * `opts.unknown` - a function which is invoked with a command line parameter not |
| 82 | defined in the `opts` configuration object. If the function returns `false`, the |
| 83 | unknown option is not added to `argv`. |
| 84 | |
| 85 | # install |
| 86 | |
| 87 | With [npm](https://npmjs.org) do: |
| 88 | |
| 89 | ``` |
| 90 | npm install minimist |
| 91 | ``` |
| 92 | |
| 93 | # license |
| 94 | |
| 95 | MIT |