Skip to content

Commit 123d1bd

Browse files
authored
Merge pull request #26 from adhocore/develop
refactor: ide support, internal improvement
2 parents 61cb8fd + 3ec5bc5 commit 123d1bd

File tree

10 files changed

+355
-37
lines changed

10 files changed

+355
-37
lines changed

src/Helper/OutputHelper.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Ahc\Cli\Helper;
44

55
use Ahc\Cli\Input\Argument;
6+
use Ahc\Cli\Input\Command;
67
use Ahc\Cli\Input\Option;
7-
use Ahc\Cli\Input\Parser as Command;
8+
use Ahc\Cli\Input\Parameter;
89
use Ahc\Cli\Output\Writer;
910

1011
/**
@@ -27,6 +28,8 @@ public function __construct(Writer $writer = null)
2728

2829
/**
2930
* @param Argument[] $arguments
31+
* @param string $header
32+
* @param string $footer
3033
*
3134
* @return self
3235
*/
@@ -39,6 +42,8 @@ public function showArgumentsHelp(array $arguments, string $header = '', string
3942

4043
/**
4144
* @param Option[] $options
45+
* @param string $header
46+
* @param string $footer
4247
*
4348
* @return self
4449
*/
@@ -50,7 +55,9 @@ public function showOptionsHelp(array $options, string $header = '', string $foo
5055
}
5156

5257
/**
53-
* @param Command[] $options
58+
* @param Command[] $commands
59+
* @param string $header
60+
* @param string $footer
5461
*
5562
* @return self
5663
*/
@@ -63,6 +70,14 @@ public function showCommandsHelp(array $commands, string $header = '', string $f
6370

6471
/**
6572
* Show help with headers and footers.
73+
*
74+
* @param string $for
75+
* @param array $items
76+
* @param int $space
77+
* @param string $header
78+
* @param string $footer
79+
*
80+
* @return void
6681
*/
6782
protected function showHelp(string $for, array $items, int $space, string $header = '', string $footer = '')
6883
{
@@ -80,7 +95,8 @@ protected function showHelp(string $for, array $items, int $space, string $heade
8095

8196
foreach ($this->sortItems($items, $padLen) as $item) {
8297
$name = $this->getName($item);
83-
$this->writer->bold(' ' . \str_pad($name, $padLen + $space))->comment($item->desc(), true);
98+
$this->writer->bold(' ' . \str_pad($name, $padLen + $space));
99+
$this->writer->comment($item->desc(), true);
84100
}
85101

86102
if ($footer) {
@@ -90,13 +106,20 @@ protected function showHelp(string $for, array $items, int $space, string $heade
90106

91107
/**
92108
* Sort items by name. As a side effect sets max length of all names.
109+
*
110+
* @param Parameter[]|Command[] $items
111+
* @param int $max
112+
*
113+
* @return array
93114
*/
94115
protected function sortItems(array $items, &$max = 0): array
95116
{
96117
$first = reset($items);
97118
$max = \strlen($first->name());
98119

99120
\uasort($items, function ($a, $b) use (&$max) {
121+
/** @var Parameter $b */
122+
/** @var Parameter $a */
100123
$max = \max(\strlen($a->name()), \strlen($b->name()), $max);
101124

102125
return $a->name() <=> $b->name();

src/IO/Interactor.php

Lines changed: 142 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,144 @@
1212
* @license MIT
1313
*
1414
* @link https://github.com/adhocore/cli
15+
*
16+
* @method Writer bgBlack($text, $eol = false)
17+
* @method Writer bgBlue($text, $eol = false)
18+
* @method Writer bgCyan($text, $eol = false)
19+
* @method Writer bgGreen($text, $eol = false)
20+
* @method Writer bgPurple($text, $eol = false)
21+
* @method Writer bgRed($text, $eol = false)
22+
* @method Writer bgWhite($text, $eol = false)
23+
* @method Writer bgYellow($text, $eol = false)
24+
* @method Writer black($text, $eol = false)
25+
* @method Writer blackBgBlue($text, $eol = false)
26+
* @method Writer blackBgCyan($text, $eol = false)
27+
* @method Writer blackBgGreen($text, $eol = false)
28+
* @method Writer blackBgPurple($text, $eol = false)
29+
* @method Writer blackBgRed($text, $eol = false)
30+
* @method Writer blackBgWhite($text, $eol = false)
31+
* @method Writer blackBgYellow($text, $eol = false)
32+
* @method Writer blue($text, $eol = false)
33+
* @method Writer blueBgBlack($text, $eol = false)
34+
* @method Writer blueBgCyan($text, $eol = false)
35+
* @method Writer blueBgGreen($text, $eol = false)
36+
* @method Writer blueBgPurple($text, $eol = false)
37+
* @method Writer blueBgRed($text, $eol = false)
38+
* @method Writer blueBgWhite($text, $eol = false)
39+
* @method Writer blueBgYellow($text, $eol = false)
40+
* @method Writer bold($text, $eol = false)
41+
* @method Writer boldBlack($text, $eol = false)
42+
* @method Writer boldBlackBgBlue($text, $eol = false)
43+
* @method Writer boldBlackBgCyan($text, $eol = false)
44+
* @method Writer boldBlackBgGreen($text, $eol = false)
45+
* @method Writer boldBlackBgPurple($text, $eol = false)
46+
* @method Writer boldBlackBgRed($text, $eol = false)
47+
* @method Writer boldBlackBgWhite($text, $eol = false)
48+
* @method Writer boldBlackBgYellow($text, $eol = false)
49+
* @method Writer boldBlue($text, $eol = false)
50+
* @method Writer boldBlueBgBlack($text, $eol = false)
51+
* @method Writer boldBlueBgCyan($text, $eol = false)
52+
* @method Writer boldBlueBgGreen($text, $eol = false)
53+
* @method Writer boldBlueBgPurple($text, $eol = false)
54+
* @method Writer boldBlueBgRed($text, $eol = false)
55+
* @method Writer boldBlueBgWhite($text, $eol = false)
56+
* @method Writer boldBlueBgYellow($text, $eol = false)
57+
* @method Writer boldCyan($text, $eol = false)
58+
* @method Writer boldCyanBgBlack($text, $eol = false)
59+
* @method Writer boldCyanBgBlue($text, $eol = false)
60+
* @method Writer boldCyanBgGreen($text, $eol = false)
61+
* @method Writer boldCyanBgPurple($text, $eol = false)
62+
* @method Writer boldCyanBgRed($text, $eol = false)
63+
* @method Writer boldCyanBgWhite($text, $eol = false)
64+
* @method Writer boldCyanBgYellow($text, $eol = false)
65+
* @method Writer boldGreen($text, $eol = false)
66+
* @method Writer boldGreenBgBlack($text, $eol = false)
67+
* @method Writer boldGreenBgBlue($text, $eol = false)
68+
* @method Writer boldGreenBgCyan($text, $eol = false)
69+
* @method Writer boldGreenBgPurple($text, $eol = false)
70+
* @method Writer boldGreenBgRed($text, $eol = false)
71+
* @method Writer boldGreenBgWhite($text, $eol = false)
72+
* @method Writer boldGreenBgYellow($text, $eol = false)
73+
* @method Writer boldPurple($text, $eol = false)
74+
* @method Writer boldPurpleBgBlack($text, $eol = false)
75+
* @method Writer boldPurpleBgBlue($text, $eol = false)
76+
* @method Writer boldPurpleBgCyan($text, $eol = false)
77+
* @method Writer boldPurpleBgGreen($text, $eol = false)
78+
* @method Writer boldPurpleBgRed($text, $eol = false)
79+
* @method Writer boldPurpleBgWhite($text, $eol = false)
80+
* @method Writer boldPurpleBgYellow($text, $eol = false)
81+
* @method Writer boldRed($text, $eol = false)
82+
* @method Writer boldRedBgBlack($text, $eol = false)
83+
* @method Writer boldRedBgBlue($text, $eol = false)
84+
* @method Writer boldRedBgCyan($text, $eol = false)
85+
* @method Writer boldRedBgGreen($text, $eol = false)
86+
* @method Writer boldRedBgPurple($text, $eol = false)
87+
* @method Writer boldRedBgWhite($text, $eol = false)
88+
* @method Writer boldRedBgYellow($text, $eol = false)
89+
* @method Writer boldWhite($text, $eol = false)
90+
* @method Writer boldWhiteBgBlack($text, $eol = false)
91+
* @method Writer boldWhiteBgBlue($text, $eol = false)
92+
* @method Writer boldWhiteBgCyan($text, $eol = false)
93+
* @method Writer boldWhiteBgGreen($text, $eol = false)
94+
* @method Writer boldWhiteBgPurple($text, $eol = false)
95+
* @method Writer boldWhiteBgRed($text, $eol = false)
96+
* @method Writer boldWhiteBgYellow($text, $eol = false)
97+
* @method Writer boldYellow($text, $eol = false)
98+
* @method Writer boldYellowBgBlack($text, $eol = false)
99+
* @method Writer boldYellowBgBlue($text, $eol = false)
100+
* @method Writer boldYellowBgCyan($text, $eol = false)
101+
* @method Writer boldYellowBgGreen($text, $eol = false)
102+
* @method Writer boldYellowBgPurple($text, $eol = false)
103+
* @method Writer boldYellowBgRed($text, $eol = false)
104+
* @method Writer boldYellowBgWhite($text, $eol = false)
105+
* @method Writer colors($text)
106+
* @method Writer comment($text, $eol = false)
107+
* @method Writer cyan($text, $eol = false)
108+
* @method Writer cyanBgBlack($text, $eol = false)
109+
* @method Writer cyanBgBlue($text, $eol = false)
110+
* @method Writer cyanBgGreen($text, $eol = false)
111+
* @method Writer cyanBgPurple($text, $eol = false)
112+
* @method Writer cyanBgRed($text, $eol = false)
113+
* @method Writer cyanBgWhite($text, $eol = false)
114+
* @method Writer cyanBgYellow($text, $eol = false)
115+
* @method Writer eol(int $n = 1)
116+
* @method Writer error($text, $eol = false)
117+
* @method Writer green($text, $eol = false)
118+
* @method Writer greenBgBlack($text, $eol = false)
119+
* @method Writer greenBgBlue($text, $eol = false)
120+
* @method Writer greenBgCyan($text, $eol = false)
121+
* @method Writer greenBgPurple($text, $eol = false)
122+
* @method Writer greenBgRed($text, $eol = false)
123+
* @method Writer greenBgWhite($text, $eol = false)
124+
* @method Writer greenBgYellow($text, $eol = false)
125+
* @method Writer info($text, $eol = false)
126+
* @method Writer ok($text, $eol = false)
127+
* @method Writer purple($text, $eol = false)
128+
* @method Writer purpleBgBlack($text, $eol = false)
129+
* @method Writer purpleBgBlue($text, $eol = false)
130+
* @method Writer purpleBgCyan($text, $eol = false)
131+
* @method Writer purpleBgGreen($text, $eol = false)
132+
* @method Writer purpleBgRed($text, $eol = false)
133+
* @method Writer purpleBgWhite($text, $eol = false)
134+
* @method Writer purpleBgYellow($text, $eol = false)
135+
* @method Writer red($text, $eol = false)
136+
* @method Writer redBgBlack($text, $eol = false)
137+
* @method Writer redBgBlue($text, $eol = false)
138+
* @method Writer redBgCyan($text, $eol = false)
139+
* @method Writer redBgGreen($text, $eol = false)
140+
* @method Writer redBgPurple($text, $eol = false)
141+
* @method Writer redBgWhite($text, $eol = false)
142+
* @method Writer redBgYellow($text, $eol = false)
143+
* @method Writer warn($text, $eol = false)
144+
* @method Writer white($text, $eol = false)
145+
* @method Writer yellow($text, $eol = false)
146+
* @method Writer yellowBgBlack($text, $eol = false)
147+
* @method Writer yellowBgBlue($text, $eol = false)
148+
* @method Writer yellowBgCyan($text, $eol = false)
149+
* @method Writer yellowBgGreen($text, $eol = false)
150+
* @method Writer yellowBgPurple($text, $eol = false)
151+
* @method Writer yellowBgRed($text, $eol = false)
152+
* @method Writer yellowBgWhite($text, $eol = false)
15153
*/
16154
class Interactor
17155
{
@@ -142,7 +280,7 @@ public function prompt(string $text, $default = null, callable $fn = null, int $
142280
$error = $e->getMessage();
143281
}
144282

145-
if ($retry > 0 && (isset($e) || \strlen($input) === 0)) {
283+
if ($retry > 0 && (isset($e) || \strlen($input ?? '') === 0)) {
146284
$this->writer->bgRed($error, true);
147285

148286
return $this->prompt($text, $default, $fn, $retry - 1);
@@ -190,17 +328,13 @@ protected function listOptions(array $choices, $default = null, bool $multi = fa
190328
protected function promptOptions(array $choices, $default): self
191329
{
192330
$options = '';
193-
$color = $this->writer->colorizer();
194331

195332
foreach ($choices as $choice) {
196-
if (\in_array($choice, (array) $default)) {
197-
$options .= '/' . $color->boldCyan($choice);
198-
} else {
199-
$options .= '/' . $color->cyan($choice);
200-
}
333+
$style = \in_array($choice, (array) $default) ? 'boldCyan' : 'cyan';
334+
$options .= "/<$style>$choice</end>";
201335
}
202336

203-
$this->writer->raw(' (' . ltrim($options, '/') . '): ');
337+
$this->writer->colors(" ($options): ");
204338

205339
return $this;
206340
}

src/Input/Command.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Ahc\Cli\Input;
44

5-
use Ahc\Cli\Application;
5+
use Ahc\Cli\Application as App;
66
use Ahc\Cli\Helper\InflectsString;
77
use Ahc\Cli\Helper\OutputHelper;
88
use Ahc\Cli\IO\Interactor;
@@ -35,7 +35,7 @@ class Command extends Parser
3535
/** @var string */
3636
protected $_usage;
3737

38-
/** @var Application The cli app this command is bound to */
38+
/** @var App The cli app this command is bound to */
3939
protected $_app;
4040

4141
/** @var callable[] Events for options */
@@ -53,8 +53,9 @@ class Command extends Parser
5353
* @param string $name
5454
* @param string $desc
5555
* @param bool $allowUnknown
56+
* @param App $app
5657
*/
57-
public function __construct(string $name, string $desc = '', bool $allowUnknown = false, Application $app = null)
58+
public function __construct(string $name, string $desc = '', bool $allowUnknown = false, App $app = null)
5859
{
5960
$this->_name = $name;
6061
$this->_desc = $desc;
@@ -67,14 +68,14 @@ public function __construct(string $name, string $desc = '', bool $allowUnknown
6768
/**
6869
* Sets default options, actions and exit handler.
6970
*
70-
* @return void
71+
* @return self
7172
*/
7273
protected function defaults(): self
7374
{
7475
$this->option('-h, --help', 'Show help')->on([$this, 'showHelp']);
7576
$this->option('-V, --version', 'Show version')->on([$this, 'showVersion']);
7677
$this->option('-v, --verbosity', 'Verbosity level', null, 0)->on(function () {
77-
$this->set('verbosity', $this->verbosity + 1);
78+
$this->set('verbosity', ($this->verbosity ?? 0) + 1);
7879

7980
return false;
8081
});
@@ -125,7 +126,7 @@ public function desc(): string
125126
/**
126127
* Get the app this command belongs to.
127128
*
128-
* @return null|Application
129+
* @return null|App
129130
*/
130131
public function app()
131132
{
@@ -135,11 +136,11 @@ public function app()
135136
/**
136137
* Bind command to the app.
137138
*
138-
* @param Application|null $app
139+
* @param App|null $app
139140
*
140141
* @return self
141142
*/
142-
public function bind(Application $app = null): self
143+
public function bind(App $app = null): self
143144
{
144145
$this->_app = $app;
145146

@@ -212,7 +213,7 @@ public function option(string $raw, string $desc = '', callable $filter = null,
212213
/**
213214
* Gets user options (i.e without defaults).
214215
*
215-
* @return string
216+
* @return array
216217
*/
217218
public function userOptions(): array
218219
{
@@ -301,20 +302,20 @@ protected function handleUnknown(string $arg, string $value = null)
301302
*/
302303
public function showHelp()
303304
{
304-
$writer = $this->writer();
305-
$helper = new OutputHelper($writer);
305+
$io = $this->io();
306+
$helper = new OutputHelper($io->writer());
306307

307-
$writer
308-
->bold("Command {$this->_name}, version {$this->_version}", true)->eol()
309-
->comment($this->_desc, true)->eol()
310-
->bold('Usage: ')->yellow("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true);
308+
$io->bold("Command {$this->_name}, version {$this->_version}", true)->eol();
309+
$io->comment($this->_desc, true)->eol();
310+
$io->bold('Usage: ')->yellow("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true);
311311

312312
$helper
313313
->showArgumentsHelp($this->allArguments())
314314
->showOptionsHelp($this->allOptions(), '', 'Legend: <required> [optional]');
315315

316316
if ($this->_usage) {
317-
$writer->eol()->greenBold('Usage Examples:', true)->raw(\trim($this->_usage))->eol();
317+
$io->eol();
318+
$io->boldGreen('Usage Examples:', true)->raw(\trim($this->_usage))->eol();
318319
}
319320

320321
return $this->emit('_exit', 0);
@@ -338,7 +339,7 @@ public function showVersion()
338339
public function emit(string $event, $value = null)
339340
{
340341
if (empty($this->_events[$event])) {
341-
return;
342+
return null;
342343
}
343344

344345
return ($this->_events[$event])($value);
@@ -395,4 +396,14 @@ protected function writer(): Writer
395396
{
396397
return $this->_app ? $this->_app->io()->writer() : new Writer;
397398
}
399+
400+
/**
401+
* Get IO instance.
402+
*
403+
* @return Interactor
404+
*/
405+
protected function io(): Interactor
406+
{
407+
return $this->_app ? $this->_app->io() : new Interactor;
408+
}
398409
}

src/Input/Option.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function short(): string
6262
/**
6363
* Test if this option matches given arg.
6464
*
65+
* @param string $arg
66+
*
6567
* @return bool
6668
*/
6769
public function is(string $arg): bool

0 commit comments

Comments
 (0)