function handle($doc, $argv = null) { try { if (!$argv && isset($_SERVER['argv'])) { $argv = array_slice($_SERVER['argv'], 1); } ExitException::$usage = printable_usage($doc); $options = parse_defaults($doc); $formalUse = formal_usage(ExitException::$usage); $pattern = parse_pattern($formalUse, $options); $argv = parse_argv(new TokenStream($argv, 'ExitException'), $options, $this->optionsFirst); foreach ($pattern->flat('AnyOptions') as $ao) { $docOptions = parse_defaults($doc); $ao->children = array_diff((array) $docOptions, $pattern->flat('Option')); } extras($this->help, $this->version, $argv, $doc); list($matched, $left, $collected) = $pattern->fix()->match($argv); if ($matched && !$left) { $return = array(); foreach (array_merge($pattern->flat(), $collected) as $a) { $name = $a->name; if ($name) { $return[$name] = $a->value; } } return new Response($return); } throw new ExitException(); } catch (ExitException $ex) { $this->handleExit($ex); return new Response(null, $ex->status, $ex->getMessage()); } }
function handle($doc, $argv = null) { try { if ($argv === null && isset($_SERVER['argv'])) { $argv = array_slice($_SERVER['argv'], 1); } $usageSections = parse_section('usage:', $doc); if (count($usageSections) == 0) { throw new LanguageError('"usage:" (case-insensitive) not found.'); } elseif (count($usageSections) > 1) { throw new LanguageError('More than one "usage:" (case-insensitive).'); } $usage = $usageSections[0]; // temp fix until python port provides solution ExitException::$usage = $usage; $options = parse_defaults($doc); $formalUse = formal_usage($usage); $pattern = parse_pattern($formalUse, $options); $argv = parse_argv(new Tokens($argv), $options, $this->optionsFirst); $patternOptions = $pattern->flat('Option'); foreach ($pattern->flat('OptionsShortcut') as $optionsShortcut) { $docOptions = parse_defaults($doc); $optionsShortcut->children = array_diff((array) $docOptions, $patternOptions); } extras($this->help, $this->version, $argv, $doc); list($matched, $left, $collected) = $pattern->fix()->match($argv); if ($matched && !$left) { $return = array(); foreach (array_merge($pattern->flat(), $collected) as $a) { $name = $a->name; if ($name) { $return[$name] = $a->value; } } return new Response($return); } throw new ExitException(); } catch (ExitException $ex) { $this->handleExit($ex); return new Response(null, $ex->status, $ex->getMessage()); } }