parse_defaults.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var should = require('chai').should(),
  2. yargs = require('../');
  3. describe('parse', function () {
  4. describe('defaults', function () {
  5. function checkNoArgs(argv, hasAlias) {
  6. it('should set defaults if no args', function() {
  7. var result = argv.parse([ ]);
  8. result.should.have.property('flag', true);
  9. if (hasAlias) {
  10. result.should.have.property('f', true);
  11. }
  12. });
  13. }
  14. function checkExtraArg(argv, hasAlias) {
  15. it('should set defaults if one extra arg', function() {
  16. var result = argv.parse([ 'extra' ]);
  17. result.should.have.property('flag', true);
  18. result.should.have.property('_').and.deep.equal(['extra']);
  19. if (hasAlias) {
  20. result.should.have.property('f', true);
  21. }
  22. });
  23. }
  24. function checkStringArg(argv, hasAlias) {
  25. it('should set defaults even if arg looks like a string', function() {
  26. var result = argv.parse([ '--flag', 'extra' ]);
  27. result.should.have.property('flag', true);
  28. result.should.have.property('_').and.deep.equal(['extra']);
  29. if (hasAlias) {
  30. result.should.have.property('f', true);
  31. }
  32. });
  33. }
  34. describe('for options with aliases', function () {
  35. var args = yargs().options({
  36. flag : {
  37. alias : 'f',
  38. default : true
  39. }
  40. });
  41. checkNoArgs(args, true);
  42. checkExtraArg(args, true);
  43. // This test case should fail, because we didn't specify that the
  44. // option is a boolean
  45. // checkStringArg(args, true);
  46. });
  47. describe('for typed options without aliases', function () {
  48. var args = yargs().options({
  49. flag : {
  50. type : 'boolean',
  51. default : true
  52. }
  53. });
  54. checkNoArgs(args);
  55. checkExtraArg(args);
  56. checkStringArg(args);
  57. });
  58. describe('for typed options with aliases', function () {
  59. var args = yargs().options({
  60. flag : {
  61. alias : 'f',
  62. type : 'boolean',
  63. default : true
  64. }
  65. });
  66. checkNoArgs(args, true);
  67. checkExtraArg(args, true);
  68. checkStringArg(args, true);
  69. });
  70. });
  71. });