Skip to content

Commit a0596a5

Browse files
committed
style: format codes by cs fix
1 parent 5e438e4 commit a0596a5

25 files changed

+85
-69
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$header = <<<'EOF'
44
This file is part of toolkit/cli-utils.
55
6-
@link https://github.com/php-toolkit/cli-utils
6+
@homepage https://github.com/php-toolkit/cli-utils
77
@author https://github.com/inhere
88
@license MIT
99
EOF;

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
4848

4949
![colors](example/cli-php-file-highlight.png)
5050

51-
## Parse CLI arguments and options
51+
## Parse CLI arguments & options
5252

5353
```php
5454
use Toolkit\Cli\Flags;
@@ -57,7 +57,7 @@ $argv = $_SERVER['argv'];
5757
// notice: must shift first element.
5858
$script = \array_shift($argv);
5959
// do parse
60-
list($args, $shortOpts, $longOpts) = Flags::parseArgv($argv);
60+
[$args, $shortOpts, $longOpts] = Flags::parseArgv($argv);
6161
```
6262

6363
## Build CLI application
@@ -87,7 +87,7 @@ $app->addCommand('test', function ($app) {
8787
}, 'the description text for the command: test');
8888
```
8989

90-
Use closure with config:
90+
Use closure with a config:
9191

9292
```php
9393
$app->addByConfig(function ($app) {
@@ -105,6 +105,8 @@ $app->addByConfig(function ($app) {
105105
Use an object:
106106

107107
```php
108+
use Toolkit\Cli\App;
109+
108110
class MyCommand
109111
{
110112
public function getHelpConfig(): array
@@ -158,7 +160,7 @@ $down = Download::file($url, '');
158160
$down->start();
159161
```
160162

161-
### Progress bar output:
163+
**Progress bar output:**
162164

163165
```text
164166
Connected...
@@ -170,7 +172,7 @@ Mime-type: application/octet-stream
170172
[========================================> ] 40% (3076/7590 kb)
171173
```
172174

173-
### Progress text output:
175+
**Progress text output:**
174176

175177
```text
176178
Download: http://no2.php.net/distributions/php-7.2.5.tar.bz2

example/color.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/

example/down.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/

example/high.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/

src/App.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/
@@ -100,7 +100,7 @@ class App
100100
* Class constructor.
101101
*
102102
* @param array $config
103-
* @param array $argv
103+
* @param array|null $argv
104104
*/
105105
public function __construct(array $config = [], array $argv = null)
106106
{
@@ -148,7 +148,6 @@ protected function findCommand(): void
148148
}
149149

150150
$newArgs = [];
151-
152151
foreach ($this->args as $key => $value) {
153152
if ($key === 0) {
154153
$this->command = trim($value);
@@ -168,20 +167,32 @@ protected function findCommand(): void
168167
* @throws InvalidArgumentException
169168
*/
170169
public function dispatch(bool $exit = true): void
170+
{
171+
$status = $this->doHandle();
172+
173+
if ($exit) {
174+
$this->stop($status);
175+
}
176+
}
177+
178+
/**
179+
* @return int
180+
*/
181+
protected function doHandle(): int
171182
{
172183
if (!$command = $this->command) {
173184
$this->displayHelp();
174-
return;
185+
return 0;
175186
}
176187

177188
if (!isset($this->commands[$command])) {
178189
$this->displayHelp("The command '{$command}' is not exists!");
179-
return;
190+
return 0;
180191
}
181192

182193
if (isset($this->opts['h']) || isset($this->opts['help'])) {
183194
$this->displayCommandHelp($command);
184-
return;
195+
return 0;
185196
}
186197

187198
try {
@@ -190,9 +201,7 @@ public function dispatch(bool $exit = true): void
190201
$status = $this->handleException($e);
191202
}
192203

193-
if ($exit) {
194-
$this->stop($status);
195-
}
204+
return $status;
196205
}
197206

198207
/**
@@ -360,7 +369,7 @@ public function commands(array $commands): void
360369
public function displayHelp(string $err = ''): void
361370
{
362371
if ($err) {
363-
echo Color::render("<red>ERROR</red>: $err\n\n");
372+
Color::println("<red>ERROR</red>: $err\n");
364373
}
365374

366375
// help
@@ -383,8 +392,7 @@ public function displayHelp(string $err = ''): void
383392

384393
$help .= "\nFor command usage please run: <cyan>{$this->script} COMMAND -h</cyan>";
385394

386-
echo Color::render($help) . PHP_EOL;
387-
exit(0);
395+
Color::println($help);
388396
}
389397

390398
/**
@@ -427,7 +435,7 @@ public function displayCommandHelp(string $name): void
427435
]);
428436
}
429437

430-
echo Color::render($help);
438+
Color::println($help);
431439
}
432440

433441
/**

src/Cli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/
@@ -63,7 +63,7 @@ public static function style(): Style
6363

6464
/**
6565
* @param string $text
66-
* @param string|int|array $style
66+
* @param string|int|array|null $style
6767
*
6868
* @return string
6969
*/

src/Color.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/
@@ -18,6 +18,7 @@
1818
use function sprintf;
1919
use function strip_tags;
2020
use function strpos;
21+
use const PHP_EOL;
2122

2223
/**
2324
* Class Color
@@ -299,7 +300,7 @@ public static function println($messages, string $style = 'info'): void
299300
{
300301
$string = is_array($messages) ? implode("\n", $messages) : (string)$messages;
301302

302-
echo self::render($string . "\n", $style);
303+
echo self::render($string, $style) . PHP_EOL;
303304
}
304305

305306
/**
@@ -344,11 +345,11 @@ public static function render(string $text, $style = null): string
344345
if (is_string($style)) {
345346
$color = self::STYLES[$style] ?? '';
346347

347-
// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
348+
// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
348349
} elseif (is_array($style)) {
349350
$color = implode(';', $style);
350351

351-
// user color tag: <info>message</info>
352+
// user color tag: <info>message</info>
352353
} elseif (strpos($text, '</') > 0) {
353354
return self::parseTag($text);
354355
}

src/ColorCode.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/
@@ -172,7 +172,6 @@ public static function fromString(string $string)
172172
break;
173173
default:
174174
throw new RuntimeException('Invalid option');
175-
break;
176175
}
177176
}
178177

src/ColorTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is part of toolkit/cli-utils.
44
*
5-
* @link https://github.com/inhere
5+
* @link https://github.com/php-toolkit/cli-utils
66
* @author https://github.com/inhere
77
* @license MIT
88
*/

0 commit comments

Comments
 (0)