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.

91 lines
2.4 KiB

7 years ago
  1. # minimist
  2. parse argument options
  3. This module is the guts of optimist's argument parser without all the
  4. fanciful decoration.
  5. [![browser support](https://ci.testling.com/substack/minimist.png)](http://ci.testling.com/substack/minimist)
  6. [![build status](https://secure.travis-ci.org/substack/minimist.png)](http://travis-ci.org/substack/minimist)
  7. # example
  8. ``` js
  9. var argv = require('minimist')(process.argv.slice(2));
  10. console.dir(argv);
  11. ```
  12. ```
  13. $ node example/parse.js -a beep -b boop
  14. { _: [], a: 'beep', b: 'boop' }
  15. ```
  16. ```
  17. $ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
  18. { _: [ 'foo', 'bar', 'baz' ],
  19. x: 3,
  20. y: 4,
  21. n: 5,
  22. a: true,
  23. b: true,
  24. c: true,
  25. beep: 'boop' }
  26. ```
  27. # methods
  28. ``` js
  29. var parseArgs = require('minimist')
  30. ```
  31. ## var argv = parseArgs(args, opts={})
  32. Return an argument object `argv` populated with the array arguments from `args`.
  33. `argv._` contains all the arguments that didn't have an option associated with
  34. them.
  35. Numeric-looking arguments will be returned as numbers unless `opts.string` or
  36. `opts.boolean` is set for that argument name.
  37. Any arguments after `'--'` will not be parsed and will end up in `argv._`.
  38. options can be:
  39. * `opts.string` - a string or array of strings argument names to always treat as
  40. strings
  41. * `opts.boolean` - a boolean, string or array of strings to always treat as
  42. booleans. if `true` will treat all double hyphenated arguments without equal signs
  43. as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
  44. * `opts.alias` - an object mapping string names to strings or arrays of string
  45. argument names to use as aliases
  46. * `opts.default` - an object mapping string argument names to default values
  47. * `opts.stopEarly` - when true, populate `argv._` with everything after the
  48. first non-option
  49. * `opts['--']` - when true, populate `argv._` with everything before the `--`
  50. and `argv['--']` with everything after the `--`. Here's an example:
  51. * `opts.unknown` - a function which is invoked with a command line parameter not
  52. defined in the `opts` configuration object. If the function returns `false`, the
  53. unknown option is not added to `argv`.
  54. ```
  55. > require('./')('one two three -- four five --six'.split(' '), { '--': true })
  56. { _: [ 'one', 'two', 'three' ],
  57. '--': [ 'four', 'five', '--six' ] }
  58. ```
  59. Note that with `opts['--']` set, parsing for arguments still stops after the
  60. `--`.
  61. # install
  62. With [npm](https://npmjs.org) do:
  63. ```
  64. npm install minimist
  65. ```
  66. # license
  67. MIT