Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Console\Parser Class Reference

Public Member Functions

 parse ($command)
 
 transferSwitchToInput ($name, &$value)
 
 getInputs ()
 
 listInputs (&$a, &$b=null, &$c=null, &$d=null, &$e=null, &$f=null, &$g=null, &$h=null, &$i=null, &$j=null, &$k=null, &$l=null, &$m=null, &$n=null, &$o=null, &$p=null, &$q=null, &$r=null, &$s=null, &$t=null, &$u=null, &$v=null, &$w=null, &$x=null, &$y=null, &$z=null)
 
 getSwitches ()
 
 parseSpecialValue ($value, Array $keywords=[])
 
 setLongOnly ($longonly=false)
 
 getLongOnly ()
 

Protected Member Functions

 addInput (Array $input)
 
 addBoolSwitch (Array $switch)
 
 addValuedSwitch (Array $switch)
 
 addSwitch ($name, $value, $escape=null)
 

Protected Attributes

 $_longonly = false
 
 $_parsed = null
 

Detailed Description

Class .

This class parses a command line. See the parse() method to get more informations about command-line vocabulary, patterns, limitations, etc.

Definition at line 49 of file Parser.php.

Member Function Documentation

Hoa\Console\Parser::addBoolSwitch ( Array  $switch)
protected

Add a boolean switch.

Parameters
array$switchSwitch.
Returns
void

Definition at line 271 of file Parser.php.

272  {
273  $this->addSwitch($switch['b'], true);
274 
275  return;
276  }
addSwitch($name, $value, $escape=null)
Definition: Parser.php:299

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Console\Parser::addInput ( Array  $input)
protected

Add an input.

Parameters
array$inputIntput.
Returns
void

Definition at line 250 of file Parser.php.

251  {
252  $handle = $input['i'];
253 
254  if (!empty($input[6])) {
255  $handle = str_replace('\\' . $input[6], $input[6], $handle);
256  } else {
257  $handle = str_replace('\\ ', ' ', $handle);
258  }
259 
260  $this->_parsed['input'][] = $handle;
261 
262  return;
263  }

Here is the caller graph for this function:

Hoa\Console\Parser::addSwitch (   $name,
  $value,
  $escape = null 
)
protected

Add a switch.

Parameters
string$nameSwitch name.
string$valueSwitch value.
string$escapeCharacter to escape.
Returns
void

Definition at line 299 of file Parser.php.

300  {
301  if (substr($name, 0, 2) == '--') {
302  return $this->addSwitch(substr($name, 2), $value, $escape);
303  }
304 
305  if (substr($name, 0, 1) == '-') {
306  if (true === $this->getLongOnly()) {
307  return $this->addSwitch('-' . $name, $value, $escape);
308  }
309 
310  foreach (str_split(substr($name, 1)) as $foo => $switch) {
311  $this->addSwitch($switch, $value, $escape);
312  }
313 
314  return;
315  }
316 
317  if (null !== $escape) {
318  $escape = '' == $escape ? ' ' : $escape;
319 
320  if (is_string($value)) {
321  $value = str_replace('\\' . $escape, $escape, $value);
322  }
323  } elseif (is_string($value)) {
324  $value = str_replace('\\ ', ' ', $value);
325  }
326 
327  if (isset($this->_parsed['switch'][$name])) {
328  if (is_bool($this->_parsed['switch'][$name])) {
329  $value = !$this->_parsed['switch'][$name];
330  } else {
331  $value = [$this->_parsed['switch'][$name], $value];
332  }
333  }
334 
335  if (empty($name)) {
336  return $this->addInput([6 => null, 'i' => $value]);
337  }
338 
339  $this->_parsed['switch'][$name] = $value;
340 
341  return;
342  }
addSwitch($name, $value, $escape=null)
Definition: Parser.php:299
addInput(Array $input)
Definition: Parser.php:250

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Console\Parser::addValuedSwitch ( Array  $switch)
protected

Add a valued switch.

Parameters
array$switchSwitch.
Returns
void

Definition at line 284 of file Parser.php.

285  {
286  $this->addSwitch($switch['b'], $switch['s'], $switch[4]);
287 
288  return;
289  }
addSwitch($name, $value, $escape=null)
Definition: Parser.php:299

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Console\Parser::getInputs ( )

Get all inputs.

Returns
array

Definition at line 369 of file Parser.php.

370  {
371  return $this->_parsed['input'];
372  }

Here is the caller graph for this function:

Hoa\Console\Parser::getLongOnly ( )

Get the long-only value.

Returns
bool

Definition at line 508 of file Parser.php.

509  {
510  return $this->_longonly;
511  }

Here is the caller graph for this function:

Hoa\Console\Parser::getSwitches ( )

Get all switches.

Returns
array

