@@ -33,14 +33,28 @@ Color::println('hello world', 'success');
33
33
echo Color::render('hello world', 'success');
34
34
```
35
35
36
- ![ colors] ( example/terminal-color.png )
36
+ ![ colors] ( example/images/color-styles.png )
37
+
38
+ ## Console log
39
+
40
+ ``` php
41
+ use Toolkit\Cli\Util\Clog;
42
+
43
+ // run: php example/log.php
44
+ foreach (Clog::getLevelNames() as $level) {
45
+ Clog::log($level, "example log $level message");
46
+ }
47
+
48
+ ```
49
+
50
+ ![ clog-example] ( example/images/clog-example.png )
37
51
38
52
## Terminal control
39
53
40
54
examples:
41
55
42
56
``` php
43
- use \ Toolkit\Cli\Util\Terminal;
57
+ use Toolkit\Cli\Util\Terminal;
44
58
45
59
Terminal::forward(3);
46
60
Terminal::backward(2);
@@ -93,106 +107,6 @@ $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
93
107
94
108
![ colors] ( example/cli-php-file-highlight.png )
95
109
96
- ## Parse CLI arguments & options
97
-
98
- ``` php
99
- use Toolkit\Cli\Flags;
100
-
101
- $argv = $_SERVER['argv'];
102
- // notice: must shift first element.
103
- $script = \array_shift($argv);
104
- // do parse
105
- [$args, $shortOpts, $longOpts] = Flags::parseArgv($argv);
106
- ```
107
-
108
- ## Build CLI application
109
-
110
- You can quickly build an simple CLI application:
111
-
112
- ``` php
113
- use Toolkit\Cli\App;
114
-
115
- // create app instance
116
- $app = new App([
117
- 'desc' => 'this is my cli application',
118
- ]);
119
- ```
120
-
121
- ### Register commands
122
-
123
- Use closure:
124
-
125
- ``` php
126
- $app->addCommand('test', function ($app) {
127
- echo "args:\n";
128
- /** @var Toolkit\Cli\App $app */
129
- /** @noinspection ForgottenDebugOutputInspection */
130
- print_r($app->getArgs());
131
-
132
- }, 'the description text for the command: test');
133
- ```
134
-
135
- Use closure with a config:
136
-
137
- ``` php
138
- $app->addByConfig(function ($app) {
139
- echo "args:\n";
140
- /** @var Toolkit\Cli\App $app */
141
- /** @noinspection ForgottenDebugOutputInspection */
142
- print_r($app->getArgs());
143
-
144
- }, [
145
- 'name' => 'cmd2',
146
- 'desc' => 'the description text for the command: test',
147
- ]);
148
- ```
149
-
150
- Use an object:
151
-
152
- ``` php
153
- use Toolkit\Cli\App;
154
-
155
- class MyCommand
156
- {
157
- public function getHelpConfig(): array
158
- {
159
- $help = <<<STR
160
- Options:
161
- --info Output some information
162
-
163
- Example:
164
- {{fullCmd}}
165
-
166
- STR;
167
-
168
- return [
169
- ' name' => 'list',
170
- 'desc' => 'list all directory name in src/',
171
- 'help' => $help,
172
- ];
173
- }
174
-
175
- public function __invoke(App $app)
176
- {
177
- echo "hello\n";
178
- }
179
- }
180
-
181
- // add command
182
- $app->addObject(new MyCommand);
183
- ```
184
-
185
- ### Run application
186
-
187
- ``` php
188
- // run
189
- $app->run();
190
- ```
191
-
192
- Run demo: ` php example/liteApp `
193
-
194
- ![ cli-app] ( example/cli-app.png )
195
-
196
110
## CLI downloader
197
111
198
112
``` php
@@ -217,6 +131,7 @@ $down->start();
217
131
## Projects
218
132
219
133
- https://github.com/inhere/php-console Build rich console application
134
+ - https://github.com/php-toolkit/pflag Generic flags parse library, build simple console application.
220
135
221
136
## Refer
222
137
0 commit comments