-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloCommand.php
More file actions
69 lines (59 loc) · 2 KB
/
HelloCommand.php
File metadata and controls
69 lines (59 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
use WebFiori\Cli\Command;
use WebFiori\Cli\ArgumentOption;
/**
* A simple hello world command that demonstrates basic CLI functionality.
*
* This command shows how to:
* - Create a basic command class
* - Handle optional arguments with defaults
* - Use different output methods
* - Return appropriate exit codes
*/
class HelloCommand extends Command {
public function __construct() {
parent::__construct('hello', [
'--name' => [
ArgumentOption::DESCRIPTION => 'The name to greet (default: World)',
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => 'World'
]
], 'A simple greeting command that says hello to someone');
}
/**
* Execute the hello command.
*
* This method demonstrates:
* - Getting argument values
* - Basic output formatting
* - Conditional logic
* - Proper return codes
*/
public function exec(): int {
// Get the name argument, with fallback to default
$name = $this->getArgValue('--name') ?? 'World';
// Trim whitespace and validate
$name = trim($name);
if (empty($name)) {
$this->error('Name cannot be empty!');
return 1; // Error exit code
}
// Special greeting for WebFiori
if (strtolower($name) === 'webfiori') {
$this->success("🎉 Hello, $name! Welcome to the CLI world!");
$this->info('You\'re using the WebFiori CLI library.');
} else {
// Standard greeting
$this->println("Hello, $name! 👋");
// Add some personality based on name length
if (strlen($name) > 10) {
$this->info('Wow, that\'s quite a long name!');
} elseif (strlen($name) <= 2) {
$this->info('Short and sweet!');
}
}
// Success message
$this->println('Have a wonderful day!');
return 0; // Success exit code
}
}