Definition at line 429 of file Parser.php.

430  {
431  return $this->_parsed['switch'];
432  }

Here is the caller graph for this function:

Hoa\Console\Parser::listInputs ( $a,
$b = null,
$c = null,
$d = null,
$e = null,
$f = null,
$g = null,
$h = null,
$i = null,
$j = null,
$k = null,
$l = null,
$m = null,
$n = null,
$o = null,
$p = null,
$q = null,
$r = null,
$s = null,
$t = null,
$u = null,
$v = null,
$w = null,
$x = null,
$y = null,
$z = null 
)

Distribute inputs in variable (like the list() function, but without error).

Parameters
string$aFirst input.
string$bSecond input.
string$cThird input.
...... ...
string$z26th input.
Returns
void

Definition at line 385 of file Parser.php.

412  {
413  $inputs = $this->getInputs();
414  $i = 'a';
415  $ii = -1;
416 
417  while (isset($inputs[++$ii]) && $i <= 'z') {
418  ${$i++} = $inputs[$ii];
419  }
420 
421  return;
422  }

Here is the call graph for this function:

Hoa\Console\Parser::parse (   $command)

Parse a command. Some explanations :

  1. Command : $ cmd is the command : cmd ; $ "cmd sub" is the command : cmd sub ; $ cmd\ sub is the command : cmd sub.
  2. Short option : $ … -s is a short option ; $ … -abc is equivalent to -a -b -c if and only if $longonly is set to false, else (set to true) -abc is equivalent to –abc.
  3. Long option : $ … –long is a long option ; $ … –lo-ng is a long option. $ etc.
  4. Boolean switch or flag : $ … -s is a boolean switch, -s is set to true ; $ … –long is a boolean switch, –long is set to true ; $ … -s -s and –long –long are boolean switches, -s and –long are set to false ; $ … -aa are boolean switches, -a is set to false, if and only if the $longonly is set to false, else –aa is set to true.
  5. Valued switch : x should be s, -long, abc etc. All the following examples are valued switches, where -x is set to the specified value. $ … -x=value : value ; $ … -x=va\ lue : va lue ; $ … -x="va lue" : va lue ; $ … -x="va l\"ue" : va l"ue ; $ … -x value : value ; $ … -x va\ lue : va lue ; $ … -x "value" : value ; $ … -x "va lue" : va lue ; $ … -x va\ l"ue : va l"ue ; $ … -x 'va "l"ue' : va "l"ue ; $ etc. (we did not written all cases, but the philosophy is here). Two type of quotes are supported : double quotes ("), and simple quotes ('). We got very particulary cases : $ … -x=-value : -value ; $ … -x "-value" : -value ; $ … -x -value : -value ; $ … -x -value : two switches, -x and -value are set to true ; $ … -x=-7 : -7, a negative number. And if we have more than one valued switch, the value is overwritted : $ … -x a -x b : b. Maybe, it should produce an array, like the special valued switch (see the point 6. please).
  6. Special valued switch : Some valued switch can have a list, or an interval in value ; e.g. -x=a,b,c, or -x=1:7 etc. This class gives the value as it is, i.e. no manipulation or treatment is made. $ … -x=a,b,c : a,b,c (and no array('a', 'b', 'c')) ; $ etc. These manipulations should be made by the user no ? The self::parseSpecialValue() is written for that.
  7. Input : The regular expression sets a value as much as possible to each switch (option). If a switch does not take a value (see the ::NO_ARGUMENT constant), the value will be transfered to the input stack. But this action is not made in this class, only in the class, because this class does not have the options profile. We got the transferSwitchToInput() method, that is called in the GetOption class. So : $ cmd -x input the input is the -x value ; $ cmd -x – input the input is a real input, not a value ; $ cmd -x value input -x is set to value, and the input is a real input ; $ cmd -x value – input equivalent to -x value input ; $ … -a b i -c d ii -a is set to b, -c to d, and we got two inputs : i and ii.

Warning : if the command was reconstitued from the $_SERVER variable, all these cases are not sure to work, because the command was already interpreted/parsed by an other parser (Shell, DOS etc.), and maybe they remove some character, or some particular case. But if we give the command manually — i.e. without any reconstitution —, all these cases will work :).

Parameters
string$commandCommand to parse.
Returns
void

Here we go …

#
(?:
    (?<b>--?[^=\s]+)
    (?:
        (?:(=)|(\s))
        (?<!\\\)(?:("|\')|)
        (?<s>(?(3)[^-]|).*?)
        (?(4)
            (?<!\\\)\4
            |
            (?(2)
                (?<!\\\)\s
                |
                (?:(?:(?<!\\\)\s)|$)
            )
        )
    )?
)
|
(?:
    (?<!\\\)(?:("|\')|)
    (?<i>.*?)
    (?(6)
        (?<!\\\)\6
        |
        (?:(?:(?<!\\\)\s)|$)
    )
)
#xSsm

Nice isn't it :-D ?

Note : this regular expression likes to capture empty array (near ), why?

Definition at line 159 of file Parser.php.

160  {
161  unset($this->_parsed);
162  $this->_parsed = [
163  'input' => [],
164  'switch' => []
165  ];
166 
206  $regex = '#(?:(?<b>--?[^=\s]+)(?:(?:(=)|(\s))(?<!\\\)(?:("|\')|)(?<s>(?(3)[^-]|).*?)(?(4)(?<!\\\)\4|(?(2)(?<!\\\)\s|(?:(?:(?<!\\\)\s)|$))))?)|(?:(?<!\\\)(?:("|\')|)(?<i>.*?)(?(6)(?<!\\\)\6|(?:(?:(?<!\\\)\s)|$)))#Ssm';
207 
208  preg_match_all($regex, $command, $matches, PREG_SET_ORDER);
209 
210  for ($i = 0, $max = count($matches); $i < $max; ++$i) {
211  $match = $matches[$i];
212 
213  if (isset($match['i']) &&
214  ('0' === $match['i'] || !empty($match['i']))) {
215  $this->addInput($match);
216  } elseif (!isset($match['i']) && !isset($match['s'])) {
217  if (isset($matches[$i + 1])) {
218  $nextMatch = $matches[$i + 1];
219 
220  if (!empty($nextMatch['i']) &&
221  '=' === $nextMatch['i'][0]) {
222  ++$i;
223  $match[2] = '=';
224  $match[3] =
225  $match[4] = null;
226  $match['s'] =
227  $match[5] = substr($nextMatch[7], 1);
228 
229  $this->addValuedSwitch($match);
230 
231  continue;
232  }
233  }
234 
235  $this->addBoolSwitch($match);
236  } elseif (!isset($match['i']) && isset($match['s'])) {
237  $this->addValuedSwitch($match);
238  }
239  }
240 
241  return;
242  }
addValuedSwitch(Array $switch)
Definition: Parser.php:284
addBoolSwitch(Array $switch)
Definition: Parser.php:271
addInput(Array $input)
Definition: Parser.php:250

Here is the call graph for this function:

Hoa\Console\Parser::parseSpecialValue (   $value,
Array  $keywords = [] 
)

Parse a special value, i.e. with comma and intervals.

Parameters
string$valueThe value to parse.
array$keywordsValue of keywords.
Returns
array
Exceptions

Definition at line 444 of file Parser.php.

445  {
446  $out = [];
447 
448  foreach (explode(',', $value) as $key => $subvalue) {
449  $subvalue = str_replace(
450  array_keys($keywords),
451  array_values($keywords),
452  $subvalue
453  );
454 
455  if (0 !== preg_match('#^(-?[0-9]+):(-?[0-9]+)$#', $subvalue, $matches)) {
456  if (0 > $matches[1] && 0 > $matches[2]) {
457  throw new Exception(
458  'Cannot give two negative numbers, given %s.',
459  0,
460  $subvalue
461  );
462  }
463 
464  array_shift($matches);
465  $max = max($matches);
466  $min = min($matches);
467 
468  if (0 > $max || 0 > $min) {
469  if (0 > $max - $min) {
470  throw new Exception(
471  'The difference between operands must be ' .
472  'positive.',
473  1
474  );
475  }
476 
477  $min = $max + $min;
478  }
479 
480  $out = array_merge(range($min, $max), $out);
481  } else {
482  $out[] = $subvalue;
483  }
484  }
485 
486  return $out;
487  }
Hoa\Console\Parser::setLongOnly (   $longonly = false)

Set the long-only parameter.

Parameters
bool$longonlyThe long-only value.
Returns
bool

Definition at line 495 of file Parser.php.

496  {
497  $old = $this->_longonly;
498  $this->_longonly = $longonly;
499 
500  return $old;
501  }
Hoa\Console\Parser::transferSwitchToInput (   $name,
$value 
)

Transfer a switch value in the input stack.

Parameters
string$nameThe switch name.
string$valueThe switch value.
Returns
void

Definition at line 351 of file Parser.php.

352  {
353  if (!isset($this->_parsed['switch'][$name])) {
354  return;
355  }
356 
357  $this->_parsed['input'][] = $this->_parsed['switch'][$name];
358  $value = true;
359  unset($this->_parsed['switch'][$name]);
360 
361  return;
362  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Console\Parser::$_longonly = false
protected

Definition at line 57 of file Parser.php.

Hoa\Console\Parser::$_parsed = null
protected

Definition at line 64 of file Parser.php.


The documentation for this class was generated from the following file: