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

Updated example to use manpage() provided by the pointybeard/helpers-functions-cli package

This commit is contained in:
Alannah Kearney 2019-05-20 22:39:08 +10:00
commit 7c06febefe
2 changed files with 28 additions and 82 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "pointybeard/helpers-cli-input", "name": "pointybeard/helpers-cli-input",
"version": "1.0.0", "version": "1.0.1",
"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",
@ -20,7 +20,8 @@
"require-dev": { "require-dev": {
"phpunit/phpunit": "^8", "phpunit/phpunit": "^8",
"pointybeard/helpers-functions-strings": "~1.0", "pointybeard/helpers-functions-strings": "~1.0",
"pointybeard/helpers-cli-colour": "~1.0" "pointybeard/helpers-cli-colour": "~1.0",
"pointybeard/helpers-functions-cli": "~1.1"
}, },
"support": { "support": {
"issues": "https://github.com/pointybeard/helpers-cli-input/issues", "issues": "https://github.com/pointybeard/helpers-cli-input/issues",

View file

@ -4,9 +4,10 @@ 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\Flags; use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings; use pointybeard\Helpers\Functions\Strings;
use pointybeard\Helpers\Cli\Colour\Colour; 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())
@ -52,87 +53,31 @@ $collection = (new Input\InputCollection())
// and validate the input according to our collection // and validate the input according to our collection
$argv = Input\InputHandlerFactory::build('Argv', $collection); $argv = Input\InputHandlerFactory::build('Argv', $collection);
// Example of using an input collection to generate a usage string
function usage(Input\InputCollection $collection): string {
$arguments = [];
foreach ($collection->getArguments() as $a) {
$arguments[] = strtoupper(
// Wrap with square brackets if it's not required
Flags\is_flag_set(Input\AbstractInputType::FLAG_OPTIONAL, $a->flags()) ||
!Flags\is_flag_set(Input\AbstractInputType::FLAG_REQUIRED, $a->flags())
? "[{$a->name()}]"
: $a->name()
);
}
$arguments = trim(implode($arguments, ' '));
return sprintf(
"Usage: php -f example.php -- [OPTIONS]... %s%s",
$arguments,
strlen($arguments) > 0 ? '...' : ''
);
}
// Example of using an input collection to generate a manual page
function manpage(Input\InputCollection $collection) : string {
$arguments = $options = [];
foreach ($collection->getArguments() as $a) {
$arguments[] = (string) $a;
}
foreach ($collection->getOptions() as $o) {
$options[] = (string) $o;
}
$arguments = implode($arguments, PHP_EOL.' ');
$options = implode($options, PHP_EOL.' ');
return sprintf('%s 1.0.0, %s
%s
Mandatory values for long options are mandatory for short options too.
Arguments:
%s
Options:
%s
Examples:
php -f example/example.php -- -vvv -d example/example.json import
',
basename(__FILE__),
Strings\utf8_wordwrap(
"An example script for the PHP Helpers: Command-line Input and Input Type Handlers composer library (pointybeard/helpers-cli-input)."
),
usage($collection),
$arguments,
$options
);
}
// Display the manual in green text // Display the manual in green text
echo Colour::colourise(manpage($collection), Colour::FG_GREEN) . PHP_EOL . PHP_EOL; echo Colour::colourise(Cli\manpage(
basename(__FILE__),
'1.0.1',
'An example script for the PHP Helpers: Command-line Input and Input Type Handlers composer library (pointybeard/helpers-cli-input).',
'php -f example/example.php -- -vvv -d example/example.json import',
$collection
), Colour::FG_GREEN) . PHP_EOL . PHP_EOL;
/* // example.php 1.0.0, An example script for the PHP Helpers: Command-line Input and Input Type Handlers
example.php 1.0.0, An example script for the PHP Helpers: Command-line Input and Input Type Handlers // composer library (pointybeard/helpers-cli-input).
composer library (pointybeard/helpers-cli-input). // Usage: example.php [OPTIONS]... ACTION...
Usage: php -f example.php -- [OPTIONS]... ACTION... //
// Mandatory values for long options are mandatory for short options too.
Mandatory values for long options are mandatory for short options too. //
// Arguments:
Arguments: // ACTION The name of the action to perform
ACTION The name of the action to perform //
// Options:
Options: // -v verbosity level. -v (errors only), -vv (warnings
-v verbosity level. -v (errors only), -vv (warnings // and errors), -vvv (everything).
and errors), -vvv (everything). // -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 -- -vvv -d example/example.json import
*/
var_dump($argv->getArgument('action')); var_dump($argv->getArgument('action'));
// string(6) "import" // string(6) "import"