UNPKG

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