Upgrade argvParser with option values with ':'

This commit is contained in:
Tim 2025-04-29 22:15:15 +02:00
parent c7931f4e10
commit da3739d392
2 changed files with 14 additions and 3 deletions

0
.gitignore vendored Normal file
View File

View File

@ -25,6 +25,11 @@ class argParsed
$this->parsed[$key] = $value;
return $this;
}
public function getRest(): string
{
return $this->get('rest', '');
}
}
readonly class argvParser
@ -56,18 +61,24 @@ readonly class argvParser
$parsed = (new argParsed())->initOptions($this->options);
while (str_starts_with($argv[0] ?? '', '-')) {
$option = substr($argv[0], 1);
$option = substr($argv[0], 1, 1);
if (substr($argv[0], 2, 1) === ':') {
$data = substr($argv[0], 3);
} else {
$data = true;
}
if ($this->options[$option]) {
$parsed->set($option, true);
$parsed->set($option, $data);
}
array_shift($argv);
}
if (count($argv) !== count($this->arguments)) {
if (count($argv) < count($this->arguments)) {
return false;
}
foreach ($this->arguments as $arg => $description) {
$parsed->set($arg, array_shift($argv));
}
$parsed->set('rest', implode(' ', $argv));
return $parsed;
}