From b49ee5559d59dd58a6b7939a23a7682d9c570c28 Mon Sep 17 00:00:00 2001 From: Alannah Kearney Date: Fri, 24 May 2019 17:13:24 +1000 Subject: [PATCH] Expanded input types to include Flag, IncrementingFlag, and LongOption. Refactored Option and Argument types. --- src/Input/Types/Argument.php | 15 ++++++ src/Input/Types/Flag.php | 21 ++++++++ src/Input/Types/IncrementingFlag.php | 21 ++++++++ src/Input/Types/LongOption.php | 81 ++++++++++++++++++++++++++++ src/Input/Types/Option.php | 22 +------- 5 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 src/Input/Types/Flag.php create mode 100644 src/Input/Types/IncrementingFlag.php create mode 100644 src/Input/Types/LongOption.php diff --git a/src/Input/Types/Argument.php b/src/Input/Types/Argument.php index 1d589e6..3ada5b9 100644 --- a/src/Input/Types/Argument.php +++ b/src/Input/Types/Argument.php @@ -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..... diff --git a/src/Input/Types/Flag.php b/src/Input/Types/Flag.php new file mode 100644 index 0000000..fba5f55 --- /dev/null +++ b/src/Input/Types/Flag.php @@ -0,0 +1,21 @@ +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); + } +} diff --git a/src/Input/Types/Option.php b/src/Input/Types/Option.php index c3ef0d0..951caf4 100644 --- a/src/Input/Types/Option.php +++ b/src/Input/Types/Option.php @@ -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 );