Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 77 additions & 42 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,53 +174,88 @@ private function getParamWithEqual(string $arg, int $eqPos): array
*/
private function handleArguments(array $argv): array
{
$count = count($argv);
for ($i = 0, $j = $count; $i < $j; $i++) {
// --foo --bar=baz
if (str_starts_with($argv[$i], '--')) {
if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// --bar=baz
continue;
}

$key = $this->stripSlashes($argv[$i]);
if (!$this->isFlag($key) && $i + 1 < $j && !str_starts_with($argv[$i + 1], '-')) {// --foo value
$this->parsedCommands[$key] = $argv[$i + 1];
$i++;
continue;
}
$this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo
continue;
}

// -k=value -abc
if (str_starts_with($argv[$i], '-')) {
if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// -k=value
continue;
}

// -a value1 -abc value2 -abc
// A flag letter is true. The other letters take the value.
$hasNextElementDash = !($i + 1 < $j && !str_starts_with($argv[$i + 1], '-'));
$chars = str_split(substr($argv[$i], 1));
$takesValue = !$hasNextElementDash && array_diff($chars, $this->flags) !== [];
foreach ($chars as $char) {
$this->parsedCommands[$char] = $takesValue && !$this->isFlag($char)
? $argv[$i + 1]
: true;
}

if ($takesValue) {// -a value1 -abc value2
$i++;
}
continue;
}

$this->parsedCommands[] = $argv[$i];
$count = count($argv);
$position = 0;
while ($position < $count) {
$argument = $argv[$position];
$next = $argv[$position + 1] ?? null;
$value = $next === null || str_starts_with($next, '-') ? null : $next;

$position += match (true) {
str_starts_with($argument, '--') => $this->handleLongOption($argument, $value),
str_starts_with($argument, '-') => $this->handleShortOptions($argument, $value),
default => $this->handlePositional($argument),
};
}

return $this->parsedCommands;
}

/**
* Handle a long option: --foo, --foo value, --foo=bar
*
* @param string $argument The option
* @param string|null $value The next argument, if it is not an option
*
* @return int The number of arguments used
*/
private function handleLongOption(string $argument, ?string $value): int
{
if ($this->parseAndMergeCommandWithEqualSign($argument)) {// --foo=bar
return 1;
}

$key = $this->stripSlashes($argument);
if ($value !== null && !$this->isFlag($key)) {// --foo value
$this->parsedCommands[$key] = $value;

return 2;
}

$this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo

return 1;
}

/**
* Handle a positional argument
*
* @param string $argument The argument
*
* @return int The number of arguments used
*/
private function handlePositional(string $argument): int
{
$this->parsedCommands[] = $argument;

return 1;
}

/**
* Handle short options: -k=value, -abc, -abc value
*
* A flag letter is true. The other letters take the value.
*
* @param string $argument The options
* @param string|null $value The next argument, if it is not an option
*
* @return int The number of arguments used
*/
private function handleShortOptions(string $argument, ?string $value): int
{
if ($this->parseAndMergeCommandWithEqualSign($argument)) {// -k=value
return 1;
}

$letters = str_split(substr($argument, 1));
$takesValue = $value !== null && array_diff($letters, $this->flags) !== [];
foreach ($letters as $letter) {
$this->parsedCommands[$letter] = $takesValue && !$this->isFlag($letter) ? $value : true;
}

return $takesValue ? 2 : 1;
}

/**
* Check if an option takes no value.
*
Expand Down