# Code coverage report for lib/cli/index.js

## Statements: 100% (22 / 22) Branches: 100% (12 / 12) Functions: 100% (2 / 2) Lines: 100% (22 / 22)

All files » lib/cli/ » index.js

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    551

    1
    25

    25

    25
    148
    148
    6
    6
    142
    6
    6
    6
    136
    1

    135

    25

    1

    26
    25

    26

    26

     var parse = require('./parse');

    /**
     * Converts a string to command line args, in particular
     * groups together quoted values
     *
     * @param  {String} string
     * @return {Array}
     */
    function stringToArgs(string) {
      var args = [];

      var parts = string.split(' '),
          length = parts.length,
          i = 0,
          open = false,
          grouped = '',
          lead = '';

      for (; i < length; i++) {
        lead = parts[i].substring(0, 1);
        if (lead === '"' || lead === '\'') {
          open = lead;
          grouped = parts[i].substring(1);
        } else if (open && parts[i].slice(-1) === open) {
          open = false;
          grouped += ' ' + parts[i].slice(0, -1);
          args.push(grouped);
        } else if (open) {
          grouped += ' ' + parts[i];
        } else {
          args.push(parts[i]);
        }
      }

      return args;
    }

    module.exports = {
      parse: function (argv) {
        if (typeof argv === 'string') {
          argv = stringToArgs(argv);
        }

        var parsed = parse(argv);

        // if (!parsed.options) parsed.options = {};
        // parsed.options.exec = parsed.exec;
        // parsed.options.ext = parsed.ext;

        // parsed.execOptions = exec(parsed);

        return parsed;
      }
    };