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
|
|
@ -9,47 +9,60 @@ use pointybeard\Helpers\Functions\Cli;
|
|||
|
||||
// Define what we are expecting to get from the command line
|
||||
$collection = (new Input\InputCollection())
|
||||
->append(new Input\Types\Argument(
|
||||
'action',
|
||||
Input\AbstractInputType::FLAG_REQUIRED,
|
||||
'The name of the action to perform'
|
||||
))
|
||||
->append(new Input\Types\Option(
|
||||
'v',
|
||||
null,
|
||||
Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_TYPE_INCREMENTING,
|
||||
'verbosity level. -v (errors only), -vv (warnings and errors), -vvv (everything).',
|
||||
null,
|
||||
0
|
||||
))
|
||||
->append(new Input\Types\Option(
|
||||
'd',
|
||||
'data',
|
||||
Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED,
|
||||
'Path to the input JSON data',
|
||||
function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) {
|
||||
// Make sure -d (--data) is a valid file that can be read
|
||||
$file = $context->getOption('d');
|
||||
->append(
|
||||
Input\InputTypeFactory::build('Argument')
|
||||
->name('action')
|
||||
->flags(Input\AbstractInputType::FLAG_REQUIRED)
|
||||
->description('The name of the action to perform')
|
||||
)
|
||||
->append(
|
||||
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
|
||||
return min(3, (int)$context->find('v'));
|
||||
}
|
||||
))
|
||||
)
|
||||
->append(
|
||||
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 -d (--data) does not exist or is not readable.');
|
||||
}
|
||||
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 -d (--data) does not appear to be a valid JSON ddocument. Returned: %s: %s', $ex->getCode(), $ex->getMessage()));
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
))
|
||||
return $json;
|
||||
}
|
||||
))
|
||||
)
|
||||
;
|
||||
|
||||
// Get the supplied input. Passing the collection will make the handler bind values
|
||||
// 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
|
||||
echo Cli\manpage(
|
||||
|
|
@ -77,24 +90,33 @@ echo Cli\manpage(
|
|||
// -d, --data=VALUE Path to the input JSON data
|
||||
//
|
||||
// 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'));
|
||||
// string(6) "import"
|
||||
try{
|
||||
|
||||
var_dump($argv->getOption('v'));
|
||||
//int(3)
|
||||
var_dump($argv->find('action'));
|
||||
// string(6) "import"
|
||||
|
||||
var_dump($argv->getOption('s'));
|
||||
//bool(true)
|
||||
var_dump($argv->find('v'));
|
||||
//int(3)
|
||||
|
||||
var_dump($argv->getOption('d'));
|
||||
// class stdClass#11 (1) {
|
||||
// public $fruit =>
|
||||
// array(2) {
|
||||
// [0] =>
|
||||
// string(5) "apple"
|
||||
// [1] =>
|
||||
// string(6) "banana"
|
||||
// }
|
||||
// }
|
||||
var_dump($argv->find('s'));
|
||||
//bool(true)
|
||||
|
||||
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'));
|
||||
|
||||
} 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