mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Code tidy
This commit is contained in:
parent
866a2e1c9e
commit
419a4a9f79
14 changed files with 53 additions and 55 deletions
|
|
@ -23,7 +23,7 @@ $collection = (new Input\InputCollection())
|
||||||
->validator(new Input\Validator(
|
->validator(new Input\Validator(
|
||||||
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||||
// Make sure verbosity level never goes above 3
|
// Make sure verbosity level never goes above 3
|
||||||
return min(3, (int)$context->find('v'));
|
return min(3, (int) $context->find('v'));
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
|
|
@ -57,10 +57,10 @@ $collection = (new Input\InputCollection())
|
||||||
|
|
||||||
// Get the supplied input. Passing the collection will make the handler bind values
|
// Get the supplied input. Passing the collection will make the handler bind values
|
||||||
// and validate the input according to our collection
|
// and validate the input according to our collection
|
||||||
try{
|
try {
|
||||||
$argv = Input\InputHandlerFactory::build('Argv', $collection);
|
$argv = Input\InputHandlerFactory::build('Argv', $collection);
|
||||||
} catch(\Exception $ex) {
|
} catch (\Exception $ex) {
|
||||||
echo "Error when attempting to bind values to collection. Returned: " . $ex->getMessage() . PHP_EOL;
|
echo 'Error when attempting to bind values to collection. Returned: '.$ex->getMessage().PHP_EOL;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,7 +73,7 @@ echo Cli\manpage(
|
||||||
Colour::FG_GREEN,
|
Colour::FG_GREEN,
|
||||||
Colour::FG_WHITE,
|
Colour::FG_WHITE,
|
||||||
[
|
[
|
||||||
'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import'
|
'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import',
|
||||||
]
|
]
|
||||||
).PHP_EOL.PHP_EOL;
|
).PHP_EOL.PHP_EOL;
|
||||||
|
|
||||||
|
|
@ -92,7 +92,6 @@ echo Cli\manpage(
|
||||||
// Examples:
|
// Examples:
|
||||||
// php -f example/example.php -- -vvvs -d example/example.json import
|
// php -f example/example.php -- -vvvs -d example/example.json import
|
||||||
|
|
||||||
|
|
||||||
var_dump($argv->find('action'));
|
var_dump($argv->find('action'));
|
||||||
// string(6) "import"
|
// string(6) "import"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,12 +42,12 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
public function validate(): void
|
public function validate(): void
|
||||||
{
|
{
|
||||||
foreach ($this->collection->getItems() as $type => $items) {
|
foreach ($this->collection->getItems() as $type => $items) {
|
||||||
foreach($items as $input) {
|
foreach ($items as $input) {
|
||||||
self::checkRequiredAndRequiredValue($input, $this->input);
|
self::checkRequiredAndRequiredValue($input, $this->input);
|
||||||
|
|
||||||
// There is a default value, input has not been set, and there
|
// There is a default value, input has not been set, and there
|
||||||
// is no validator
|
// is no validator
|
||||||
if(
|
if (
|
||||||
null !== $input->default() &&
|
null !== $input->default() &&
|
||||||
null === $this->find($input->name()) &&
|
null === $this->find($input->name()) &&
|
||||||
null === $input->validator()
|
null === $input->validator()
|
||||||
|
|
@ -55,7 +55,7 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
$result = $input->default();
|
$result = $input->default();
|
||||||
|
|
||||||
// Input has been set and it has a validator
|
// Input has been set and it has a validator
|
||||||
} elseif(null !== $this->find($input->name()) && null !== $input->validator()) {
|
} elseif (null !== $this->find($input->name()) && null !== $input->validator()) {
|
||||||
$validator = $input->validator();
|
$validator = $input->validator();
|
||||||
|
|
||||||
if ($validator instanceof \Closure) {
|
if ($validator instanceof \Closure) {
|
||||||
|
|
@ -66,11 +66,11 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = $validator->validate($input, $this);
|
$result = $validator->validate($input, $this);
|
||||||
} catch(\Exception $ex) {
|
} catch (\Exception $ex) {
|
||||||
throw new Exceptions\InputValidationFailedException($input, 0, $ex);
|
throw new Exceptions\InputValidationFailedException($input, 0, $ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// No default, no validator, but may or may not have been set
|
// No default, no validator, but may or may not have been set
|
||||||
} else {
|
} else {
|
||||||
$result = $this->find($input->name());
|
$result = $this->find($input->name());
|
||||||
}
|
}
|
||||||
|
|
@ -82,14 +82,14 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
|
|
||||||
public function find(string $name)
|
public function find(string $name)
|
||||||
{
|
{
|
||||||
if(isset($this->input[$name])) {
|
if (isset($this->input[$name])) {
|
||||||
return $this->input[$name];
|
return $this->input[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the collection to see if anything responds to $name
|
// Check the collection to see if anything responds to $name
|
||||||
foreach($this->collection->getItems() as $type => $items) {
|
foreach ($this->collection->getItems() as $type => $items) {
|
||||||
foreach($items as $ii) {
|
foreach ($items as $ii) {
|
||||||
if($ii->respondsTo($name) && isset($this->input[$ii->name()])) {
|
if ($ii->respondsTo($name) && isset($this->input[$ii->name()])) {
|
||||||
return $this->input[$ii->name()];
|
return $this->input[$ii->name()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ abstract class AbstractInputType implements Interfaces\InputTypeInterface
|
||||||
$this->default = $default;
|
$this->default = $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __call($name, array $args=[])
|
public function __call($name, array $args = [])
|
||||||
{
|
{
|
||||||
if (empty($args)) {
|
if (empty($args)) {
|
||||||
return $this->$name;
|
return $this->$name;
|
||||||
|
|
@ -43,7 +43,7 @@ abstract class AbstractInputType implements Interfaces\InputTypeInterface
|
||||||
|
|
||||||
public function respondsTo(string $name): bool
|
public function respondsTo(string $name): bool
|
||||||
{
|
{
|
||||||
return ($name == $this->name);
|
return $name == $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType(): string
|
public function getType(): string
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,13 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace pointybeard\Helpers\Cli\Input\Exceptions;
|
namespace pointybeard\Helpers\Cli\Input\Exceptions;
|
||||||
|
|
||||||
use pointybeard\Helpers\Cli\Input\AbstractInputType;
|
use pointybeard\Helpers\Cli\Input\AbstractInputType;
|
||||||
|
|
||||||
class InputValidationFailedException extends \Exception
|
class InputValidationFailedException extends \Exception
|
||||||
{
|
{
|
||||||
public function __construct(AbstractInputType $input, $code = 0, \Exception $previous = null)
|
public function __construct(AbstractInputType $input, $code = 0, \Exception $previous = null)
|
||||||
{
|
{
|
||||||
return parent::__construct(sprintf("Validation failed for %s. Returned: %s", $input->getDisplayName(), $previous->getMessage()), $code, $previous);
|
return parent::__construct(sprintf('Validation failed for %s. Returned: %s', $input->getDisplayName(), $previous->getMessage()), $code, $previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ class RequiredInputMissingValueException extends \Exception
|
||||||
'a value is required for %s',
|
'a value is required for %s',
|
||||||
$input->getDisplayName()
|
$input->getDisplayName()
|
||||||
), $code, $previous);
|
), $code, $previous);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInput(): Input\AbstractInputType
|
public function getInput(): Input\AbstractInputType
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ class Argv extends Input\AbstractInputHandler
|
||||||
? $a->name()
|
? $a->name()
|
||||||
: $argumentCount
|
: $argumentCount
|
||||||
] = $token;
|
] = $token;
|
||||||
$argumentCount++;
|
++$argumentCount;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$it->next();
|
$it->next();
|
||||||
|
|
|
||||||
|
|
@ -30,42 +30,45 @@ class InputCollection
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find(string $name, array $restrictToType=null, array $excludeType=null, &$type = null, &$index = null): ?AbstractInputType
|
public function find(string $name, array $restrictToType = null, array $excludeType = null, &$type = null, &$index = null): ?AbstractInputType
|
||||||
{
|
{
|
||||||
foreach($this->items as $type => $items) {
|
foreach ($this->items as $type => $items) {
|
||||||
|
|
||||||
// Check if we're restricting to or excluding specific types
|
// Check if we're restricting to or excluding specific types
|
||||||
if(null !== $restrictToType && !in_array($type, $restrictToType)) {
|
if (null !== $restrictToType && !in_array($type, $restrictToType)) {
|
||||||
continue;
|
continue;
|
||||||
|
} elseif (null !== $excludeType && in_array($type, $excludeType)) {
|
||||||
} elseif(null !== $excludeType && in_array($type, $excludeType)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($items as $index => $item) {
|
foreach ($items as $index => $item) {
|
||||||
if($item->respondsTo($name)) {
|
if ($item->respondsTo($name)) {
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$type = null;
|
$type = null;
|
||||||
$index = null;
|
$index = null;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTypes(): array {
|
public function getTypes(): array
|
||||||
|
{
|
||||||
return array_keys($this->items);
|
return array_keys($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getItems(): array {
|
public function getItems(): array
|
||||||
|
{
|
||||||
return $this->items;
|
return $this->items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getItemsByType(string $type): array {
|
public function getItemsByType(string $type): array
|
||||||
|
{
|
||||||
return $this->items[$type] ?? [];
|
return $this->items[$type] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getItemByIndex(string $type, int $index): ?AbstractInputType {
|
public function getItemByIndex(string $type, int $index): ?AbstractInputType
|
||||||
|
{
|
||||||
return $this->items[$type][$index] ?? null;
|
return $this->items[$type][$index] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,8 +77,8 @@ class InputCollection
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|
||||||
foreach ($collections as $c) {
|
foreach ($collections as $c) {
|
||||||
foreach($c->items() as $type => $items) {
|
foreach ($c->items() as $type => $items) {
|
||||||
foreach($items as $item) {
|
foreach ($items as $item) {
|
||||||
$items[] = $item;
|
$items[] = $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace pointybeard\Helpers\Cli\Input;
|
namespace pointybeard\Helpers\Cli\Input;
|
||||||
|
|
||||||
use pointybeard\Helpers\Functions\Flags;
|
|
||||||
use pointybeard\Helpers\Foundation\Factory;
|
use pointybeard\Helpers\Foundation\Factory;
|
||||||
|
|
||||||
final class InputTypeFactory extends Factory\AbstractFactory
|
final class InputTypeFactory extends Factory\AbstractFactory
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,10 @@ interface InputTypeInterface
|
||||||
const FLAG_TYPE_INCREMENTING = 0x0400;
|
const FLAG_TYPE_INCREMENTING = 0x0400;
|
||||||
|
|
||||||
public function getType(): string;
|
public function getType(): string;
|
||||||
|
|
||||||
public function respondsTo(string $name): bool;
|
public function respondsTo(string $name): bool;
|
||||||
|
|
||||||
public function __toString(): string;
|
public function __toString(): string;
|
||||||
|
|
||||||
public function getDisplayName(): string;
|
public function getDisplayName(): string;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,9 @@ use pointybeard\Helpers\Functions\Cli;
|
||||||
|
|
||||||
class Argument extends Input\AbstractInputType
|
class Argument extends Input\AbstractInputType
|
||||||
{
|
{
|
||||||
|
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = null)
|
||||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default=null)
|
|
||||||
{
|
{
|
||||||
if(null === $validator) {
|
if (null === $validator) {
|
||||||
$validator = function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
$validator = function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||||
// This dummy validator is necessary otherwise the argument
|
// This dummy validator is necessary otherwise the argument
|
||||||
// value is ALWAYS set to default (most often NULL) regardless
|
// value is ALWAYS set to default (most often NULL) regardless
|
||||||
|
|
@ -25,7 +24,8 @@ class Argument extends Input\AbstractInputType
|
||||||
parent::__construct($name, $flags, $description, $validator, $default);
|
parent::__construct($name, $flags, $description, $validator, $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDisplayName(): string {
|
public function getDisplayName(): string
|
||||||
|
{
|
||||||
return strtoupper($this->name());
|
return strtoupper($this->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,13 @@ declare(strict_types=1);
|
||||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||||
|
|
||||||
use pointybeard\Helpers\Functions\Flags;
|
use pointybeard\Helpers\Functions\Flags;
|
||||||
use pointybeard\Helpers\Functions\Strings;
|
|
||||||
use pointybeard\Helpers\Functions\Cli;
|
|
||||||
use pointybeard\Helpers\Cli\Input;
|
|
||||||
|
|
||||||
class Flag extends Option
|
class Flag extends Option
|
||||||
{
|
{
|
||||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = false)
|
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)) {
|
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");
|
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);
|
parent::__construct($name, null, $flags, $description, $validator, $default);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,12 @@ declare(strict_types=1);
|
||||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||||
|
|
||||||
use pointybeard\Helpers\Functions\Flags;
|
use pointybeard\Helpers\Functions\Flags;
|
||||||
use pointybeard\Helpers\Functions\Strings;
|
|
||||||
use pointybeard\Helpers\Functions\Cli;
|
|
||||||
use pointybeard\Helpers\Cli\Input;
|
|
||||||
|
|
||||||
class IncrementingFlag extends Flag
|
class IncrementingFlag extends Flag
|
||||||
{
|
{
|
||||||
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = 0)
|
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)) {
|
if (Flags\is_flag_set($flags, self::FLAG_TYPE_INCREMENTING)) {
|
||||||
$flags = $flags | self::FLAG_TYPE_INCREMENTING;
|
$flags = $flags | self::FLAG_TYPE_INCREMENTING;
|
||||||
}
|
}
|
||||||
parent::__construct($name, null, $flags, $description, $validator, $default);
|
parent::__construct($name, null, $flags, $description, $validator, $default);
|
||||||
|
|
|
||||||
|
|
@ -21,18 +21,18 @@ class LongOption extends Input\AbstractInputType
|
||||||
|
|
||||||
public function respondsTo(string $name): bool
|
public function respondsTo(string $name): bool
|
||||||
{
|
{
|
||||||
return ($name == $this->name || $name == $this->short);
|
return $name == $this->name || $name == $this->short;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDisplayName(): string {
|
public function getDisplayName(): string
|
||||||
|
{
|
||||||
$short =
|
$short =
|
||||||
null !== $this->short()
|
null !== $this->short()
|
||||||
? '-'.$this->short().', '
|
? '-'.$this->short().', '
|
||||||
: null
|
: null
|
||||||
;
|
;
|
||||||
|
|
||||||
return sprintf("%s--%s", $short, $this->name());
|
return sprintf('%s--%s', $short, $this->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,14 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace pointybeard\Helpers\Cli\Input\Types;
|
namespace pointybeard\Helpers\Cli\Input\Types;
|
||||||
|
|
||||||
use pointybeard\Helpers\Functions\Flags;
|
|
||||||
use pointybeard\Helpers\Functions\Strings;
|
use pointybeard\Helpers\Functions\Strings;
|
||||||
use pointybeard\Helpers\Functions\Cli;
|
use pointybeard\Helpers\Functions\Cli;
|
||||||
use pointybeard\Helpers\Cli\Input;
|
use pointybeard\Helpers\Cli\Input;
|
||||||
|
|
||||||
class Option extends Input\AbstractInputType
|
class Option extends Input\AbstractInputType
|
||||||
{
|
{
|
||||||
public function getDisplayName(): string {
|
public function getDisplayName(): string
|
||||||
|
{
|
||||||
return '-'.$this->name();
|
return '-'.$this->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue