11
0
Fork 0
mirror of https://github.com/n3w/helpers-cli-input.git synced 2025-12-21 13:43:22 +00:00

Compare commits

...

7 commits

9 changed files with 37 additions and 57 deletions

View file

@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). This project adheres to [Semantic Versioning](http://semver.org/).
## [1.2.2][]
#### Changed
- Minor improvement to logic in `AbstractInputHandler::validateInput()`. Ensures that an input with a validator, but with a default value and no user suplied input, will have the default value used.
## [1.2.1][]
#### Changed
- Updated `InputHandlerFactory` and `InputTypeFactory` to work with changes in `pointybeard/helpers-foundation-factory` 1.0.2
## [1.2.0][] ## [1.2.0][]
#### Added #### Added
- Added `InputTypeFilterIterator` class - Added `InputTypeFilterIterator` class
@ -73,6 +81,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
#### Added #### Added
- Initial release - Initial release
[1.2.2]: https://github.com/pointybeard/helpers-functions-cli/compare/1.2.1...1.2.2
[1.2.1]: https://github.com/pointybeard/helpers-functions-cli/compare/1.2.0...1.2.1
[1.2.0]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.4...1.2.0 [1.2.0]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.4...1.2.0
[1.1.4]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.3...1.1.4 [1.1.4]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.3...1.1.4
[1.1.3]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.2...1.1.3 [1.1.3]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.2...1.1.3

View file

@ -1,7 +1,7 @@
# PHP Helpers: Command-line Input and Input Type Handlers # PHP Helpers: Command-line Input and Input Type Handlers
- Version: v1.2.0 - Version: v1.2.2
- Date: June 01 2019 - Date: Aug 06 2019
- [Release notes](https://github.com/pointybeard/helpers-cli-input/blob/master/CHANGELOG.md) - [Release notes](https://github.com/pointybeard/helpers-cli-input/blob/master/CHANGELOG.md)
- [GitHub repository](https://github.com/pointybeard/helpers-cli-input) - [GitHub repository](https://github.com/pointybeard/helpers-cli-input)

View file

@ -1,6 +1,5 @@
{ {
"name": "pointybeard/helpers-cli-input", "name": "pointybeard/helpers-cli-input",
"version": "1.2.0",
"description": "Collection of classes for handling argv (and other) input when calling command-line scripts. Helps with parsing, collecting and validating arguments, options, and flags.", "description": "Collection of classes for handling argv (and other) input when calling command-line scripts. Helps with parsing, collecting and validating arguments, options, and flags.",
"homepage": "https://github.com/pointybeard/helpers-cli-input", "homepage": "https://github.com/pointybeard/helpers-cli-input",
"license": "MIT", "license": "MIT",
@ -17,12 +16,6 @@
"pointybeard/helpers-foundation-factory": "~1.0", "pointybeard/helpers-foundation-factory": "~1.0",
"pointybeard/helpers-functions-flags": "~1.0" "pointybeard/helpers-functions-flags": "~1.0"
}, },
"require-dev": {
"phpunit/phpunit": "^8",
"pointybeard/helpers-functions-strings": "~1.1.0",
"pointybeard/helpers-cli-colour": "~1.0",
"pointybeard/helpers-functions-cli": "~1.1.0"
},
"support": { "support": {
"issues": "https://github.com/pointybeard/helpers-cli-input/issues", "issues": "https://github.com/pointybeard/helpers-cli-input/issues",
"wiki": "https://github.com/pointybeard/helpers-cli-input/wiki" "wiki": "https://github.com/pointybeard/helpers-cli-input/wiki"

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
include __DIR__.'/../vendor/autoload.php'; include __DIR__.'/../vendor/autoload.php';
use pointybeard\Helpers\Cli\Input; use pointybeard\Helpers\Cli\Input;
use pointybeard\Helpers\Cli\Colour\Colour;
use pointybeard\Helpers\Functions\Cli; use pointybeard\Helpers\Functions\Cli;
// Define what we are expecting to get from the command line // Define what we are expecting to get from the command line
@ -64,34 +63,6 @@ try {
exit; exit;
} }
// Display the manual in green text
echo Cli\manpage(
basename(__FILE__),
'1.0.2',
'An example script for the PHP Helpers: Command-line Input and Input Type Handlers composer library (pointybeard/helpers-cli-input).',
$collection,
Colour::FG_GREEN,
Colour::FG_WHITE,
[
'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import',
]
).PHP_EOL.PHP_EOL;
// example.php 1.0.2, An example script for the PHP Helpers: Command-line Input
// and Input Type Handlers composer library (pointybeard/helpers-cli-input).
// Usage: example.php [OPTIONS]... ACTION...
//
// Arguments:
// ACTION The name of the action to perform
//
// Options:
// -v verbosity level. -v (errors only), -vv
// (warnings and errors), -vvv (everything).
// -d, --data=VALUE Path to the input JSON data
//
// Examples:
// php -f example/example.php -- -vvv -d example/example.json import
var_dump($argv->find('action')); var_dump($argv->find('action'));
// string(6) "import" // string(6) "import"

View file

@ -68,17 +68,17 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_REQUIRED)) { if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_REQUIRED)) {
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 and input has not been set. Assign the
// is no validator // default value to the result.
if ( if (
null !== $input->default() && null !== $input->default() &&
null === $this->find($input->name()) && null === $this->find($input->name())
null === $input->validator()
) { ) {
$result = $input->default(); $result = $input->default();
// Input has been set and it has a validator. Skip this if // Input has been set AND it has a validator. Run the validator over the
// FLAG_VALIDATION_SKIP_CUSTOM is set // input. Note, this will be skipped if FLAG_VALIDATION_SKIP_CUSTOM is
// set
} elseif (null !== $this->find($input->name()) && null !== $input->validator() && !Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_CUSTOM)) { } elseif (null !== $this->find($input->name()) && null !== $input->validator() && !Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_CUSTOM)) {
$validator = $input->validator(); $validator = $input->validator();
@ -94,7 +94,8 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
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, but may or may not have been set so assign whatever value
// it might have to the result
} else { } else {
$result = $this->find($input->name()); $result = $this->find($input->name());
} }

