- Set default value of option to null

This commit is contained in:
Dave M. 2026-01-05 12:39:08 +00:00
parent 9070bf3862
commit 91bcd381f8
4 changed files with 69 additions and 24 deletions

View File

@ -48,15 +48,6 @@ class CliRequest
public function parseOptions(Command $command) : void public function parseOptions(Command $command) : void
{ {
$this->getopt($command->options); $this->getopt($command->options);
/*
if ( false !== $options ) {
if ($restIndex !== null) {
$this->rest = $this->argv()[$restIndex];
}
$this->options = $options;
}*/
} }
protected function getopt(OptionStack $options) :void protected function getopt(OptionStack $options) :void
@ -72,22 +63,7 @@ class CliRequest
return; return;
} }
/*elseif (is_array($currentOption)) {
foreach($currentOption as $matchingOption) {
$matchingOption->handle();
}
}
else {
}*/
# }
#else {
# }
} }
# dump($options);
} }
protected function argv() : array protected function argv() : array

8
src/CliTrait.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace Mcnd\CLI;
trait CliTrait
{
}

View File

@ -93,6 +93,7 @@ class Option
OptionValueTypeEnum::NoValue => $this->parsed->toggle(), OptionValueTypeEnum::NoValue => $this->parsed->toggle(),
OptionValueTypeEnum::CountValue => $this->parsed->toggle(), OptionValueTypeEnum::CountValue => $this->parsed->toggle(),
OptionValueTypeEnum::OptionalValue => $this->parsed->setValue(true), OptionValueTypeEnum::OptionalValue => $this->parsed->setValue(true),
default => null,
}; };
} }

60
src/Terminal.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace Mcnd\CLI;
class Terminal
{
public static function proposeListChoice(string $text, array $choices, mixed $default = null) : mixed
{
$range = range(1, count($choices));
$keys = array_combine($range, array_keys($choices));
static::printline($text);
foreach(array_combine($range, array_values($choices)) as $idx => $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);
}
}