1. Go to this page and download the library: Download grano22/simplecli library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
grano22 / simplecli example snippets
use Grano22\SimpleCli\App\Factory\SimpleCliUniversalFilterConstraint;
use Grano22\SimpleCli\App\SimpleCliAppFactory;
use Grano22\SimpleCli\Command\Input\SimpleCliArgument;
use Grano22\SimpleCli\Command\Input\SimpleCliCommandInput;
use Grano22\SimpleCli\Command\Input\SimpleCliOption;
$myAwesomeCliApp = SimpleCliAppFactory::create()
// Define help independent option for all commands
->withCommandsOption(
commandNames: SimpleCliUniversalFilterConstraint::wildcard('*'),
name: 'help',
options: SimpleCliOption::OPTIONAL | SimpleCliOption::IGNORE_REST_REQUIRED | SimpleCliOption::NEGABLE
)
// Add your custom unknown error handler
->setUnexpectedErrorHandler(static function(Exception $exception) {
// Do something with your custom exception
echo $exception->getMessage();
exit(1);
})
->withCommandsArguments(
commandNames: ['greetings'],
name: 'tone',
options: SimpleCliArgument::REQUIRED
)
->withCommandOption(
commandName: ['greetings', 'info'],
name: 'verbose',
options: SimpleCliOption::OPTIONAL
)
// Define your command logic :)
->withCommand(
name: 'greetings',
executionLogic: static function(SimpleCliCommandInput $input): int {
$message = "Hello world";
$tone = $input->getArguments()->getByName('tone')->getValue();
if ($tone = 'high') {
$message .= '!';
}
echo $message;
return 0;
}
)
->build()
;
$myAwesomeCliApp->autoExecuteCommand();
$myAwesomeCliApp = SimpleCliAppFactory::create()
// Define your event listener for one or multiple events
->addEventsListener(static function(CommandNotFoundEvent|MissingArgumentEvent $event) {
echo "Missing argument or command";
exit(1);
}, [CommandNotFoundEvent::class, MissingArgumentEvent::class])
;