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