Move stow to it's own dir and separate include files out
This commit is contained in:
parent
511881bb09
commit
c7931f4e10
74
php/argvParser.php
Normal file
74
php/argvParser.php
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class argParsed
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private array $parsed = [],
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function initOptions(array $options, mixed $default = false): self
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,115 +1,8 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
function path(...$segments): string
|
require_once __DIR__ . '/../argvParser.php';
|
||||||
{
|
require_once __DIR__ . '/../functions.php';
|
||||||
return join(DIRECTORY_SEPARATOR, $segments);
|
|
||||||
}
|
|
||||||
|
|
||||||
function line(string $line): void
|
|
||||||
{
|
|
||||||
echo $line . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
class argParsed
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private array $parsed = [],
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public function initOptions(array $options, mixed $default = false): self
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
readonly class stow
|
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 = new stow($action, $backup, $force);
|
||||||
$stow->run($stowPath, $targetPath);
|
$stow->run($stowPath, $targetPath);
|
||||||
|
|
11
php/functions.php
Normal file
11
php/functions.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
function path(...$segments): string
|
||||||
|
{
|
||||||
|
return join(DIRECTORY_SEPARATOR, $segments);
|
||||||
|
}
|
||||||
|
|
||||||
|
function line(string $line): void
|
||||||
|
{
|
||||||
|
echo $line . PHP_EOL;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user