my-program.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env node
  2. //process.env.DEBUG_NOPT = 1
  3. // my-program.js
  4. var nopt = require("../lib/nopt")
  5. , Stream = require("stream").Stream
  6. , path = require("path")
  7. , knownOpts = { "foo" : [String, null]
  8. , "bar" : [Stream, Number]
  9. , "baz" : path
  10. , "bloo" : [ "big", "medium", "small" ]
  11. , "flag" : Boolean
  12. , "pick" : Boolean
  13. }
  14. , shortHands = { "foofoo" : ["--foo", "Mr. Foo"]
  15. , "b7" : ["--bar", "7"]
  16. , "m" : ["--bloo", "medium"]
  17. , "p" : ["--pick"]
  18. , "f" : ["--flag", "true"]
  19. , "g" : ["--flag"]
  20. , "s" : "--flag"
  21. }
  22. // everything is optional.
  23. // knownOpts and shorthands default to {}
  24. // arg list defaults to process.argv
  25. // slice defaults to 2
  26. , parsed = nopt(knownOpts, shortHands, process.argv, 2)
  27. console.log("parsed =\n"+ require("util").inspect(parsed))