Skip to content

Commit 9629d99

Browse files
authored
Merge pull request #52 from adhocore/develop
Develop
2 parents 13254de + 3eb5907 commit 9629d99

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

src/Helper/Normalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Ahc\Cli\Helper;
1313

14+
use Ahc\Cli\Input\Option;
1415
use Ahc\Cli\Input\Parameter;
1516

1617
/**
@@ -58,8 +59,8 @@ public function normalizeArgs(array $args): array
5859
*/
5960
public function normalizeValue(Parameter $parameter, string $value = null)
6061
{
61-
if (\is_bool($default = $parameter->default())) {
62-
return !$default;
62+
if ($parameter instanceof Option && $parameter->bool()) {
63+
return !$parameter->default();
6364
}
6465

6566
if ($parameter->variadic()) {

src/Helper/OutputHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ public function showUsage(string $usage = null): self
211211
}
212212

213213
$lines = \explode("\n", \str_replace(['<eol>', '<eol/>', '</eol>', "\r\n"], "\n", $usage));
214-
foreach ($lines as &$pos) {
215-
$pos = \strpos(\preg_replace('~<.*?>~', '', $pos), ' ##');
214+
foreach ($lines as $i => &$pos) {
215+
if (false === $pos = \strrpos(\preg_replace('~</?\w+/?>~', '', $pos), ' ##')) {
216+
unset($lines[$i]);
217+
}
216218
}
217219

218220
$maxlen = \max($lines) + 4;

src/Input/Option.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ public function is(string $arg): bool
8787
*/
8888
public function bool(): bool
8989
{
90-
return \preg_match('/\-no|\-with/', $this->long) > 0;
90+
return \preg_match('/\-no-|\-with-/', $this->long) > 0;
9191
}
9292
}

src/Output/Color.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Color
3535
const DARKGRAY = 100;
3636

3737
/** @var string Cli format */
38-
protected $format = "\033[:bold:;:fg:;:bg:m:text:\033[0m";
38+
protected $format = "\033[:mod:;:fg:;:bg:m:txt:\033[0m";
3939

4040
/** @var array Custom styles */
4141
protected static $styles = [];
@@ -50,7 +50,7 @@ class Color
5050
*/
5151
public function comment(string $text, array $style = []): string
5252
{
53-
return $this->line($text, ['bold' => 2] + $style);
53+
return $this->line($text, ['mod' => 2] + $style);
5454
}
5555

5656
/**
@@ -115,17 +115,17 @@ public function info(string $text, array $style = []): string
115115
*/
116116
public function line(string $text, array $style = []): string
117117
{
118-
$style += ['bg' => null, 'fg' => static::WHITE, 'bold' => 0];
118+
$style += ['bg' => null, 'fg' => static::WHITE, 'bold' => 0, 'mod' => null];
119119

120120
$format = $style['bg'] === null
121121
? \str_replace(';:bg:', '', $this->format)
122122
: $this->format;
123123

124124
$line = \strtr($format, [
125-
':bold:' => (int) $style['bold'],
126-
':fg:' => (int) $style['fg'],
127-
':bg:' => (int) $style['bg'] + 10,
128-
':text:' => $text,
125+
':mod:' => (int) ($style['mod'] ?? $style['bold']),
126+
':fg:' => (int) $style['fg'],
127+
':bg:' => (int) $style['bg'] + 10,
128+
':txt:' => $text,
129129
]);
130130

131131
return $line;
@@ -227,9 +227,13 @@ protected function parseCall(string $name, array $arguments): array
227227
{
228228
list($text, $style) = $arguments + ['', []];
229229

230-
if (\stripos($name, 'bold') !== false) {
231-
$name = \str_ireplace('bold', '', $name);
232-
$style += ['bold' => 1];
230+
$mods = ['bold' => 1, 'dim' => 2, 'italic' => 3, 'underline' => 4, 'flash' => 5];
231+
232+
foreach ($mods as $mod => $value) {
233+
if (\stripos($name, $mod) !== false) {
234+
$name = \str_ireplace($mod, '', $name);
235+
$style += ['bold' => $value];
236+
}
233237
}
234238

235239
if (!\preg_match_all('/([b|B|f|F]g)?([A-Z][a-z]+)([^A-Z])?/', $name, $matches)) {

tests/Helper/OutputHelperTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function test_show_usage()
109109
'<bold> $0</end> <comment>-a apple</end> ## apple only<eol>',
110110
'<bold> $0</end> <comment>-a apple -b ball</end> ## apple ball<eol>',
111111
'loooooooooooong text ## something<eol>',
112+
'no shell comments<eol>',
112113
'short text ## something else<eol>',
113114
]));
114115

@@ -118,6 +119,7 @@ public function test_show_usage()
118119
' test -a apple # apple only',
119120
' test -a apple -b ball # apple ball',
120121
'loooooooooooong text # something',
122+
'no shell comments',
121123
'short text # something else',
122124
'',
123125
], $this->output());
@@ -128,7 +130,7 @@ public function test_show_usage()
128130
public function newHelper()
129131
{
130132
return new OutputHelper(new Writer(static::$ou, new class extends Color {
131-
protected $format = ':text:';
133+
protected $format = ':txt:';
132134
}));
133135
}
134136

tests/Input/CommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ protected function newCommand(string $version = '0.0.1', string $desc = '', bool
250250
{
251251
$p = new Command('cmd', $desc, $allowUnknown, $app);
252252

253-
return $p->version($version);
253+
return $p->version($version . \debug_backtrace()[1]['function'])->onExit(function () {
254+
return false;
255+
});
254256
}
255257
}

0 commit comments

Comments
 (0)