Skip to content

Commit

Permalink
fix console re-parse command args and opttions error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 17, 2020
1 parent 01fb7d0 commit 98f9bc8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/console/src/Input/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Swoft\Console\Contract\InputInterface;
use Swoft\Console\Exception\CommandFlagException;
use function array_merge;
use function array_values;
use function getcwd;
use function is_bool;
use function is_int;
use function strpos;
use function trim;

/**
Expand Down Expand Up @@ -93,26 +95,27 @@ abstract public function toString(): string;

/**
* find command name. it is first argument.
*
* @param array $flags
*
* @return array
*/
protected function findCommand(): void
protected function findCommand(array $flags): array
{
if (!isset($this->args[0])) {
return;
if (!isset($flags[0])) {
return $flags;
}

$newArgs = [];

foreach ($this->args as $key => $value) {
if ($key === 0) {
$this->command = trim($value);
} elseif (is_int($key)) {
$newArgs[] = $value;
} else {
$newArgs[$key] = $value;
}
// Not input command name
if (strpos($flags[0], '-') === 0) {
return $flags;
}

$this->args = $newArgs;
$this->command = trim($flags[0]);

// remove first element, reset index key.
unset($flags[0]);
return array_values($flags);
}

/***********************************************************************************
Expand Down
7 changes: 3 additions & 4 deletions src/console/src/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ public function __construct(array $args = null, bool $parsing = true)
$this->scriptFile = array_shift($args);
$this->fullScript = implode(' ', $args);

$this->flags = $args;
// find command name, other is flags
$this->flags = $this->findCommand($args);
$this->pwd = $this->getPwd();

if ($parsing) {
// list($this->args, $this->sOpts, $this->lOpts) = InputParser::fromArgv($args);
[$this->args, $this->sOpts, $this->lOpts] = Flags::parseArgv($args);

// find command name
$this->findCommand();
}
}

Expand Down Expand Up @@ -133,6 +131,7 @@ public function parseFlags(array $info, bool $binding = false): void
}
}

// re-parsing
if ($this->flags) {
[$this->args, $this->sOpts, $this->lOpts] = Flags::parseArgv($this->flags, $config);

Expand Down

0 comments on commit 98f9bc8

Please sign in to comment.