-
Notifications
You must be signed in to change notification settings - Fork 0
/
example
executable file
·107 lines (95 loc) · 2.7 KB
/
example
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env php
<?php
declare(strict_types=1);//optional but good practice IMO. Google it.
use BHayes\CLI\CLI;
use BHayes\CLI\Colour;
use BHayes\CLI\UserErrorResponse;
use BHayes\CLI\UserResponse;
use BHayes\CLI\UserSuccessResponse;
require_once 'vendor/autoload.php'; //if installed via composer
/**
* This is the documentation that will appear when you type --help.
*/
class Example
{
/**
* This one is easy to run.
* try runMe with --help to see this text.
*/
public function runMe(string $optional = null)
{
//either return output or just output directly its up to you.
echo "I work with no arguments.";
if ($optional !== null) {
echo " But thanks for providing me with: ";
var_dump($optional);
}
}
/**
* This command will only run when all the requirements are met.
*/
public function tryMe(bool $bool, string $string, float $float, int $int)
{
return "You did it! You gave me bool a string, a float and an int.";
}
/**
* This method will accept any number of string arguments while the
* the others will fail if you pass them too many arguments.
*
* @param string ...$bunchOfStrings
*/
public function variadic(string ...$bunchOfStrings)
{
echo "You said ";
if (empty($bunchOfStrings)) {
echo "nothing.";
}
print_r($bunchOfStrings);
echo "\n";
}
/**
* Demos prompts.
*
* @throws UserResponse
*/
public function survey():string
{
if (! CLI::confirm('Shall we begin?')) {
return "Cancelled";
}
$colour = CLI::prompt('Whats your favorite colour?');
$colourCode = Colour::code($colour);
throw new UserResponse("I love $colour too!", $colourCode, '☺');
}
/**
* Tests the UserResponse throwable.
*
* @param bool|null $success
* @throws UserResponse
*/
public function throwsUserResponse(bool $success = null)
{
if ($success === true) {
throw new UserSuccessResponse();//all params optional
}
if ($success === false) {
throw new UserErrorResponse('Some error message user needs to see!');
}
throw new UserResponse('Try this again with true or false.');
}
/**
* foo is now an option because it has been declared public.
* @var bool
*/
public $foo = false;
/**
* Run me with and without `--foo` and see the result.
*/
public function bar()
{
if (!$this->foo) return "Try running this again with the --foo option.";
return $this;
}
};
$cli = new CLI(new Example());
$cli->run();