Version 1
[yaffs-website] / node_modules / grunt / lib / grunt / cli.js
1 'use strict';
2
3 var grunt = require('../grunt');
4
5 // External libs.
6 var nopt = require('nopt');
7 var gruntOptions = require('grunt-known-options');
8
9 // This is only executed when run via command line.
10 var 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.
31 var optlist = cli.optlist = gruntOptions;
32
33 // Parse `optlist` into a form that nopt can handle.
34 var aliases = {};
35 var known = {};
36
37 Object.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
45 var parsed = nopt(known, aliases, process.argv, 2);
46 cli.tasks = parsed.argv.remain;
47 cli.options = parsed;
48 delete parsed.argv;
49
50 // Initialize any Array options that weren't initialized.
51 Object.keys(optlist).forEach(function(key) {
52   if (optlist[key].type === Array && !(key in cli.options)) {
53     cli.options[key] = [];
54   }
55 });