Skip to content

Commit

Permalink
Fix CLI parsing to not choke on unexpected input
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Feb 25, 2016
1 parent 86f558b commit c8638c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/commands/CommandLocator.php
Expand Up @@ -26,6 +26,8 @@ public function __construct(Factory $factory) {
public function getCommandForRequest(CLI\Request $request) {
$command = $request->getCommand();
switch ($command) {

case '':
case 'help': {
return $this->factory->getHelpCommand();
}
Expand Down
8 changes: 6 additions & 2 deletions src/shared/cli/Options.php
Expand Up @@ -38,8 +38,12 @@ private function parseOptions(array $options) {
$this->options[$key] = $value;
continue;
}
$this->options[ltrim($option, '-')] = $options[$idx + 1];
$skipNext = true;
if (isset($options[$idx + 1])) {
$this->options[ltrim($option, '-')] = $options[$idx + 1];
$skipNext = true;
} else {
$this->options[ltrim($option, '-')] = true;
}
continue;
}
if (strpos($option, '-') === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/cli/Request.php
Expand Up @@ -66,7 +66,7 @@ private function parse() {
*/
private function extractOptions() {
$opts = [];
while (strpos($this->argv[0], '-') === 0) {
while (count($this->argv) && strpos($this->argv[0][0], '-') === 0) {
$opts[] = array_shift($this->argv);
}
return $opts;
Expand Down

0 comments on commit c8638c5

Please sign in to comment.