diff --git a/php/argvParser.php b/php/argvParser.php new file mode 100644 index 0000000..05719ac --- /dev/null +++ b/php/argvParser.php @@ -0,0 +1,74 @@ +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; + } +} + +readonly class argvParser +{ + /** + * @param array $options + * @param array $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 + + $parsed = (new argParsed())->initOptions($this->options); + while (str_starts_with($argv[0] ?? '', '-')) { + $option = substr($argv[0], 1); + if ($this->options[$option]) { + $parsed->set($option, true); + } + 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; + } +} \ No newline at end of file diff --git a/bin/bin/stow b/php/bin/stow similarity index 64% rename from bin/bin/stow rename to php/bin/stow index eb940a9..f389769 100755 --- a/bin/bin/stow +++ b/php/bin/stow @@ -1,115 +1,8 @@ #!/usr/bin/env php 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; - } -} - -readonly class argvParser -{ - /** - * @param array $options - * @param array $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); - if ($this->options[$option]) { - $parsed->set($option, true); - } - 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; - } -} - -$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); -$targetPath = dirname($stowPath, 2); - -if (!is_dir($stowPath)) { - line("Directory '$stowName' does not exist"); - exit(1); -} +require_once __DIR__ . '/../argvParser.php'; +require_once __DIR__ . '/../functions.php'; readonly class stow { @@ -186,6 +79,30 @@ readonly class stow } } +$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); +$targetPath = dirname($stowPath, 2); + +if (!is_dir($stowPath)) { + line("Directory '$stowName' does not exist"); + exit(1); +} + $stow = new stow($action, $backup, $force); $stow->run($stowPath, $targetPath); diff --git a/php/functions.php b/php/functions.php new file mode 100644 index 0000000..61e2774 --- /dev/null +++ b/php/functions.php @@ -0,0 +1,11 @@ +