選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. If you want to write an option parser, and have it be good, there are
  2. two ways to do it. The Right Way, and the Wrong Way.
  3. The Wrong Way is to sit down and write an option parser. We've all done
  4. that.
  5. The Right Way is to write some complex configurable program with so many
  6. options that you go half-insane just trying to manage them all, and put
  7. it off with duct-tape solutions until you see exactly to the core of the
  8. problem, and finally snap and write an awesome option parser.
  9. If you want to write an option parser, don't write an option parser.
  10. Write a package manager, or a source control system, or a service
  11. restarter, or an operating system. You probably won't end up with a
  12. good one of those, but if you don't give up, and you are relentless and
  13. diligent enough in your procrastination, you may just end up with a very
  14. nice option parser.
  15. ## USAGE
  16. // my-program.js
  17. var nopt = require("nopt")
  18. , Stream = require("stream").Stream
  19. , path = require("path")
  20. , knownOpts = { "foo" : [String, null]
  21. , "bar" : [Stream, Number]
  22. , "baz" : path
  23. , "bloo" : [ "big", "medium", "small" ]
  24. , "flag" : Boolean
  25. , "pick" : Boolean
  26. , "many" : [String, Array]
  27. }
  28. , shortHands = { "foofoo" : ["--foo", "Mr. Foo"]
  29. , "b7" : ["--bar", "7"]
  30. , "m" : ["--bloo", "medium"]
  31. , "p" : ["--pick"]
  32. , "f" : ["--flag"]
  33. }
  34. // everything is optional.
  35. // knownOpts and shorthands default to {}
  36. // arg list defaults to process.argv
  37. // slice defaults to 2
  38. , parsed = nopt(knownOpts, shortHands, process.argv, 2)
  39. console.log(parsed)
  40. This would give you support for any of the following:
  41. ```bash
  42. $ node my-program.js --foo "blerp" --no-flag
  43. { "foo" : "blerp", "flag" : false }
  44. $ node my-program.js ---bar 7 --foo "Mr. Hand" --flag
  45. { bar: 7, foo: "Mr. Hand", flag: true }
  46. $ node my-program.js --foo "blerp" -f -----p
  47. { foo: "blerp", flag: true, pick: true }
  48. $ node my-program.js -fp --foofoo
  49. { foo: "Mr. Foo", flag: true, pick: true }
  50. $ node my-program.js --foofoo -- -fp # -- stops the flag parsing.
  51. { foo: "Mr. Foo", argv: { remain: ["-fp"] } }
  52. $ node my-program.js --blatzk 1000 -fp # unknown opts are ok.
  53. { blatzk: 1000, flag: true, pick: true }
  54. $ node my-program.js --blatzk true -fp # but they need a value
  55. { blatzk: true, flag: true, pick: true }
  56. $ node my-program.js --no-blatzk -fp # unless they start with "no-"
  57. { blatzk: false, flag: true, pick: true }
  58. $ node my-program.js --baz b/a/z # known paths are resolved.
  59. { baz: "/Users/isaacs/b/a/z" }
  60. # if Array is one of the types, then it can take many
  61. # values, and will always be an array. The other types provided
  62. # specify what types are allowed in the list.
  63. $ node my-program.js --many 1 --many null --many foo
  64. { many: ["1", "null", "foo"] }
  65. $ node my-program.js --many foo
  66. { many: ["foo"] }
  67. ```
  68. Read the tests at the bottom of `lib/nopt.js` for more examples of
  69. what this puppy can do.
  70. ## Types
  71. The following types are supported, and defined on `nopt.typeDefs`
  72. * String: A normal string. No parsing is done.
  73. * path: A file system path. Gets resolved against cwd if not absolute.
  74. * url: A url. If it doesn't parse, it isn't accepted.
  75. * Number: Must be numeric.
  76. * Date: Must parse as a date. If it does, and `Date` is one of the options,
  77. then it will return a Date object, not a string.
  78. * Boolean: Must be either `true` or `false`. If an option is a boolean,
  79. then it does not need a value, and its presence will imply `true` as
  80. the value. To negate boolean flags, do `--no-whatever` or `--whatever
  81. false`
  82. * NaN: Means that the option is strictly not allowed. Any value will
  83. fail.
  84. * Stream: An object matching the "Stream" class in node. Valuable
  85. for use when validating programmatically. (npm uses this to let you
  86. supply any WriteStream on the `outfd` and `logfd` config options.)
  87. * Array: If `Array` is specified as one of the types, then the value
  88. will be parsed as a list of options. This means that multiple values
  89. can be specified, and that the value will always be an array.
  90. If a type is an array of values not on this list, then those are
  91. considered valid values. For instance, in the example above, the
  92. `--bloo` option can only be one of `"big"`, `"medium"`, or `"small"`,
  93. and any other value will be rejected.
  94. When parsing unknown fields, `"true"`, `"false"`, and `"null"` will be
  95. interpreted as their JavaScript equivalents, and numeric values will be
  96. interpreted as a number.
  97. You can also mix types and values, or multiple types, in a list. For
  98. instance `{ blah: [Number, null] }` would allow a value to be set to
  99. either a Number or null.
  100. To define a new type, add it to `nopt.typeDefs`. Each item in that
  101. hash is an object with a `type` member and a `validate` method. The
  102. `type` member is an object that matches what goes in the type list. The
  103. `validate` method is a function that gets called with `validate(data,
  104. key, val)`. Validate methods should assign `data[key]` to the valid
  105. value of `val` if it can be handled properly, or return boolean
  106. `false` if it cannot.
  107. You can also call `nopt.clean(data, types, typeDefs)` to clean up a
  108. config object and remove its invalid properties.
  109. ## Error Handling
  110. By default, nopt outputs a warning to standard error when invalid
  111. options are found. You can change this behavior by assigning a method
  112. to `nopt.invalidHandler`. This method will be called with
  113. the offending `nopt.invalidHandler(key, val, types)`.
  114. If no `nopt.invalidHandler` is assigned, then it will console.error
  115. its whining. If it is assigned to boolean `false` then the warning is
  116. suppressed.
  117. ## Abbreviations
  118. Yes, they are supported. If you define options like this:
  119. ```javascript
  120. { "foolhardyelephants" : Boolean
  121. , "pileofmonkeys" : Boolean }
  122. ```
  123. Then this will work:
  124. ```bash
  125. node program.js --foolhar --pil
  126. node program.js --no-f --pileofmon
  127. # etc.
  128. ```
  129. ## Shorthands
  130. Shorthands are a hash of shorter option names to a snippet of args that
  131. they expand to.
  132. If multiple one-character shorthands are all combined, and the
  133. combination does not unambiguously match any other option or shorthand,
  134. then they will be broken up into their constituent parts. For example:
  135. ```json
  136. { "s" : ["--loglevel", "silent"]
  137. , "g" : "--global"
  138. , "f" : "--force"
  139. , "p" : "--parseable"
  140. , "l" : "--long"
  141. }
  142. ```
  143. ```bash
  144. npm ls -sgflp
  145. # just like doing this:
  146. npm ls --loglevel silent --global --force --long --parseable
  147. ```
  148. ## The Rest of the args
  149. The config object returned by nopt is given a special member called
  150. `argv`, which is an object with the following fields:
  151. * `remain`: The remaining args after all the parsing has occurred.
  152. * `original`: The args as they originally appeared.
  153. * `cooked`: The args after flags and shorthands are expanded.
  154. ## Slicing
  155. Node programs are called with more or less the exact argv as it appears
  156. in C land, after the v8 and node-specific options have been plucked off.
  157. As such, `argv[0]` is always `node` and `argv[1]` is always the
  158. JavaScript program being run.
  159. That's usually not very useful to you. So they're sliced off by
  160. default. If you want them, then you can pass in `0` as the last
  161. argument, or any other number that you'd like to slice off the start of
  162. the list.