11
0
Fork 0
mirror of https://github.com/n3w/helpers-cli-input.git synced 2025-12-19 12:43:23 +00:00
helpers-cli-input/example/example.php

113 lines
4 KiB
PHP
Raw Normal View History

2019-05-20 15:08:41 +10:00
<?php
declare(strict_types=1);
include __DIR__.'/../vendor/autoload.php';
use pointybeard\Helpers\Cli\Input;
use pointybeard\Helpers\Cli\Colour\Colour;
use pointybeard\Helpers\Functions\Cli;
2019-05-20 15:08:41 +10:00
// Define what we are expecting to get from the command line
$collection = (new Input\InputCollection())
2019-06-01 22:49:10 +10:00
->add(
Input\InputTypeFactory::build('Argument')
->name('action')
->flags(Input\AbstractInputType::FLAG_REQUIRED)
->description('The name of the action to perform')
)
2019-06-01 22:49:10 +10:00
->add(
Input\InputTypeFactory::build('IncrementingFlag')
->name('v')
->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_TYPE_INCREMENTING)
->description('verbosity level. -v (errors only), -vv (warnings and errors), -vvv (everything).')
->validator(new Input\Validator(
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
// Make sure verbosity level never goes above 3
2019-05-27 00:22:25 +10:00
return min(3, (int) $context->find('v'));
}
))
)
2019-06-01 22:49:10 +10:00
->add(
Input\InputTypeFactory::build('LongOption')
->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)) {
throw new \Exception('The file specified via option --data does not exist or is not readable.');
}
// Now make sure it is valid JSON
try {
$json = json_decode(file_get_contents($file), false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $ex) {
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;
}
))
)
2019-05-20 15:08:41 +10:00
;
// Get the supplied input. Passing the collection will make the handler bind values
// and validate the input according to our collection
2019-05-27 00:22:25 +10:00
try {
$argv = Input\InputHandlerFactory::build('Argv', $collection);
2019-05-27 00:22:25 +10:00
} catch (\Exception $ex) {
echo 'Error when attempting to bind values to collection. Returned: '.$ex->getMessage().PHP_EOL;
exit;
}
2019-05-20 15:08:41 +10:00
// 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,
[
2019-05-27 00:22:25 +10:00
'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:
2019-06-01 22:49:10 +10:00
// php -f example/example.php -- -vvv -d example/example.json import
var_dump($argv->find('action'));
// string(6) "import"
var_dump($argv->find('v'));
//int(3)
var_dump($argv->find('data'));
// class stdClass#11 (1) {
// public $fruit =>
// array(2) {
// [0] =>
// string(5) "apple"
// [1] =>
// string(6) "banana"
// }
// }
var_dump($argv->find('nope-doesnt-exist'));
// NULL