mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Updated README, CHANGELOG, example code, and composer.json for 1.1.0
This commit is contained in:
parent
9e27b52991
commit
a4a8d5e351
4 changed files with 93 additions and 56 deletions
17
CHANGELOG.md
17
CHANGELOG.md
|
|
@ -5,6 +5,19 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
**View all [Unreleased][] changes here**
|
**View all [Unreleased][] changes here**
|
||||||
|
|
||||||
|
## [1.1.0][]
|
||||||
|
#### Added
|
||||||
|
- Expanded input types to include `Flag`, `IncrementingFlag`, and `LongOption`.
|
||||||
|
- Added `InputTypeFactory` to help with loading input type classes
|
||||||
|
|
||||||
|
#### Changed
|
||||||
|
- Updated to work with more than just `Argument` and `Option` input types. Makes use of `InputTypeFactory` to allow addition of new types as needed.
|
||||||
|
|
||||||
|
## [1.0.2][]
|
||||||
|
#### Changed
|
||||||
|
- Updated example to reflect changes to `manpage()` function in `pointybeard/helpers-functions-cli` package
|
||||||
|
- Refactoring and improvemnts to `Argument::__toString()` and `Option::__toString()`
|
||||||
|
|
||||||
## [1.0.2][]
|
## [1.0.2][]
|
||||||
#### Fixed
|
#### Fixed
|
||||||
- Fixed `InputCollection::getArgumentsByIndex()` so it returns NULL if the index does not exist instead of throwing an E_NOTICE message
|
- Fixed `InputCollection::getArgumentsByIndex()` so it returns NULL if the index does not exist instead of throwing an E_NOTICE message
|
||||||
|
|
@ -17,6 +30,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
#### Added
|
#### Added
|
||||||
- Initial release
|
- Initial release
|
||||||
|
|
||||||
[Unreleased]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.2...integration
|
[Unreleased]: https://github.com/pointybeard/helpers-functions-cli/compare/1.1.0...integration
|
||||||
|
[1.1.0]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.3...1.1.0
|
||||||
|
[1.0.3]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.2...1.0.3
|
||||||
[1.0.2]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.1...1.0.2
|
[1.0.2]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.1...1.0.2
|
||||||
[1.0.1]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.0...1.0.1
|
[1.0.1]: https://github.com/pointybeard/helpers-functions-cli/compare/1.0.0...1.0.1
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# PHP Helpers: Command-line Input and Input Type Handlers
|
# PHP Helpers: Command-line Input and Input Type Handlers
|
||||||
|
|
||||||
- Version: v1.0.3
|
- Version: v1.1.0
|
||||||
- Date: May 24 2019
|
- Date: May 24 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)
|
||||||
|
|
@ -9,7 +9,7 @@ Collection of classes for handling argv (and other) input when calling command-l
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
This library is installed via [Composer](http://getcomposer.org/). To install, use `composer require pointybeard/helpers-cli-input` or add `"pointybeard/helpers-cli-input": "~1.0"` to your `composer.json` file.
|
This library is installed via [Composer](http://getcomposer.org/). To install, use `composer require pointybeard/helpers-cli-input` or add `"pointybeard/helpers-cli-input": "~1.1"` to your `composer.json` file.
|
||||||
|
|
||||||
And run composer to update your dependencies:
|
And run composer to update your dependencies:
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@ To include all the [PHP Helpers](https://github.com/pointybeard/helpers) package
|
||||||
|
|
||||||
Include this library in your PHP files with `use pointybeard\Helpers\Cli`. See example code in `example/example.php`. The example code can be run with the following command:
|
Include this library in your PHP files with `use pointybeard\Helpers\Cli`. See example code in `example/example.php`. The example code can be run with the following command:
|
||||||
|
|
||||||
php -f example/example.php -- -vvv -d example/example.json import
|
php -f example/example.php -- -vvvs -d example/example.json import
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "pointybeard/helpers-cli-input",
|
"name": "pointybeard/helpers-cli-input",
|
||||||
"version": "1.0.3",
|
"version": "1.1.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",
|
||||||
|
|
|
||||||
|
|
@ -9,47 +9,60 @@ 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
|
||||||
$collection = (new Input\InputCollection())
|
$collection = (new Input\InputCollection())
|
||||||
->append(new Input\Types\Argument(
|
->append(
|
||||||
'action',
|
Input\InputTypeFactory::build('Argument')
|
||||||
Input\AbstractInputType::FLAG_REQUIRED,
|
->name('action')
|
||||||
'The name of the action to perform'
|
->flags(Input\AbstractInputType::FLAG_REQUIRED)
|
||||||
))
|
->description('The name of the action to perform')
|
||||||
->append(new Input\Types\Option(
|
)
|
||||||
'v',
|
->append(
|
||||||
null,
|
Input\InputTypeFactory::build('IncrementingFlag')
|
||||||
Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_TYPE_INCREMENTING,
|
->name('v')
|
||||||
'verbosity level. -v (errors only), -vv (warnings and errors), -vvv (everything).',
|
->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_TYPE_INCREMENTING)
|
||||||
null,
|
->description('verbosity level. -v (errors only), -vv (warnings and errors), -vvv (everything).')
|
||||||
0
|
->validator(new Input\Validator(
|
||||||
))
|
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||||
->append(new Input\Types\Option(
|
// Make sure verbosity level never goes above 3
|
||||||
'd',
|
return min(3, (int)$context->find('v'));
|
||||||
'data',
|
}
|
||||||
Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED,
|
))
|
||||||
'Path to the input JSON data',
|
)
|
||||||
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
->append(
|
||||||
// Make sure -d (--data) is a valid file that can be read
|
Input\InputTypeFactory::build('LongOption')
|
||||||
$file = $context->getOption('d');
|
->name('data')
|
||||||
|
->short('d')
|
||||||
|
->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
||||||
|
->description('Path to the input JSON data')
|
||||||
|
->validator(new Input\Validator(
|
||||||
|
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||||
|
// Make sure -d (--data) is a valid file that can be read
|
||||||
|
$file = $context->find('data');
|
||||||
|
|
||||||
if (!is_readable($file)) {
|
if (!is_readable($file)) {
|
||||||
throw new \Exception('The file specified via option -d (--data) does not exist or is not readable.');
|
throw new \Exception('The file specified via option --data does not exist or is not readable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now make sure it is valid JSON
|
// Now make sure it is valid JSON
|
||||||
try {
|
try {
|
||||||
$json = json_decode(file_get_contents($file), false, 512, JSON_THROW_ON_ERROR);
|
$json = json_decode(file_get_contents($file), false, 512, JSON_THROW_ON_ERROR);
|
||||||
} catch (JsonException $ex) {
|
} catch (JsonException $ex) {
|
||||||
throw new \Exception(sprintf('The file specified via option -d (--data) does not appear to be a valid JSON ddocument. Returned: %s: %s', $ex->getCode(), $ex->getMessage()));
|
throw new \Exception(sprintf('The file specified via option --data does not appear to be a valid JSON ddocument. Returned: %s: %s', $ex->getCode(), $ex->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $json;
|
return $json;
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
// 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
|
||||||
$argv = Input\InputHandlerFactory::build('Argv', $collection);
|
try{
|
||||||
|
$argv = Input\InputHandlerFactory::build('Argv', $collection);
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
echo "Error when attempting to bind values to collection. Returned: " . $ex->getMessage() . PHP_EOL;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
// Display the manual in green text
|
// Display the manual in green text
|
||||||
echo Cli\manpage(
|
echo Cli\manpage(
|
||||||
|
|
@ -77,24 +90,33 @@ echo Cli\manpage(
|
||||||
// -d, --data=VALUE Path to the input JSON data
|
// -d, --data=VALUE Path to the input JSON data
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
// php -f example/example.php -- -vvv -d example/example.json import
|
// php -f example/example.php -- -vvvs -d example/example.json import
|
||||||
|
|
||||||
var_dump($argv->getArgument('action'));
|
try{
|
||||||
// string(6) "import"
|
|
||||||
|
|
||||||
var_dump($argv->getOption('v'));
|
var_dump($argv->find('action'));
|
||||||
//int(3)
|
// string(6) "import"
|
||||||
|
|
||||||
var_dump($argv->getOption('s'));
|
var_dump($argv->find('v'));
|
||||||
//bool(true)
|
//int(3)
|
||||||
|
|
||||||
var_dump($argv->getOption('d'));
|
var_dump($argv->find('s'));
|
||||||
// class stdClass#11 (1) {
|
//bool(true)
|
||||||
// public $fruit =>
|
|
||||||
// array(2) {
|
var_dump($argv->find('data'));
|
||||||
// [0] =>
|
// class stdClass#11 (1) {
|
||||||
// string(5) "apple"
|
// public $fruit =>
|
||||||
// [1] =>
|
// array(2) {
|
||||||
// string(6) "banana"
|
// [0] =>
|
||||||
// }
|
// string(5) "apple"
|
||||||
// }
|
// [1] =>
|
||||||
|
// string(6) "banana"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
var_dump($argv->find('nope-doesnt-exist'));
|
||||||
|
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
echo "Error: " . $ex->getMessage() . PHP_EOL;
|
||||||
|
}
|
||||||
|
//Error trying to access input. Returned: Input nope-doesnt-exist could not be found.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue