parse_camelCase.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var should = require('chai').should(),
  2. yargs = require('../');
  3. describe('parse', function () {
  4. function runTests (yargs, strict) {
  5. if (!strict) {
  6. // Skip this test in strict mode because this option is not specified
  7. it('should provide options with dashes as camelCase properties', function () {
  8. var result = yargs()
  9. .parse([ '--some-option' ]);
  10. result.should.have.property('some-option').that.is.a('boolean').and.is.true;
  11. result.should.have.property('someOption' ).that.is.a('boolean').and.is.true;
  12. });
  13. }
  14. it('should provide count options with dashes as camelCase properties', function () {
  15. var result = yargs()
  16. .option('some-option', {
  17. describe : 'some option',
  18. type : 'count'
  19. })
  20. .parse([ '--some-option', '--some-option', '--some-option' ]);
  21. result.should.have.property('some-option', 3);
  22. result.should.have.property('someOption' , 3);
  23. });
  24. it('should provide options with dashes and aliases as camelCase properties', function () {
  25. var result = yargs()
  26. .option('some-option', {
  27. alias : 'o',
  28. describe : 'some option'
  29. })
  30. .parse([ '--some-option' ]);
  31. result.should.have.property('some-option').that.is.a('boolean').and.is.true;
  32. result.should.have.property('someOption' ).that.is.a('boolean').and.is.true;
  33. });
  34. it('should provide defaults of options with dashes as camelCase properties', function() {
  35. var result = yargs()
  36. .option('some-option', {
  37. describe : 'some option',
  38. default : 'asdf'
  39. })
  40. .parse([ ]);
  41. result.should.have.property('some-option', 'asdf');
  42. result.should.have.property('someOption' , 'asdf');
  43. });
  44. it('should provide aliases of options with dashes as camelCase properties', function() {
  45. var result = yargs()
  46. .option('some-option', {
  47. alias : 'o',
  48. describe : 'some option',
  49. default : 'asdf'
  50. })
  51. .parse([ ]);
  52. result.should.have.property('o', 'asdf');
  53. result.should.have.property('some-option', 'asdf');
  54. result.should.have.property('someOption' , 'asdf');
  55. });
  56. it('should provide aliases of options with dashes as camelCase properties', function() {
  57. var result = yargs()
  58. .option('o', {
  59. alias : 'some-option',
  60. describe : 'some option',
  61. default : 'asdf'
  62. })
  63. .parse([ ]);
  64. result.should.have.property('o', 'asdf');
  65. result.should.have.property('some-option', 'asdf');
  66. result.should.have.property('someOption' , 'asdf');
  67. });
  68. it('should provide aliases with dashes as camelCase properties', function() {
  69. var result = yargs()
  70. .option('o', {
  71. alias : 'some-option',
  72. describe : 'some option'
  73. })
  74. .parse([ '--some-option', 'val' ]);
  75. result.should.have.property('o' ).that.is.a('string').and.equals('val');
  76. result.should.have.property('some-option').that.is.a('string').and.equals('val');
  77. result.should.have.property('someOption' ).that.is.a('string').and.equals('val');
  78. });
  79. }
  80. describe('dashes and camelCase', function () {
  81. runTests(function() {
  82. return yargs();
  83. });
  84. });
  85. describe('dashes and camelCase (strict)', function () {
  86. runTests(function() {
  87. // Special handling for failure messages, because normally a
  88. // failure calls process.exit(1);
  89. return yargs().strict().fail(function(msg) {
  90. throw new Error(msg);
  91. });
  92. }, true);
  93. // See https://github.com/chevex/yargs/issues/31
  94. it('should not fail when options with defaults are missing', function () {
  95. var result = yargs()
  96. .fail(function(msg) {
  97. throw new Error(msg);
  98. })
  99. .option('some-option', {
  100. describe : 'some option',
  101. default : 80
  102. })
  103. .strict()
  104. .parse([ ]);
  105. });
  106. });
  107. });