Compare commits

..
2 Commits
Author SHA1 Message Date
mcndave 6c5f0b1f40 - Fixed for PHP 8.5 2026-07-14 14:24:02 +00:00
mcndave 91bcd381f8 - Set default value of option to null 2026-01-05 12:39:08 +00:00
5 changed files with 71 additions and 26 deletions

No files matched your search

+2 -2
View File
@@ -21,7 +21,7 @@ class CliMiddleware implements MiddlewareInterface
public ContainerInterface $container;
public null|CommandFetcher $fetcher;
public ?CommandFetcher $fetcher;
public CommandStack $commands;
@@ -31,7 +31,7 @@ class CliMiddleware implements MiddlewareInterface
protected array $parsedOptions = [];
public function __construct(ContainerInterface $container, callable $defaultResponseInterface = null, null|CommandFetcher $fetcher = null, null|CommandStack $stack = null, bool $allowAsciiFormatting = true) {
public function __construct(ContainerInterface $container, ? callable $defaultResponseInterface = null, ?CommandFetcher $fetcher = null, ?CommandStack $stack = null, bool $allowAsciiFormatting = true) {
$this->defaultResponse = $defaultResponseInterface;
$this->container = $container;
$this->fetcher = $fetcher;
-24
View File
@@ -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
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Mcnd\CLI;
trait CliTrait
{
}
+1
View File
@@ -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
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);
}
}