mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 20:53:27 +00:00
Expanded input types to include Flag, IncrementingFlag, and LongOption. Refactored Option and Argument types.
This commit is contained in:
parent
b36bfeac3d
commit
b49ee5559d
5 changed files with 139 additions and 21 deletions
|
|
@ -10,6 +10,21 @@ use pointybeard\Helpers\Functions\Cli;
|
|||
|
||||
class Argument extends Input\AbstractInputType
|
||||
{
|
||||
|
||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default=null)
|
||||
{
|
||||
if(null === $validator) {
|
||||
$validator = function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||
// This dummy validator is necessary otherwise the argument
|
||||
// value is ALWAYS set to default (most often NULL) regardless
|
||||
// of if the argument was set or not
|
||||
return $context->find($input->name());
|
||||
};
|
||||
}
|
||||
|
||||
parent::__construct($name, $flags, $description, $validator, $default);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
// MAGIC VALUES!!! OH MY.....
|
||||
|
|
|
|||
21
src/Input/Types/Flag.php
Normal file
21
src/Input/Types/Flag.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||
|
||||
use pointybeard\Helpers\Functions\Flags;
|
||||
use pointybeard\Helpers\Functions\Strings;
|
||||
use pointybeard\Helpers\Functions\Cli;
|
||||
use pointybeard\Helpers\Cli\Input;
|
||||
|
||||
class Flag extends Option
|
||||
{
|
||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = false)
|
||||
{
|
||||
if(Flags\is_flag_set($flags, self::FLAG_VALUE_REQUIRED) || Flags\is_flag_set($flags, self::FLAG_VALUE_OPTIONAL)) {
|
||||
throw new \Exception("The flags FLAG_VALUE_REQUIRED and FLAG_VALUE_OPTIONAL cannot be used on an input of type Flag");
|
||||
}
|
||||
parent::__construct($name, null, $flags, $description, $validator, $default);
|
||||
}
|
||||
}
|
||||
21
src/Input/Types/IncrementingFlag.php
Normal file
21
src/Input/Types/IncrementingFlag.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||
|
||||
use pointybeard\Helpers\Functions\Flags;
|
||||
use pointybeard\Helpers\Functions\Strings;
|
||||
use pointybeard\Helpers\Functions\Cli;
|
||||
use pointybeard\Helpers\Cli\Input;
|
||||
|
||||
class IncrementingFlag extends Flag
|
||||
{
|
||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = 0)
|
||||
{
|
||||
if(Flags\is_flag_set($flags, self::FLAG_TYPE_INCREMENTING)) {
|
||||
$flags = $flags | self::FLAG_TYPE_INCREMENTING;
|
||||
}
|
||||
parent::__construct($name, null, $flags, $description, $validator, $default);
|
||||
}
|
||||
}
|
||||
81
src/Input/Types/LongOption.php
Normal file
81
src/Input/Types/LongOption.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||
|
||||
use pointybeard\Helpers\Functions\Flags;
|
||||
use pointybeard\Helpers\Functions\Strings;
|
||||
use pointybeard\Helpers\Functions\Cli;
|
||||
use pointybeard\Helpers\Cli\Input;
|
||||
|
||||
class LongOption extends Input\AbstractInputType
|
||||
{
|
||||
protected $short;
|
||||
|
||||
public function __construct(string $name = null, string $short = null, int $flags = null, string $description = null, object $validator = null, $default = false)
|
||||
{
|
||||
$this->short = $short;
|
||||
parent::__construct($name, $flags, $description, $validator, $default);
|
||||
}
|
||||
|
||||
public function respondsTo(string $name): bool
|
||||
{
|
||||
return ($name == $this->name || $name == $this->short);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
// MAGIC VALUES!!! OH MY.....
|
||||
$padCharacter = ' ';
|
||||
$paddingBufferSize = 0.15; // 15%
|
||||
$optionNamePaddedWidth = 30;
|
||||
$minimumWindowWidth = 80;
|
||||
$secondaryLineIndentlength = 2;
|
||||
|
||||
// Get the window dimensions but restrict width to minimum
|
||||
// of $minimumWindowWidth
|
||||
$window = Cli\get_window_size();
|
||||
$window['cols'] = max($minimumWindowWidth, $window['cols']);
|
||||
|
||||
// This shrinks the total line length (derived by the window width) by
|
||||
// $paddingBufferSize
|
||||
$paddingBuffer = (int) ceil($window['cols'] * $paddingBufferSize);
|
||||
|
||||
// Create a string of $padCharacter which is prepended to each secondary
|
||||
// line
|
||||
$secondaryLineLeadPadding = str_pad(
|
||||
'',
|
||||
$optionNamePaddedWidth,
|
||||
$padCharacter,
|
||||
STR_PAD_LEFT
|
||||
);
|
||||
|
||||
$short = null !== $this->short() ? '-'.$this->short() : null;
|
||||
$long = null;
|
||||
|
||||
$long = '--'.$this->name();
|
||||
if (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_REQUIRED)) {
|
||||
$long .= '=VALUE';
|
||||
} elseif (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_OPTIONAL)) {
|
||||
$long .= '[=VALUE]';
|
||||
}
|
||||
|
||||
$first = Strings\mb_str_pad(
|
||||
(null !== $short ? "{$short}, " : '').$long, // -O, --LONG,
|
||||
$optionNamePaddedWidth,
|
||||
$padCharacter
|
||||
);
|
||||
|
||||
$second = Strings\utf8_wordwrap_array(
|
||||
$this->description(),
|
||||
$window['cols'] - $optionNamePaddedWidth - $paddingBuffer
|
||||
);
|
||||
|
||||
for ($ii = 1; $ii < count($second); ++$ii) {
|
||||
$second[$ii] = $secondaryLineLeadPadding.$second[$ii];
|
||||
}
|
||||
|
||||
return $first.implode($second, PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,16 +11,6 @@ use pointybeard\Helpers\Cli\Input;
|
|||
|
||||
class Option extends Input\AbstractInputType
|
||||
{
|
||||
protected $long;
|
||||
protected $default;
|
||||
|
||||
public function __construct(string $name, string $long = null, int $flags = null, string $description = null, object $validator = null, $default = false)
|
||||
{
|
||||
$this->default = $default;
|
||||
$this->long = $long;
|
||||
parent::__construct($name, $flags, $description, $validator);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
// MAGIC VALUES!!! OH MY.....
|
||||
|
|
@ -49,19 +39,9 @@ class Option extends Input\AbstractInputType
|
|||
);
|
||||
|
||||
$short = '-'.$this->name();
|
||||
$long = null;
|
||||
|
||||
if (null !== $this->long()) {
|
||||
$long = '--'.$this->long();
|
||||
if (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_REQUIRED)) {
|
||||
$long .= '=VALUE';
|
||||
} elseif (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_OPTIONAL)) {
|
||||
$long .= '[=VALUE]';
|
||||
}
|
||||
}
|
||||
|
||||
$first = Strings\mb_str_pad(
|
||||
$short.(null !== $long ? ", {$long}" : ''), // -O, --LONG,
|
||||
$short,
|
||||
$optionNamePaddedWidth,
|
||||
$padCharacter
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue