basic.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. var nopt = require("../")
  2. , test = require('tap').test
  3. test("passing a string results in a string", function (t) {
  4. var parsed = nopt({ key: String }, {}, ["--key", "myvalue"], 0)
  5. t.same(parsed.key, "myvalue")
  6. t.end()
  7. })
  8. // https://github.com/npm/nopt/issues/31
  9. test("Empty String results in empty string, not true", function (t) {
  10. var parsed = nopt({ empty: String }, {}, ["--empty"], 0)
  11. t.same(parsed.empty, "")
  12. t.end()
  13. })
  14. test("~ path is resolved to $HOME", function (t) {
  15. var path = require("path")
  16. if (!process.env.HOME) process.env.HOME = "/tmp"
  17. var parsed = nopt({key: path}, {}, ["--key=~/val"], 0)
  18. t.same(parsed.key, path.resolve(process.env.HOME, "val"))
  19. t.end()
  20. })
  21. // https://github.com/npm/nopt/issues/24
  22. test("Unknown options are not parsed as numbers", function (t) {
  23. var parsed = nopt({"parse-me": Number}, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0)
  24. t.equal(parsed['leave-as-is'], '1.20')
  25. t.equal(parsed['parse-me'], 1.2)
  26. t.end()
  27. });
  28. // https://github.com/npm/nopt/issues/48
  29. test("Check types based on name of type", function (t) {
  30. var parsed = nopt({"parse-me": {name: "Number"}}, null, ['--parse-me=1.20'], 0)
  31. t.equal(parsed['parse-me'], 1.2)
  32. t.end()
  33. })
  34. test("Missing types are not parsed", function (t) {
  35. var parsed = nopt({"parse-me": {}}, null, ['--parse-me=1.20'], 0)
  36. //should only contain argv
  37. t.equal(Object.keys(parsed).length, 1)
  38. t.end()
  39. })
  40. test("Types passed without a name are not parsed", function (t) {
  41. var parsed = nopt({"parse-me": {}}, {}, ['--parse-me=1.20'], 0)
  42. //should only contain argv
  43. t.equal(Object.keys(parsed).length, 1)
  44. t.end()
  45. })
  46. test("other tests", function (t) {
  47. var util = require("util")
  48. , Stream = require("stream")
  49. , path = require("path")
  50. , url = require("url")
  51. , shorthands =
  52. { s : ["--loglevel", "silent"]
  53. , d : ["--loglevel", "info"]
  54. , dd : ["--loglevel", "verbose"]
  55. , ddd : ["--loglevel", "silly"]
  56. , noreg : ["--no-registry"]
  57. , reg : ["--registry"]
  58. , "no-reg" : ["--no-registry"]
  59. , silent : ["--loglevel", "silent"]
  60. , verbose : ["--loglevel", "verbose"]
  61. , h : ["--usage"]
  62. , H : ["--usage"]
  63. , "?" : ["--usage"]
  64. , help : ["--usage"]
  65. , v : ["--version"]
  66. , f : ["--force"]
  67. , desc : ["--description"]
  68. , "no-desc" : ["--no-description"]
  69. , "local" : ["--no-global"]
  70. , l : ["--long"]
  71. , p : ["--parseable"]
  72. , porcelain : ["--parseable"]
  73. , g : ["--global"]
  74. }
  75. , types =
  76. { aoa: Array
  77. , nullstream: [null, Stream]
  78. , date: Date
  79. , str: String
  80. , browser : String
  81. , cache : path
  82. , color : ["always", Boolean]
  83. , depth : Number
  84. , description : Boolean
  85. , dev : Boolean
  86. , editor : path
  87. , force : Boolean
  88. , global : Boolean
  89. , globalconfig : path
  90. , group : [String, Number]
  91. , gzipbin : String
  92. , logfd : [Number, Stream]
  93. , loglevel : ["silent","win","error","warn","info","verbose","silly"]
  94. , long : Boolean
  95. , "node-version" : [false, String]
  96. , npaturl : url
  97. , npat : Boolean
  98. , "onload-script" : [false, String]
  99. , outfd : [Number, Stream]
  100. , parseable : Boolean
  101. , pre: Boolean
  102. , prefix: path
  103. , proxy : url
  104. , "rebuild-bundle" : Boolean
  105. , registry : url
  106. , searchopts : String
  107. , searchexclude: [null, String]
  108. , shell : path
  109. , t: [Array, String]
  110. , tag : String
  111. , tar : String
  112. , tmp : path
  113. , "unsafe-perm" : Boolean
  114. , usage : Boolean
  115. , user : String
  116. , username : String
  117. , userconfig : path
  118. , version : Boolean
  119. , viewer: path
  120. , _exit : Boolean
  121. , path: path
  122. }
  123. ; [["-v", {version:true}, []]
  124. ,["---v", {version:true}, []]
  125. ,["ls -s --no-reg connect -d",
  126. {loglevel:"info",registry:null},["ls","connect"]]
  127. ,["ls ---s foo",{loglevel:"silent"},["ls","foo"]]
  128. ,["ls --registry blargle", {}, ["ls"]]
  129. ,["--no-registry", {registry:null}, []]
  130. ,["--no-color true", {color:false}, []]
  131. ,["--no-color false", {color:true}, []]
  132. ,["--no-color", {color:false}, []]
  133. ,["--color false", {color:false}, []]
  134. ,["--color --logfd 7", {logfd:7,color:true}, []]
  135. ,["--color=true", {color:true}, []]
  136. ,["--logfd=10", {logfd:10}, []]
  137. ,["--tmp=/tmp -tar=gtar",{tmp:"/tmp",tar:"gtar"},[]]
  138. ,["--tmp=tmp -tar=gtar",
  139. {tmp:path.resolve(process.cwd(), "tmp"),tar:"gtar"},[]]
  140. ,["--logfd x", {}, []]
  141. ,["a -true -- -no-false", {true:true},["a","-no-false"]]
  142. ,["a -no-false", {false:false},["a"]]
  143. ,["a -no-no-true", {true:true}, ["a"]]
  144. ,["a -no-no-no-false", {false:false}, ["a"]]
  145. ,["---NO-no-No-no-no-no-nO-no-no"+
  146. "-No-no-no-no-no-no-no-no-no"+
  147. "-no-no-no-no-NO-NO-no-no-no-no-no-no"+
  148. "-no-body-can-do-the-boogaloo-like-I-do"
  149. ,{"body-can-do-the-boogaloo-like-I-do":false}, []]
  150. ,["we are -no-strangers-to-love "+
  151. "--you-know=the-rules --and=so-do-i "+
  152. "---im-thinking-of=a-full-commitment "+
  153. "--no-you-would-get-this-from-any-other-guy "+
  154. "--no-gonna-give-you-up "+
  155. "-no-gonna-let-you-down=true "+
  156. "--no-no-gonna-run-around false "+
  157. "--desert-you=false "+
  158. "--make-you-cry false "+
  159. "--no-tell-a-lie "+
  160. "--no-no-and-hurt-you false"
  161. ,{"strangers-to-love":false
  162. ,"you-know":"the-rules"
  163. ,"and":"so-do-i"
  164. ,"you-would-get-this-from-any-other-guy":false
  165. ,"gonna-give-you-up":false
  166. ,"gonna-let-you-down":false
  167. ,"gonna-run-around":false
  168. ,"desert-you":false
  169. ,"make-you-cry":false
  170. ,"tell-a-lie":false
  171. ,"and-hurt-you":false
  172. },["we", "are"]]
  173. ,["-t one -t two -t three"
  174. ,{t: ["one", "two", "three"]}
  175. ,[]]
  176. ,["-t one -t null -t three four five null"
  177. ,{t: ["one", "null", "three"]}
  178. ,["four", "five", "null"]]
  179. ,["-t foo"
  180. ,{t:["foo"]}
  181. ,[]]
  182. ,["--no-t"
  183. ,{t:["false"]}
  184. ,[]]
  185. ,["-no-no-t"
  186. ,{t:["true"]}
  187. ,[]]
  188. ,["-aoa one -aoa null -aoa 100"
  189. ,{aoa:["one", null, '100']}
  190. ,[]]
  191. ,["-str 100"
  192. ,{str:"100"}
  193. ,[]]
  194. ,["--color always"
  195. ,{color:"always"}
  196. ,[]]
  197. ,["--no-nullstream"
  198. ,{nullstream:null}
  199. ,[]]
  200. ,["--nullstream false"
  201. ,{nullstream:null}
  202. ,[]]
  203. ,["--notadate=2011-01-25"
  204. ,{notadate: "2011-01-25"}
  205. ,[]]
  206. ,["--date 2011-01-25"
  207. ,{date: new Date("2011-01-25")}
  208. ,[]]
  209. ,["-cl 1"
  210. ,{config: true, length: 1}
  211. ,[]
  212. ,{config: Boolean, length: Number, clear: Boolean}
  213. ,{c: "--config", l: "--length"}]
  214. ,["--acount bla"
  215. ,{"acount":true}
  216. ,["bla"]
  217. ,{account: Boolean, credentials: Boolean, options: String}
  218. ,{a:"--account", c:"--credentials",o:"--options"}]
  219. ,["--clear"
  220. ,{clear:true}
  221. ,[]
  222. ,{clear:Boolean,con:Boolean,len:Boolean,exp:Boolean,add:Boolean,rep:Boolean}
  223. ,{c:"--con",l:"--len",e:"--exp",a:"--add",r:"--rep"}]
  224. ,["--file -"
  225. ,{"file":"-"}
  226. ,[]
  227. ,{file:String}
  228. ,{}]
  229. ,["--file -"
  230. ,{"file":true}
  231. ,["-"]
  232. ,{file:Boolean}
  233. ,{}]
  234. ,["--path"
  235. ,{"path":null}
  236. ,[]]
  237. ,["--path ."
  238. ,{"path":process.cwd()}
  239. ,[]]
  240. ].forEach(function (test) {
  241. var argv = test[0].split(/\s+/)
  242. , opts = test[1]
  243. , rem = test[2]
  244. , actual = nopt(test[3] || types, test[4] || shorthands, argv, 0)
  245. , parsed = actual.argv
  246. delete actual.argv
  247. for (var i in opts) {
  248. var e = JSON.stringify(opts[i])
  249. , a = JSON.stringify(actual[i] === undefined ? null : actual[i])
  250. if (e && typeof e === "object") {
  251. t.deepEqual(e, a)
  252. } else {
  253. t.equal(e, a)
  254. }
  255. }
  256. t.deepEqual(rem, parsed.remain)
  257. })
  258. t.end()
  259. })