lib/argv.js
function argv(args) {
  var out = {directives: {}}
    , i = 0;

  /* istanbul ignore next: never use process.argv in tests */
  args = args || process.argv.slice(2);
  if(!args.length) return out;

  // read conf from stdin
  if(args[i] === STDIN) {
    out.stdin = true;
    i++;
  // conf should be first arg
  }else if(!PTN.test(args[i])) {
    out.file = args[i];
    i++;
  }

  function add(key, value) {
    if(!key || value === null) return;
    if(!out.directives[key]) {
      out.directives[key] = value;
    // handle repeated directives, create an array
    }else if(out.directives[key]
      && !Array.isArray(out.directives[key])){
      out.directives[key] = [out.directives[key]];
      out.directives[key].push(value)
    // append to existing array value
    }else{
      out.directives[key].push(value)
    }
  }

  function gobble(arg, key, value) {
    if(arg === undefined) return add(key, value);
    value = value || '';
    if(PTN.test(arg)) {
      key = arg.replace(PTN, '');
      // gobble up value arguments
      while((arg = args[i++]) !== undefined && !PTN.test(arg)) {
        if(value) value += ' ';
        value += arg;
      }
      if(PTN.test(arg)) i--;
      add(key, value);
      value = null;
    }else{
      if(value) value += ' ';
      value += arg;
    }
    gobble(args[i++], key, value);
  }
  gobble(args[i]);
  return out;
}
function argv(args) {
  var out = {directives: {}}
    , i = 0;

  /* istanbul ignore next: never use process.argv in tests */
  args = args || process.argv.slice(2);
  if(!args.length) return out;

  // read conf from stdin
  if(args[i] === STDIN) {
    out.stdin = true;
    i++;
  // conf should be first arg
  }else if(!PTN.test(args[i])) {
    out.file = args[i];
    i++;
  }

  function add(key, value) {
    if(!key || value === null) return;
    if(!out.directives[key]) {
      out.directives[key] = value;
    // handle repeated directives, create an array
    }else if(out.directives[key]
      && !Array.isArray(out.directives[key])){
      out.directives[key] = [out.directives[key]];
      out.directives[key].push(value)
    // append to existing array value
    }else{
      out.directives[key].push(value)
    }
  }

  function gobble(arg, key, value) {
    if(arg === undefined) return add(key, value);
    value = value || '';
    if(PTN.test(arg)) {
      key = arg.replace(PTN, '');
      // gobble up value arguments
      while((arg = args[i++]) !== undefined && !PTN.test(arg)) {
        if(value) value += ' ';
        value += arg;
      }
      if(PTN.test(arg)) i--;
      add(key, value);
      value = null;
    }else{
      if(value) value += ' ';
      value += arg;
    }
    gobble(args[i++], key, value);
  }
  gobble(args[i]);
  return out;
}
function argv ( args ) { var out = { directives : { } } , i = 0 ; /* istanbul ignore next: never use process.argv in tests */ args = args || process . argv . slice ( 2 ) ; if ( ! args . length ) return out ; // read conf from stdin if ( args [ i ] === STDIN ) { out . stdin = true ; i ++ ; // conf should be first arg } else if ( ! PTN . test ( args [ i ] ) ) { out . file = args [ i ] ; i ++ ; } function add ( key , value ) { if ( ! key || value === null ) return ; if ( ! out . directives [ key ] ) { out . directives [ key ] = value ; // handle repeated directives, create an array } else if ( out . directives [ key ] && ! Array . isArray ( out . directives [ key ] ) ) { out . directives [ key ] = [ out . directives [ key ] ] ; out . directives [ key ] . push ( value ) // append to existing array value } else { out . directives [ key ] . push ( value ) } } function gobble ( arg , key , value ) { if ( arg === undefined ) return add ( key , value ) ; value = value || '' ; if ( PTN . test ( arg ) ) { key = arg . replace ( PTN , '' ) ; // gobble up value arguments while ( ( arg = args [ i ++ ] ) !== undefined && ! PTN . test ( arg ) ) { if ( value ) value += ' ' ; value += arg ; } if ( PTN . test ( arg ) ) i -- ; add ( key , value ) ; value = null ; } else { if ( value ) value += ' ' ; value += arg ; } gobble ( args [ i ++ ] , key , value ) ; } gobble ( args [ i ] ) ; return out ; }
Parse arguments redis-server style.
Parse arguments redis - server style .