View file

@ -9,27 +9,34 @@ use pointybeard\Helpers\Foundation\Factory;
final class InputHandlerFactory extends Factory\AbstractFactory final class InputHandlerFactory extends Factory\AbstractFactory
{ {
public static function getTemplateNamespace(): string public function getTemplateNamespace(): string
{ {
return __NAMESPACE__.'\\Handlers\\%s'; return __NAMESPACE__.'\\Handlers\\%s';
} }
public static function getExpectedClassType(): ?string public function getExpectedClassType(): ?string
{ {
return __NAMESPACE__.'\\Interfaces\\InputHandlerInterface'; return __NAMESPACE__.'\\Interfaces\\InputHandlerInterface';
} }
public static function build(string $name, InputCollection $collection = null, int $flags = null): Interfaces\InputHandlerInterface public static function build(string $name, ...$arguments): object
{ {
// Since passing flags is optional, we can use array_pad
// to ensure there are always at least 2 elements in $arguments
[$collection, $flags] = array_pad($arguments, 2, null);
$factory = new self;
try { try {
$handler = self::instanciate( $handler = $factory->instanciate(
self::generateTargetClassName($name) $factory->generateTargetClassName($name)
); );
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw new Exceptions\UnableToLoadInputHandlerException($name, 0, $ex); throw new Exceptions\UnableToLoadInputHandlerException($name, 0, $ex);
} }
if ($collection instanceof InputCollection) { if (null !== $collection) {
$handler->bind( $handler->bind(
$collection, $collection,
$flags $flags

View file

@ -8,14 +8,12 @@ use pointybeard\Helpers\Foundation\Factory;
final class InputTypeFactory extends Factory\AbstractFactory final class InputTypeFactory extends Factory\AbstractFactory
{ {
use Factory\Traits\hasSimpleFactoryBuildMethodTrait; public function getTemplateNamespace(): string
public static function getTemplateNamespace(): string
{ {
return __NAMESPACE__.'\\Types\\%s'; return __NAMESPACE__.'\\Types\\%s';
} }
public static function getExpectedClassType(): ?string public function getExpectedClassType(): ?string
{ {
return __NAMESPACE__.'\\Interfaces\\InputTypeInterface'; return __NAMESPACE__.'\\Interfaces\\InputTypeInterface';
} }

View file

@ -72,6 +72,6 @@ class Argument extends Input\AbstractInputType
$second[$ii] = $secondaryLineLeadPadding.$second[$ii]; $second[$ii] = $secondaryLineLeadPadding.$second[$ii];
} }
return $first.implode($second, PHP_EOL); return $first.implode(PHP_EOL, $second);
} }
} }

View file

@ -84,6 +84,6 @@ class LongOption extends Input\AbstractInputType
$second[$ii] = $secondaryLineLeadPadding.$second[$ii]; $second[$ii] = $secondaryLineLeadPadding.$second[$ii];
} }
return $first.implode($second, PHP_EOL); return $first.implode(PHP_EOL, $second);
} }
} }