lib/interpreter.js
function interpret(input) {
  var cli = []
    , exp = null
    , i
    , s
    , q
    , e
    , sre = /^("|').*/
    , ere = /.*("|')$/;

  assert(
    typeof input === 'string' || input instanceof String,
    'interpret invalid input: must be a string');

  input = '' + input;

  assert(
    input && !/^\s+$/.test(input),
    'interpret invalid input: empty string');

  input = input.replace(/^\s+/, '').replace(/\s+$/, '');

  cli = input.split(/\s+/);

  cli = cli.map(function(str) {
    var num = parseInt(str);
    if(!isNaN(num)) {
      return num;
    }
    return str;
  })

  // expand quoted values
  if(this.quote) {
    exp = [];
    for(i = 0;i < cli.length;i++) {
      s = cli[i];
      if(sre.test(s)) {
        // string is self-closed no whitespace: ie: "dbsize" || 'dbsize'
        if(ere.test(s)) {
          s = s.substr(1, s.length - 2);
          exp.push(s);
        // find terminating string
        }else{
          assert(i < cli.length -1, 'invalid argument(s)');
          e = s.replace(sre, "$1");
          q = s;
          while(++i < cli.length) {
            s = cli[i];
            q += ' ' + s;
            if(s.substr(-1) === e) {
              exp.push(q.substr(1, q.length - 2));
              break;
            }
            // no terminating match found
            assert(false, 'invalid argument(s)');
          }
        }
      }else{
        exp.push(s);
      }
    }
    cli = exp;
  }

  //console.error('interpret got input %s', input);
  //console.error('interpret returning %j', cli);

  return cli;
}
function interpret(input) {
  var cli = []
    , exp = null
    , i
    , s
    , q
    , e
    , sre = /^("|').*/
    , ere = /.*("|')$/;

  assert(
    typeof input === 'string' || input instanceof String,
    'interpret invalid input: must be a string');

  input = '' + input;

  assert(
    input && !/^\s+$/.test(input),
    'interpret invalid input: empty string');

  input = input.replace(/^\s+/, '').replace(/\s+$/, '');

  cli = input.split(/\s+/);

  cli = cli.map(function(str) {
    var num = parseInt(str);
    if(!isNaN(num)) {
      return num;
    }
    return str;
  })

  // expand quoted values
  if(this.quote) {
    exp = [];
    for(i = 0;i < cli.length;i++) {
      s = cli[i];
      if(sre.test(s)) {
        // string is self-closed no whitespace: ie: "dbsize" || 'dbsize'
        if(ere.test(s)) {
          s = s.substr(1, s.length - 2);
          exp.push(s);
        // find terminating string
        }else{
          assert(i < cli.length -1, 'invalid argument(s)');
          e = s.replace(sre, "$1");
          q = s;
          while(++i < cli.length) {
            s = cli[i];
            q += ' ' + s;
            if(s.substr(-1) === e) {
              exp.push(q.substr(1, q.length - 2));
              break;
            }
            // no terminating match found
            assert(false, 'invalid argument(s)');
          }
        }
      }else{
        exp.push(s);
      }
    }
    cli = exp;
  }

  //console.error('interpret got input %s', input);
  //console.error('interpret returning %j', cli);

  return cli;
}
function interpret ( input ) { var cli = [ ] , exp = null , i , s , q , e , sre = / ^("|').* / , ere = / .*("|')$ / ; assert ( typeof input === 'string' || input instanceof String , 'interpret invalid input: must be a string' ) ; input = '' + input ; assert ( input && ! / ^\s+$ / . test ( input ) , 'interpret invalid input: empty string' ) ; input = input . replace ( / ^\s+ / , '' ) . replace ( / \s+$ / , '' ) ; cli = input . split ( / \s+ / ) ; cli = cli . map ( function ( str ) { var num = parseInt ( str ) ; if ( ! isNaN ( num ) ) { return num ; } return str ; } ) // expand quoted values if ( this . quote ) { exp = [ ] ; for ( i = 0 ; i < cli . length ; i ++ ) { s = cli [ i ] ; if ( sre . test ( s ) ) { // string is self-closed no whitespace: ie: "dbsize" || 'dbsize' if ( ere . test ( s ) ) { s = s . substr ( 1 , s . length - 2 ) ; exp . push ( s ) ; // find terminating string } else { assert ( i < cli . length - 1 , 'invalid argument(s)' ) ; e = s . replace ( sre , "$1" ) ; q = s ; while ( ++ i < cli . length ) { s = cli [ i ] ; q += ' ' + s ; if ( s . substr ( - 1 ) === e ) { exp . push ( q . substr ( 1 , q . length - 2 ) ) ; break ; } // no terminating match found assert ( false , 'invalid argument(s)' ) ; } } } else { exp . push ( s ) ; } } cli = exp ; } //console.error('interpret got input %s', input); //console.error('interpret returning %j', cli); return cli ; }
Interpret a string value into a command
and arguments.

@param input The input string.

@return An array containing a command and arguments.
Interpret a string value into a command and arguments .