liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var _ = require('lodash').runInContext();
 
module.exports = function(options, singleDash) {
  return _.reduce(options, function(memo, val, key) {
    val = _.isArray(val) ? val : [val];
 
    // Loop over each value for this key so that multiple values
    // can be assigned to an option
    _.each(val, function(v) {
      // Build the flag as --key
      var flag = key.length === 1 ? key : _.kebabCase(key);
 
      // Set = style options, e.g. --fruit=banana
      if (key.slice(-1) === '=') {
        flag = `${flag}=${v}`;
      }
 
      var prefix = (key.length === 1 || singleDash) ? '-' : '--';
 
      if (v === false) {
        prefix += 'no-';
      }
 
      flag = prefix + flag;
 
      // Add the option to the list
      memo.push(flag);
 
      // Specifically allow "true" to mean "this flag has no arg with it"
      if (typeof v !== 'boolean' && flag.indexOf('=') === -1) {
        // Add the value to the list as well
        memo.push(v);
      }
    });
 
    return memo;
  }, []);
};