parse.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. var should = require('chai').should(),
  2. yargs = require('../'),
  3. path = require('path');
  4. describe('parse', function () {
  5. it('should pass when specifying a "short boolean"', function () {
  6. var parse = yargs.parse([ '-b' ]);
  7. parse.should.have.property('b').to.be.ok.and.be.a('boolean');
  8. parse.should.have.property('_').with.length(0);
  9. });
  10. it('should pass when specifying a "long boolean"', function () {
  11. var parse = yargs.parse(['--bool']);
  12. parse.should.have.property('bool', true);
  13. parse.should.have.property('_').with.length(0);
  14. });
  15. it('should place bare options in the _ array', function () {
  16. var parse = yargs.parse(['foo', 'bar', 'baz']);
  17. parse.should.have.property('_').and.deep.equal(['foo','bar','baz']);
  18. });
  19. it('should expand grouped short options to a hash with a key for each', function () {
  20. var parse = yargs.parse(['-cats']);
  21. parse.should.have.property('c', true);
  22. parse.should.have.property('a', true);
  23. parse.should.have.property('t', true);
  24. parse.should.have.property('s', true);
  25. parse.should.have.property('_').with.length(0);
  26. });
  27. it('should set the value of the final option in a group to the next supplied value', function () {
  28. var parse = yargs.parse(['-cats', 'meow']);
  29. parse.should.have.property('c', true);
  30. parse.should.have.property('a', true);
  31. parse.should.have.property('t', true);
  32. parse.should.have.property('s', 'meow');
  33. parse.should.have.property('_').with.length(0);
  34. });
  35. it('should set the value of a single short option to the next supplied value', function () {
  36. var parse = yargs.parse(['-h', 'localhost']);
  37. parse.should.have.property('h', 'localhost');
  38. parse.should.have.property('_').with.length(0);
  39. });
  40. it('should set the value of multiple single short options to the next supplied values relative to each', function () {
  41. var parse = yargs.parse(['-h', 'localhost', '-p', '555']);
  42. parse.should.have.property('h', 'localhost');
  43. parse.should.have.property('p', 555);
  44. parse.should.have.property('_').with.length(0);
  45. });
  46. it('should set the value of a single long option to the next supplied value', function () {
  47. var parse = yargs.parse(['--pow', 'xixxle']);
  48. parse.should.have.property('pow', 'xixxle');
  49. parse.should.have.property('_').with.length(0);
  50. });
  51. it('should set the value of a single long option if an = was used', function () {
  52. var parse = yargs.parse(['--pow=xixxle']);
  53. parse.should.have.property('pow', 'xixxle');
  54. parse.should.have.property('_').with.length(0);
  55. });
  56. it('should set the value of multiple long options to the next supplied values relative to each', function () {
  57. var parse = yargs.parse(['--host', 'localhost', '--port', '555']);
  58. parse.should.have.property('host', 'localhost');
  59. parse.should.have.property('port', 555);
  60. parse.should.have.property('_').with.length(0);
  61. });
  62. it('should set the value of multiple long options if = signs were used', function () {
  63. var parse = yargs.parse(['--host=localhost', '--port=555']);
  64. parse.should.have.property('host', 'localhost');
  65. parse.should.have.property('port', 555);
  66. parse.should.have.property('_').with.length(0);
  67. });
  68. it('should still set values appropriately if a mix of short, long, and grouped short options are specified', function () {
  69. var parse = yargs.parse(['-h', 'localhost', '-fp', '555', 'script.js']);
  70. parse.should.have.property('f', true);
  71. parse.should.have.property('p', 555);
  72. parse.should.have.property('h', 'localhost');
  73. parse.should.have.property('_').and.deep.equal(['script.js']);
  74. });
  75. it('should still set values appropriately if a mix of short and long options are specified', function () {
  76. var parse = yargs.parse(['-h', 'localhost', '--port', '555']);
  77. parse.should.have.property('h', 'localhost');
  78. parse.should.have.property('port', 555);
  79. parse.should.have.property('_').with.length(0);
  80. });
  81. it('should explicitly set a boolean option to false if preceeded by "--no-"', function () {
  82. var parse = yargs.parse(['--no-moo']);
  83. parse.should.have.property('moo', false);
  84. parse.should.have.property('_').with.length(0);
  85. });
  86. it('should group values into an array if the same option is specified multiple times', function () {
  87. var parse = yargs.parse(['-v', 'a', '-v', 'b', '-v', 'c' ]);
  88. parse.should.have.property('v').and.deep.equal(['a','b','c']);
  89. parse.should.have.property('_').with.length(0);
  90. });
  91. it('should still set values appropriately if we supply a comprehensive list of various types of options', function () {
  92. var parse = yargs.parse([
  93. '--name=meowmers', 'bare', '-cats', 'woo',
  94. '-h', 'awesome', '--multi=quux',
  95. '--key', 'value',
  96. '-b', '--bool', '--no-meep', '--multi=baz',
  97. '--', '--not-a-flag', 'eek'
  98. ]);
  99. parse.should.have.property('c', true);
  100. parse.should.have.property('a', true);
  101. parse.should.have.property('t', true);
  102. parse.should.have.property('s', 'woo');
  103. parse.should.have.property('h', 'awesome');
  104. parse.should.have.property('b', true);
  105. parse.should.have.property('bool', true);
  106. parse.should.have.property('key', 'value');
  107. parse.should.have.property('multi').and.deep.equal(['quux', 'baz']);
  108. parse.should.have.property('meep', false);
  109. parse.should.have.property('name', 'meowmers');
  110. parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', 'eek']);
  111. });
  112. it('should parse numbers appropriately', function () {
  113. var argv = yargs.parse([
  114. '-x', '1234',
  115. '-y', '5.67',
  116. '-z', '1e7',
  117. '-w', '10f',
  118. '--hex', '0xdeadbeef',
  119. '789',
  120. ]);
  121. argv.should.have.property('x', 1234).and.be.a('number');
  122. argv.should.have.property('y', 5.67).and.be.a('number');
  123. argv.should.have.property('z', 1e7).and.be.a('number');
  124. argv.should.have.property('w', '10f').and.be.a('string');
  125. argv.should.have.property('hex', 0xdeadbeef).and.be.a('number');
  126. argv.should.have.property('_').and.deep.equal([789]);
  127. argv._[0].should.be.a('number');
  128. });
  129. it('should not set the next value as the value of a short option if that option is explicitly defined as a boolean', function () {
  130. var parse = yargs([ '-t', 'moo' ]).boolean(['t']).argv;
  131. parse.should.have.property('t', true).and.be.a('boolean');
  132. parse.should.have.property('_').and.deep.equal(['moo']);
  133. });
  134. it('should set boolean options values if the next value is "true" or "false"', function () {
  135. var parse = yargs(['--verbose', 'false', 'moo', '-t', 'true'])
  136. .boolean(['t', 'verbose']).default('verbose', true).argv;
  137. parse.should.have.property('verbose', false).and.be.a('boolean');
  138. parse.should.have.property('t', true).and.be.a('boolean');
  139. parse.should.have.property('_').and.deep.equal(['moo']);
  140. });
  141. it('should set boolean options to false by default', function () {
  142. var parse = yargs(['moo'])
  143. .boolean(['t', 'verbose'])
  144. .default('verbose', false)
  145. .default('t', false).argv;
  146. parse.should.have.property('verbose', false).and.be.a('boolean');
  147. parse.should.have.property('t', false).and.be.a('boolean');
  148. parse.should.have.property('_').and.deep.equal(['moo']);
  149. });
  150. it('should allow defining options as boolean in groups', function () {
  151. var parse = yargs([ '-x', '-z', 'one', 'two', 'three' ])
  152. .boolean(['x','y','z']).argv;
  153. parse.should.have.property('x', true).and.be.a('boolean');
  154. parse.should.have.property('y', false).and.be.a('boolean');
  155. parse.should.have.property('z', true).and.be.a('boolean');
  156. parse.should.have.property('_').and.deep.equal(['one','two','three']);
  157. });
  158. it('should preserve newlines in option values' , function () {
  159. var args = yargs.parse(['-s', "X\nX"]);
  160. args.should.have.property('_').with.length(0);
  161. args.should.have.property('s', 'X\nX');
  162. // reproduce in bash:
  163. // VALUE="new
  164. // line"
  165. // node program.js --s="$VALUE"
  166. args = yargs.parse(["--s=X\nX"]);
  167. args.should.have.property('_').with.length(0);
  168. args.should.have.property('s', 'X\nX');
  169. });
  170. it('should not convert numbers to type number if explicitly defined as strings' , function () {
  171. var s = yargs([ '-s', '0001234' ]).string('s').argv.s;
  172. s.should.be.a('string').and.equal('0001234');
  173. var x = yargs([ '-x', '56' ]).string('x').argv.x;
  174. x.should.be.a('string').and.equal('56');
  175. });
  176. it('should leave all non-hyphenated values as strings if _ is defined as a string', function () {
  177. var s = yargs([ ' ', ' ' ]).string('_').argv._;
  178. s.should.have.length(2);
  179. s[0].should.be.a('string').and.equal(' ');
  180. s[1].should.be.a('string').and.equal(' ');
  181. });
  182. it('should normalize redundant paths', function () {
  183. var a = yargs([ '-s', '/tmp/../' ]).alias('s', 'save').normalize('s').argv;
  184. a.should.have.property('s', '/');
  185. a.should.have.property('save', '/');
  186. });
  187. it('should normalize redundant paths when a value is later assigned', function () {
  188. var a = yargs(['-s']).normalize('s').argv;
  189. a.should.have.property('s', true);
  190. a.s = '/path/to/new/dir/../../';
  191. a.s.should.equal('/path/to/');
  192. });
  193. it('should assign data after forward slash to the option before the slash', function () {
  194. var parse = yargs.parse(['-I/foo/bar/baz']);
  195. parse.should.have.property('_').with.length(0);
  196. parse.should.have.property('I', '/foo/bar/baz');
  197. parse = yargs.parse(['-xyz/foo/bar/baz']);
  198. parse.should.have.property('x', true);
  199. parse.should.have.property('y', true);
  200. parse.should.have.property('z', '/foo/bar/baz');
  201. parse.should.have.property('_').with.length(0);
  202. });
  203. it('should set alias value to the same value as the full option', function () {
  204. var argv = yargs([ '-f', '11', '--zoom', '55' ])
  205. .alias('z', 'zoom')
  206. .argv;
  207. argv.should.have.property('zoom', 55);
  208. argv.should.have.property('z', 55);
  209. argv.should.have.property('f', 11);
  210. });
  211. /*
  212. *it('should load options and values from a file when config is used', function () {
  213. * var argv = yargs([ '--settings', '../test/config.json', '--foo', 'bar' ])
  214. * .alias('z', 'zoom')
  215. * .config('settings')
  216. * .argv;
  217. * argv.should.have.property('herp', 'derp');
  218. * argv.should.have.property('zoom', 55);
  219. * argv.should.have.property('foo').and.deep.equal(['baz','bar']);
  220. *});
  221. */
  222. it('should allow multiple aliases to be specified', function () {
  223. var argv = yargs([ '-f', '11', '--zoom', '55' ])
  224. .alias('z', [ 'zm', 'zoom' ])
  225. .argv;
  226. argv.should.have.property('zoom', 55);
  227. argv.should.have.property('z', 55);
  228. argv.should.have.property('zm', 55);
  229. argv.should.have.property('f', 11);
  230. });
  231. it('should define option as boolean and set default to true', function () {
  232. var argv = yargs.options({
  233. sometrue: {
  234. boolean: true,
  235. default: true
  236. }
  237. }).argv;
  238. argv.should.have.property('sometrue', true);
  239. });
  240. it('should define option as boolean and set default to false', function () {
  241. var argv = yargs.options({
  242. somefalse: {
  243. boolean: true,
  244. default: false
  245. }
  246. }).argv;
  247. argv.should.have.property('somefalse', false);
  248. });
  249. it('should allow object graph traversal via dot notation', function () {
  250. var argv = yargs([
  251. '--foo.bar', '3', '--foo.baz', '4',
  252. '--foo.quux.quibble', '5', '--foo.quux.o_O',
  253. '--beep.boop'
  254. ]).argv;
  255. argv.should.have.property('foo').and.deep.equal({
  256. bar: 3,
  257. baz: 4,
  258. quux: {
  259. quibble: 5,
  260. o_O: true
  261. }
  262. });
  263. argv.should.have.property('beep').and.deep.equal({ boop: true });
  264. });
  265. it('should allow booleans and aliases to be defined with chainable api', function () {
  266. var aliased = [ '-h', 'derp' ],
  267. regular = [ '--herp', 'derp' ],
  268. opts = {
  269. herp: { alias: 'h', boolean: true }
  270. },
  271. aliasedArgv = yargs(aliased).boolean('herp').alias('h', 'herp').argv,
  272. propertyArgv = yargs(regular).boolean('herp').alias('h', 'herp').argv;
  273. aliasedArgv.should.have.property('herp', true);
  274. aliasedArgv.should.have.property('h', true);
  275. aliasedArgv.should.have.property('_').and.deep.equal(['derp']);
  276. propertyArgv.should.have.property('herp', true);
  277. propertyArgv.should.have.property('h', true);
  278. propertyArgv.should.have.property('_').and.deep.equal(['derp']);
  279. });
  280. it('should allow booleans and aliases to be defined with options hash', function () {
  281. var aliased = [ '-h', 'derp' ],
  282. regular = [ '--herp', 'derp' ],
  283. opts = {
  284. herp: { alias: 'h', boolean: true }
  285. },
  286. aliasedArgv = yargs(aliased).options(opts).argv,
  287. propertyArgv = yargs(regular).options(opts).argv;
  288. aliasedArgv.should.have.property('herp', true);
  289. aliasedArgv.should.have.property('h', true);
  290. aliasedArgv.should.have.property('_').and.deep.equal(['derp']);
  291. propertyArgv.should.have.property('herp', true);
  292. propertyArgv.should.have.property('h', true);
  293. propertyArgv.should.have.property('_').and.deep.equal(['derp']);
  294. });
  295. it('should set boolean and alias using explicit true', function () {
  296. var aliased = [ '-h', 'true' ],
  297. regular = [ '--herp', 'true' ],
  298. opts = {
  299. herp: { alias: 'h', boolean: true }
  300. },
  301. aliasedArgv = yargs(aliased).boolean('h').alias('h', 'herp').argv,
  302. propertyArgv = yargs(regular).boolean('h').alias('h', 'herp').argv;
  303. aliasedArgv.should.have.property('herp', true);
  304. aliasedArgv.should.have.property('h', true);
  305. aliasedArgv.should.have.property('_').with.length(0);
  306. });
  307. // regression, see https://github.com/substack/node-optimist/issues/71
  308. it('should set boolean and --x=true', function() {
  309. var parsed = yargs(['--boool', '--other=true']).boolean('boool').argv;
  310. parsed.should.have.property('boool', true);
  311. parsed.should.have.property('other', 'true');
  312. parsed = yargs(['--boool', '--other=false']).boolean('boool').argv;
  313. parsed.should.have.property('boool', true);
  314. parsed.should.have.property('other', 'false');
  315. });
  316. // regression, see https://github.com/chevex/yargs/issues/63
  317. it('should not add the same key to argv multiple times, when creating camel-case aliases', function() {
  318. var yargs = require('../')(['--health-check=banana', '--second-key', 'apple', '-t=blarg'])
  319. .options('h', {
  320. alias: 'health-check',
  321. description: 'health check',
  322. default: 'apple'
  323. })
  324. .options('second-key', {
  325. alias: 's',
  326. description: 'second key',
  327. default: 'banana'
  328. })
  329. .options('third-key', {
  330. alias: 't',
  331. description: 'third key',
  332. default: 'third'
  333. })
  334. // before this fix, yargs failed parsing
  335. // one but not all forms of an arg.
  336. yargs.argv.secondKey.should.eql('apple');
  337. yargs.argv.s.should.eql('apple');
  338. yargs.argv['second-key'].should.eql('apple');
  339. yargs.argv.healthCheck.should.eql('banana');
  340. yargs.argv.h.should.eql('banana');
  341. yargs.argv['health-check'].should.eql('banana');
  342. yargs.argv.thirdKey.should.eql('blarg');
  343. yargs.argv.t.should.eql('blarg');
  344. yargs.argv['third-key'].should.eql('blarg');
  345. });
  346. });