cli.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. 'use strict';
  10. var grunt = require('../grunt');
  11. // Nodejs libs.
  12. var path = require('path');
  13. // External libs.
  14. var nopt = require('nopt');
  15. // This is only executed when run via command line.
  16. var cli = module.exports = function(options, done) {
  17. // CLI-parsed options override any passed-in "default" options.
  18. if (options) {
  19. // For each default option...
  20. Object.keys(options).forEach(function(key) {
  21. if (!(key in cli.options)) {
  22. // If this option doesn't exist in the parsed cli.options, add it in.
  23. cli.options[key] = options[key];
  24. } else if (cli.optlist[key].type === Array) {
  25. // If this option's type is Array, append it to any existing array
  26. // (or create a new array).
  27. [].push.apply(cli.options[key], options[key]);
  28. }
  29. });
  30. }
  31. // Run tasks.
  32. grunt.tasks(cli.tasks, cli.options, done);
  33. };
  34. // Default options.
  35. var optlist = cli.optlist = {
  36. help: {
  37. short: 'h',
  38. info: 'Display this help text.',
  39. type: Boolean
  40. },
  41. base: {
  42. info: 'Specify an alternate base path. By default, all file paths are relative to the Gruntfile. (grunt.file.setBase) *',
  43. type: path
  44. },
  45. color: {
  46. info: 'Disable colored output.',
  47. type: Boolean,
  48. negate: true
  49. },
  50. gruntfile: {
  51. info: 'Specify an alternate Gruntfile. By default, grunt looks in the current or parent directories for the nearest Gruntfile.js or Gruntfile.coffee file.',
  52. type: path
  53. },
  54. debug: {
  55. short: 'd',
  56. info: 'Enable debugging mode for tasks that support it.',
  57. type: [Number, Boolean]
  58. },
  59. stack: {
  60. info: 'Print a stack trace when exiting with a warning or fatal error.',
  61. type: Boolean
  62. },
  63. force: {
  64. short: 'f',
  65. info: 'A way to force your way past warnings. Want a suggestion? Don\'t use this option, fix your code.',
  66. type: Boolean
  67. },
  68. tasks: {
  69. info: 'Additional directory paths to scan for task and "extra" files. (grunt.loadTasks) *',
  70. type: Array
  71. },
  72. npm: {
  73. info: 'Npm-installed grunt plugins to scan for task and "extra" files. (grunt.loadNpmTasks) *',
  74. type: Array
  75. },
  76. write: {
  77. info: 'Disable writing files (dry run).',
  78. type: Boolean,
  79. negate: true
  80. },
  81. verbose: {
  82. short: 'v',
  83. info: 'Verbose mode. A lot more information output.',
  84. type: Boolean
  85. },
  86. version: {
  87. short: 'V',
  88. info: 'Print the grunt version. Combine with --verbose for more info.',
  89. type: Boolean
  90. },
  91. // Even though shell auto-completion is now handled by grunt-cli, leave this
  92. // option here for display in the --help screen.
  93. completion: {
  94. info: 'Output shell auto-completion rules. See the grunt-cli documentation for more information.',
  95. type: String
  96. },
  97. };
  98. // Parse `optlist` into a form that nopt can handle.
  99. var aliases = {};
  100. var known = {};
  101. Object.keys(optlist).forEach(function(key) {
  102. var short = optlist[key].short;
  103. if (short) {
  104. aliases[short] = '--' + key;
  105. }
  106. known[key] = optlist[key].type;
  107. });
  108. var parsed = nopt(known, aliases, process.argv, 2);
  109. cli.tasks = parsed.argv.remain;
  110. cli.options = parsed;
  111. delete parsed.argv;
  112. // Initialize any Array options that weren't initialized.
  113. Object.keys(optlist).forEach(function(key) {
  114. if (optlist[key].type === Array && !(key in cli.options)) {
  115. cli.options[key] = [];
  116. }
  117. });