- Set default value of option to null
This commit is contained in:
parent
9070bf3862
commit
91bcd381f8
@ -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,23 +63,8 @@ class CliRequest
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*elseif (is_array($currentOption)) {
|
||||
foreach($currentOption as $matchingOption) {
|
||||
$matchingOption->handle();
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
}*/
|
||||
# }
|
||||
#else {
|
||||
|
||||
# }
|
||||
}
|
||||
|
||||
# dump($options);
|
||||
}
|
||||
|
||||
protected function argv() : array
|
||||
{
|
||||
|
||||
8
src/CliTrait.php
Normal file
8
src/CliTrait.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Mcnd\CLI;
|
||||
|
||||
trait CliTrait
|
||||
{
|
||||
|
||||
}
|
||||
@ -93,6 +93,7 @@ class Option
|
||||
OptionValueTypeEnum::NoValue => $this->parsed->toggle(),
|
||||
OptionValueTypeEnum::CountValue => $this->parsed->toggle(),
|
||||
OptionValueTypeEnum::OptionalValue => $this->parsed->setValue(true),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
60
src/Terminal.php
Normal file
60
src/Terminal.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user