Compare commits
2
Commits
9070bf3862
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c5f0b1f40 | ||
|
|
91bcd381f8 |
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user