This example demonstrates the most basic CLI command creation using WebFiori CLI library.
- Creating a simple command class
- Adding optional arguments with default values
- Basic output formatting with emojis
- Help system integration
- Error handling
main.php- Application entry point and runner setupHelloCommand.php- The hello command implementation
php main.php
# or
php main.php helpOutput:
Usage:
command [arg1 arg2="val" arg3...]
Global Arguments:
--ansi:[Optional] Force the use of ANSI output.
Available Commands:
help: Display CLI Help. To display help for specific command, use the argument "--command" with this command.
hello: A simple greeting command that says hello to someone
php main.php help --command=helloOutput:
hello: A simple greeting command that says hello to someone
Supported Arguments:
--name:[Optional][Default = 'World'] The name to greet (default: World)
php main.php helloOutput:
Hello, World! 👋
Have a wonderful day!
php main.php hello --name=AhmedOutput:
Hello, Ahmed! 👋
Have a wonderful day!
php main.php hello --name="Fatima Al-Zahra"Output:
Hello, Fatima Al-Zahra! 👋
Have a wonderful day!
php main.php hello --name=Mohammed --ansiOutput:
Hello, Mohammed! 👋
Have a wonderful day!
php main.php invalidOutput:
Error: The command 'invalid' is not supported.
- Command Structure: Commands extend
WebFiori\Cli\Commandand implementexec()method - Arguments: Optional arguments defined in constructor with default values
- Output: Use
println()for formatted output with emoji support - Help Integration: Commands automatically integrate with help system
- Error Handling: Invalid commands show appropriate error messages
- Global Arguments:
--ansiflag works with all commands
class HelloCommand extends Command {
public function __construct() {
parent::__construct('hello', [
'--name' => [
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => 'World',
ArgumentOption::DESCRIPTION => 'The name to greet (default: World)'
]
], 'A simple greeting command that says hello to someone');
}
public function exec(): int {
$name = $this->getArgValue('--name');
$this->println("Hello, %s! 👋", $name);
$this->println("Have a wonderful day!");
return 0;
}
}This example serves as the foundation for understanding WebFiori CLI basics before moving to more advanced features.
- 02-arguments-and-options - Learn advanced argument handling and validation
- 03-user-input - Add interactive user input to your commands
- 04-output-formatting - Enhance output with colors and formatting
- 10-multi-command-app - Build complete CLI applications
- 12-command-scaffolding - Generate commands automatically
- 05-interactive-commands - Interactive command workflows
- 11-masked-input - Secure input handling