erikpalla
3/17/2017 - 9:13 PM

process.argv

process.argv

// fetch command line arguments
// The function loops through the process.argv array. When it encounters a value preceded with one or 
// more dashes, it creates a new named value in the arg object which is set to true. When it 
// encounters a value without dashes, it sets the previous named value (if available) to that string.

// When we run gulp task1 --a 123 --b "my string" --c, the arg object is set to:

// {
//   "a": "123",
//   "b": "my string",
//   "c": true
// }


const arg = (argList => {

  let arg = {}, a, opt, thisOpt, curOpt;
  for (a = 0; a < argList.length; a++) {

    thisOpt = argList[a].trim();
    opt = thisOpt.replace(/^\-+/, '');

    if (opt === thisOpt) {

      // argument value
      if (curOpt) arg[curOpt] = opt;
      curOpt = null;

    }
    else {

      // argument name
      curOpt = opt;
      arg[curOpt] = true;

    }

  }

  return arg;

})(process.argv);