Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. const _ = require( 'lodash' );
  2.  
  3. const optionDefine = {
  4. help: {
  5. type: 'boolean',
  6. alias: 'h',
  7. default: false,
  8. description: 'display this help'
  9. },
  10. list: {
  11. type: 'boolean',
  12. alias: 'l',
  13. default: false,
  14. description: 'display tasks'
  15. },
  16. silent: {
  17. type: 'boolean',
  18. default: false,
  19. description: 'log errors only'
  20. },
  21. debug: {
  22. type: 'boolean',
  23. default: false,
  24. description: ''
  25. },
  26. version: {
  27. type: 'boolean',
  28. alias: 'v',
  29. default: false,
  30. description: 'display version'
  31. }
  32. };
  33.  
  34. optionDefine[Symbol.iterator] = function* () {
  35. for ( let key in this ) {
  36. yield [ key, this[key] ];
  37. }
  38. };
  39.  
  40. function showHelp(aOptionDefine) {
  41. const maxLength = _
  42. .chain( _.keys( optionDefine ) )
  43. .map( name => name.length )
  44. .max()
  45. .value();
  46.  
  47. console.log( 'Options:' );
  48. for ( let [name, opts] of aOptionDefine ) {
  49. process.stdout.write( ' ' );
  50.  
  51. if ( opts.alias === undefined ) {
  52. process.stdout.write( ' ' );
  53. } else {
  54. process.stdout.write( `-${opts.alias},` );
  55. }
  56.  
  57. process.stdout.write( ` --${name}` );
  58. process.stdout.write( _.repeat( ' ', ( maxLength - name.length ) + 2 ) );
  59. process.stdout.write( opts.description );
  60. process.stdout.write( '\n' );
  61. }
  62. }
  63.  
  64.  
  65. showHelp( optionDefine );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement