-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.php
executable file
·117 lines (98 loc) · 3.25 KB
/
example.php
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
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/php
<?php
include "cli.php";
class Example extends CLI{
function __construct($appname = null, $author = null, $copyright = null) {
parent::__construct('CLI Framework Example', 'Author Name', '(c) 2012 Etopian Inc.');
}
/**
* You are responsible for defining variables (class attributes) which will be used by
* $this->main() to do its job.
*
* You can use arguments, flags, and options functions (methods) explained later
* to define these variables (class attributes).
* First they are set using args, flags, and options functions, and then main() is called.
*/
private $optionvar1 = 'example opt var';
private $flagvar2 = 'bar';
private $argumentvar = 'argvar';
/**
* The main() function gets called if at least one argument is present.
* If no arguments are present, the automatically generated help is displayed.
*
* The main functions job to do the main work of the script.
*
*/
public function main(){
//how to use the $this->getInput() function to get specific input from the user.
$input = '';
while($input != 'yes'){
$input = $this->getInput('Type yes to continue. Type no to exit.');
if($input == 'no'){
exit();
}
}
//you may color the output like so.
print $this->colorText("flagvar2: ".$this->flagvar2."\n", "YELLOW");
print $this->optionvar1."\n";
print $this->argumentvar."\n";
}
/**
* Now we define flags, arguments and options.
* Notice each one of the defintions for these functions must be public.
*
* The basic naming convention is:
* public function flag_NAME
* public function option_NAME
* public function argument_NAME
*
* In the function we define the help using:
* if($opt == 'help'){ return 'help message'. }
*
* We then follow up code to process that argument, flag, or option.
*
* There are no return values expected from any of the following functions.
*
*/
/**
* Define the flag -e, so if you run './example -e' this function will be called
* Flags do not handle values, to handle values ($opt) use option_ for that.
*/
public function flag_e($opt = null){
if($opt == 'help'){
return 'Help for the flag -e';
}
print "\n".'flag_e was just called and $opt was: '.$opt."\n";
$this->flagvar2 = 'flag var is now set';
}
/**
* Argument is like flag, but just a string.
* ./example.php example
*/
public function argument_example($opt = null){
if($opt == 'help'){
return 'Help for the argument \'example\'';
}
print "\n".'argument_example was just called and $opt was: '.$opt."\n";
$this->argumentvar = 'example';
}
/**
* ./example.php --example=test
*
* Will output $opt = test when this function is called.
*
*/
public function option_example($opt = null){
if($opt == 'help'){
return 'Help for the option --example=value';
}
print "\n".'option_example was just called and $opt was: '.$opt."\n";
$this->optionvar1 = $opt;
//you can also call $this->getInput('message');
//from within these functions to get input associated with the given option.
}
}
/**
*IMPORTANT, instantiate your class! i.e. new Classname();
*/
new Example();