Skip to content

Commit 70a75bc

Browse files
committed
feat: add simple console logger
1 parent 2aa3b54 commit 70a75bc

File tree

8 files changed

+376
-110
lines changed

8 files changed

+376
-110
lines changed

Diff for: README.md

+17-102
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,28 @@ Color::println('hello world', 'success');
3333
echo Color::render('hello world', 'success');
3434
```
3535

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)
3751

3852
## Terminal control
3953

4054
examples:
4155

4256
```php
43-
use \Toolkit\Cli\Util\Terminal;
57+
use Toolkit\Cli\Util\Terminal;
4458

4559
Terminal::forward(3);
4660
Terminal::backward(2);
@@ -93,106 +107,6 @@ $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
93107

94108
![colors](example/cli-php-file-highlight.png)
95109

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-
196110
## CLI downloader
197111

198112
```php
@@ -217,6 +131,7 @@ $down->start();
217131
## Projects
218132

219133
- 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.
220135

221136
## Refer
222137

Diff for: example/down.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@
77
* @license MIT
88
*/
99

10-
use Toolkit\Cli\App;
11-
use Toolkit\Cli\Download;
10+
use Toolkit\Cli\Util\Download;
1211

1312
require dirname(__DIR__) . '/test/bootstrap.php';
1413

15-
$app = new App();
1614
$url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2';
1715
$down = Download::create($url);
1816

19-
$type = $app->getOpt('type', 'text');
20-
21-
if ($type === 'bar') {
22-
$down->setShowType($type);
23-
}
17+
$type = 'bar';
2418

19+
$down->setShowType($type);
2520
$down->start();

Diff for: example/images/clog-example.png

85.3 KB
Loading

Diff for: example/images/color-styles.png

199 KB
Loading

Diff for: example/images/down-file-bar.jpg

70.2 KB
Loading

Diff for: example/log.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/php-toolkit/cli-utils
6+
* @author https://github.com/inhere
7+
* @license MIT
8+
*/
9+
10+
use Toolkit\Cli\Util\Clog;
11+
12+
require dirname(__DIR__) . '/test/bootstrap.php';
13+
14+
// run: php example/log.php
15+
foreach (Clog::getLevelNames() as $level) {
16+
Clog::log($level, "example log $level message");
17+
}

0 commit comments

Comments
 (0)