UNPKG

1.52 kBJavaScriptView Raw
1'use strict';
2
3var grunt = require('../grunt');
4
5// External libs.
6var nopt = require('nopt');
7var gruntOptions = require('grunt-known-options');
8
9// This is only executed when run via command line.
10var cli = module.exports = function(options, done) {
11 // CLI-parsed options override any passed-in "default" options.
12 if (options) {
13 // For each default option...
14 Object.keys(options).forEach(function(key) {
15 if (!(key in cli.options)) {
16 // If this option doesn't exist in the parsed cli.options, add it in.
17 cli.options[key] = options[key];
18 } else if (cli.optlist[key].type === Array) {
19 // If this option's type is Array, append it to any existing array
20 // (or create a new array).
21 [].push.apply(cli.options[key], options[key]);
22 }
23 });
24 }
25
26 // Run tasks.
27 grunt.tasks(cli.tasks, cli.options, done);
28};
29
30// Default options.
31var optlist = cli.optlist = gruntOptions;
32
33// Parse `optlist` into a form that nopt can handle.
34var aliases = {};
35var known = {};
36
37Object.keys(optlist).forEach(function(key) {
38 var short = optlist[key].short;
39 if (short) {
40 aliases[short] = '--' + key;
41 }
42 known[key] = optlist[key].type;
43});
44
45var parsed = nopt(known, aliases, process.argv, 2);
46cli.tasks = parsed.argv.remain;
47cli.options = parsed;
48delete parsed.argv;
49
50// Initialize any Array options that weren't initialized.
51Object.keys(optlist).forEach(function(key) {
52 if (optlist[key].type === Array && !(key in cli.options)) {
53 cli.options[key] = [];
54 }
55});