Add argv parser class

This commit is contained in:
Tim 2025-04-11 16:19:07 +02:00
parent 2d0c74b25e
commit ca647f9382

View File

@ -11,36 +11,98 @@ function line(string $line): void
echo $line . PHP_EOL; echo $line . PHP_EOL;
} }
$cwd = getcwd(); class argParsed
$action = 'stow'; {
$backup = $force = false; public function __construct(
array_shift($argv); // we don't care about the script name private array $parsed = [],
) {}
if (count($argv) === 0) { public function initOptions(array $options, mixed $default = false): self
line("Usage: stow [-u] [-b] [-f] <stow-directory>"); {
exit(1); foreach (array_keys($options) as $name) {
$this->set($name, $default);
}
return $this;
}
public function get(string $key, mixed $default = null): mixed
{
return $this->parsed[$key] ?? $default;
}
public function set(string $key, mixed $value): self
{
$this->parsed[$key] = $value;
return $this;
}
} }
while (str_starts_with($argv[0], '-')) { readonly class argvParser
{
/**
* @param array<string, string> $options
* @param array<string, string> $arguments
*/
public function __construct(
private array $options = [],
private array $arguments = [],
) {}
public function getOptionsHelp(): string
{
$line = '';
foreach ($this->options as $option => $description) {
$line .= sprintf('[-%s (%s)] ', $option, $description);
}
foreach ($this->arguments as $argument => $description) {
$line .= sprintf('<%s (%s)> ', $argument, $description);
}
return $line;
}
public function parseArgv(array $argv): argParsed|false
{
array_shift($argv); // shift the script name
if (count($argv) < count($this->arguments)) {
return false;
}
$parsed = (new argParsed())->initOptions($this->options);
while (str_starts_with($argv[0], '-')) {
$option = substr($argv[0], 1); $option = substr($argv[0], 1);
switch ($option) { if ($this->options[$option]) {
case 'u': $parsed->set($option, true);
$action = 'unstow';
break;
case 'b':
$backup = true;
break;
case 'f':
$force = true;
break;
default:
line("Unknown option: $option");
exit(1);
} }
array_shift($argv); array_shift($argv);
}
if (count($argv) !== count($this->arguments)) {
return false;
}
foreach ($this->arguments as $arg => $description) {
$parsed->set($arg, array_shift($argv));
}
return $parsed;
}
} }
$stowName = $argv[0]; $argvParser = new ArgvParser(
['u' => 'Unstow files', 'b' => 'Backup existing files', 'f' => 'Force overwrite'],
['stow-directory' => 'The directory to stow']
);
$parsed = $argvParser->parseArgv($argv);
if ($parsed === false) {
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
exit(1);
}
$cwd = getcwd();
$action = $parsed->get('u') ? 'unstow' : 'stow';
$backup = $parsed->get('b');
$force = $parsed->get('f');
$stowName = $parsed->get('stow-directory');
$stowPath = path($cwd, $stowName); $stowPath = path($cwd, $stowName);
$targetPath = dirname($stowPath, 2); $targetPath = dirname($stowPath, 2);
@ -126,3 +188,5 @@ readonly class stow
$stow = new stow($action, $backup, $force); $stow = new stow($action, $backup, $force);
$stow->run($stowPath, $targetPath); $stow->run($stowPath, $targetPath);
exit(0);