From 91bcd381f895685c4e09ff84144d251780938ce9 Mon Sep 17 00:00:00 2001 From: Dave M Date: Mon, 5 Jan 2026 12:39:08 +0000 Subject: [PATCH] - Set default value of option to null --- src/CliRequest.php | 24 ------------------- src/CliTrait.php | 8 +++++++ src/Option.php | 1 + src/Terminal.php | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/CliTrait.php create mode 100644 src/Terminal.php diff --git a/src/CliRequest.php b/src/CliRequest.php index f80b44d..9a44193 100644 --- a/src/CliRequest.php +++ b/src/CliRequest.php @@ -48,15 +48,6 @@ class CliRequest public function parseOptions(Command $command) : void { $this->getopt($command->options); - -/* - if ( false !== $options ) { - if ($restIndex !== null) { - $this->rest = $this->argv()[$restIndex]; - } - - $this->options = $options; - }*/ } protected function getopt(OptionStack $options) :void @@ -72,22 +63,7 @@ class CliRequest return; } - - /*elseif (is_array($currentOption)) { - foreach($currentOption as $matchingOption) { - $matchingOption->handle(); - } - } - else { - - }*/ - # } - #else { - - # } } - - # dump($options); } protected function argv() : array diff --git a/src/CliTrait.php b/src/CliTrait.php new file mode 100644 index 0000000..813c3d1 --- /dev/null +++ b/src/CliTrait.php @@ -0,0 +1,8 @@ + $this->parsed->toggle(), OptionValueTypeEnum::CountValue => $this->parsed->toggle(), OptionValueTypeEnum::OptionalValue => $this->parsed->setValue(true), + default => null, }; } diff --git a/src/Terminal.php b/src/Terminal.php new file mode 100644 index 0000000..4b7a264 --- /dev/null +++ b/src/Terminal.php @@ -0,0 +1,60 @@ + $content) { + static::printline(" {$idx} - {$content}"); + } + + $read = static::readline("Selection : ") ?: $default; + + if (! isset($keys[$read])) { + # Retry + return static::proposeListChoice($text, $choices, $default); + } + + return $keys[$read]; + } + + public static function print(string ...$texts) { + $handle = fopen("php://stdin", "w"); + + foreach($texts as $text) { + fputs($handle, $text); + } + + fclose($handle); + } + + public static function printline(string $text) + { + return static::print($text, PHP_EOL); + } + + public static function sprintfline(string $text, mixed... $arguments) + { + return static::printline(sprintf($text, ...$arguments)); + } + + public static function readline(?string $prompt): string|false + { + static::print($prompt); + + $handle = fopen("php://stdin", "r"); + + $return = fgets($handle); + + fclose($handle); + + return trim($return, PHP_EOL); + } +} \ No newline at end of file