Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6809673

Browse files
committed
fix some bugs
1 parent ae24f95 commit 6809673

File tree

4 files changed

+96
-68
lines changed

4 files changed

+96
-68
lines changed

‎libs/cli-utils/example/down.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
require dirname(__DIR__) . '/test/boot.php';
99

10-
$url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2';
1110
$app = new \Toolkit\Cli\App();
12-
$down = Download::file($url, '');
11+
$url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2';
12+
$down = Download::create($url, '');
13+
14+
$type = $app->getOpt('type', 'text');
1315

14-
$type = $app->getArg(0, 'text');
1516
if ($type === 'bar') {
1617
$down->setShowType($type);
1718
}

‎libs/cli-utils/src/Download.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ final class Download
2929
/** @var string */
3030
private $showType;
3131

32+
/**
33+
* @param string $url
34+
* @param string $saveAs
35+
* @param string $type
36+
* @return Download
37+
*/
38+
public static function create(string $url, string $saveAs = '', string $type = self::PROGRESS_TEXT)
39+
{
40+
return new self($url, $saveAs, $type);
41+
}
42+
3243
/**
3344
* eg: php down.php <http://example.com/file> <localFile>
3445
* @param string $url

‎libs/cli-utils/src/Flags.php

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ public static function parseArgv(array $params, array $config = []): array
9090

9191
$config = \array_merge([
9292
// List of parameters without values(bool option keys)
93-
'noValues' => [], // ['debug', 'h']
93+
'boolOpts' => [], // ['debug', 'h']
9494
// Whether merge short-opts and long-opts
9595
// 'mergeOpts' => false,
9696
// want parsed options. if not empty, will ignore no matched
9797
'wantParsedOpts' => [],
98-
// list of params allow array.
99-
'arrayValues' => [], // ['names', 'status']
98+
// list of option allow array values.
99+
'arrayOpts' => [], // ['names', 'status']
100100
], $config);
101101

102102
$args = $sOpts = $lOpts = [];
103103
// config
104-
$noValues = \array_flip((array)$config['noValues']);
105-
$arrValues = \array_flip((array)$config['arrayValues']);
104+
$boolOpts = \array_flip((array)$config['boolOpts']);
105+
$arrayOpts = \array_flip((array)$config['arrayOpts']);
106106

107107
// each() will deprecated at 7.2. so,there use current and next instead it.
108108
// while (list(,$p) = each($params)) {
@@ -111,65 +111,69 @@ public static function parseArgv(array $params, array $config = []): array
111111

112112
// is options
113113
if ($p{0} === '-') {
114-
$val = true;
115-
$opt = \substr($p, 1);
114+
$value = true;
115+
$option = \substr($p, 1);
116116
$isLong = false;
117117

118118
// long-opt: (--<opt>)
119-
if (\strpos($opt, '-') === 0) {
120-
$opt= \substr($opt, 1);
119+
if (\strpos($option, '-') === 0) {
120+
$option= \substr($option, 1);
121121
$isLong = true;
122122

123123
// long-opt: value specified inline (--<opt>=<value>)
124-
if (\strpos($opt, '=') !== false) {
125-
list($opt, $val) = \explode('=', $opt, 2);
124+
if (\strpos($option, '=') !== false) {
125+
list($option, $value) = \explode('=', $option, 2);
126126
}
127127

128128
// short-opt: value specified inline (-<opt>=<value>)
129-
} elseif (isset($opt{1}) && $opt{1} === '=') {
130-
list($opt, $val) = \explode('=', $opt, 2);
129+
} elseif (isset($option{1}) && $option{1} === '=') {
130+
list($option, $value) = \explode('=', $option, 2);
131131
}
132132

133133
// check if next parameter is a descriptor or a value
134134
$nxt = \current($params);
135135

136136
// next elem is value. fix: allow empty string ''
137-
if ($val === true && !isset($noValues[$opt]) && self::nextIsValue($nxt)) {
137+
if ($value === true && !isset($boolOpts[$option]) && self::nextIsValue($nxt)) {
138138
// list(,$val) = each($params);
139-
$val = $nxt;
139+
$value = $nxt;
140140
\next($params);
141141

142142
// short-opt: bool opts. like -e -abc
143-
} elseif (!$isLong && $val === true) {
144-
foreach (\str_split($opt) as $char) {
143+
} elseif (!$isLong && $value === true) {
144+
foreach (\str_split($option) as $char) {
145145
$sOpts[$char] = true;
146146
}
147147
continue;
148148
}
149149

150-
$val = self::filterBool($val);
151-
$isArray = isset($arrValues[$opt]);
150+
$value = self::filterBool($value);
151+
$isArray = isset($arrayOpts[$option]);
152152

153153
if ($isLong) {
154154
if ($isArray) {
155-
$lOpts[$opt][] = $val;
155+
$lOpts[$option][] = $value;
156156
} else {
157-
$lOpts[$opt] = $val;
157+
$lOpts[$option] = $value;
158158
}
159159
} elseif ($isArray) { // short
160-
$sOpts[$opt][] = $val;
160+
$sOpts[$option][] = $value;
161161
} else { // short
162-
$sOpts[$opt] = $val;
162+
$sOpts[$option] = $value;
163163
}
164-
// arguments: param doesn't belong to any option, define it is args
164+
165+
continue;
166+
}
167+
168+
// parse arguments:
169+
// - param doesn't belong to any option, define it is args
170+
171+
// value specified inline (<arg>=<value>)
172+
if (\strpos($p, '=') !== false) {
173+
list($name, $value) = \explode('=', $p, 2);
174+
$args[$name] = self::filterBool($value);
165175
} else {
166-
// value specified inline (<arg>=<value>)
167-
if (\strpos($p, '=') !== false) {
168-
list($name, $val) = \explode('=', $p, 2);
169-
$args[$name] = self::filterBool($val);
170-
} else {
171-
$args[] = $p;
172-
}
176+
$args[] = $p;
173177
}
174178
}
175179

@@ -183,6 +187,7 @@ public static function parseArgv(array $params, array $config = []): array
183187
* 'arg' => 'val',
184188
* '--lp' => 'val2',
185189
* '--s' => 'val3',
190+
* '-h' => true,
186191
* ]);
187192
* ```
188193
* @param array $params
@@ -193,14 +198,22 @@ public static function parseArray(array $params): array
193198
$args = $sOpts = $lOpts = [];
194199

195200
foreach ($params as $key => $val) {
196-
if ($key === '--' || $key === '-') {
201+
if (\is_int($key)) { // as argument
202+
$args[$key] = $val;
197203
continue;
198204
}
199205

200-
if (0 === \strpos($key, '--')) {
201-
$lOpts[substr($key, 2)] = $val;
202-
} elseif (\strpos($key, '-') === 0) {
203-
$sOpts[\substr($key, 1)] = $val;
206+
$cleanKey = \trim((string)$key, '-');
207+
208+
if ('' === $cleanKey) { // as argument
209+
$args[] = $val;
210+
continue;
211+
}
212+
213+
if (0 === \strpos($key, '--')) { // long option
214+
$lOpts[$cleanKey] = $val;
215+
} elseif (0 === \strpos($key, '-')) { // short option
216+
$sOpts[$cleanKey] = $val;
204217
} else {
205218
$args[$key] = $val;
206219
}
@@ -210,9 +223,12 @@ public static function parseArray(array $params): array
210223
}
211224

212225
/**
226+
* parse flags from a string
227+
*
213228
* ```php
214229
* $result = Flags::parseString('foo --bar="foobar"');
215230
* ```
231+
*
216232
* @todo ...
217233
* @param string $string
218234
*/
@@ -250,7 +266,7 @@ public static function filterBool($val, $enable = true)
250266
* @param mixed $val
251267
* @return bool
252268
*/
253-
public static function nextIsValue(string$val): bool
269+
public static function nextIsValue($val): bool
254270
{
255271
// current() fetch error, will return FALSE
256272
if ($val === false) {

‎libs/cli-utils/src/Highlighter.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function highlight(string $source, bool $withLineNumber = false): string
7777
return $this->lineNumbers($lines);
7878
}
7979

80-
return implode(PHP_EOL, $lines);
80+
return \implode(\PHP_EOL, $lines);
8181
}
8282

8383
/**
@@ -106,7 +106,7 @@ public function highlightSnippet($source, $lineNumber, $linesBefore = 2, $linesA
106106
$tokenLines = $this->getHighlightedLines($source);
107107

108108
$offset = $lineNumber - $linesBefore - 1;
109-
$offset = max($offset, 0);
109+
$offset = \max($offset, 0);
110110
$length = $linesAfter + $linesBefore + 1;
111111
$tokenLines = \array_slice($tokenLines, $offset, $length, $preserveKeys = true);
112112

@@ -146,40 +146,40 @@ private function tokenize(string $source): array
146146
foreach ($tokens as $token) {
147147
if (\is_array($token)) {
148148
switch ($token[0]) {
149-
case T_INLINE_HTML:
149+
case \T_INLINE_HTML:
150150
$newType = self::TOKEN_HTML;
151151
break;
152-
case T_COMMENT:
153-
case T_DOC_COMMENT:
152+
case \T_COMMENT:
153+
case \T_DOC_COMMENT:
154154
$newType = self::TOKEN_COMMENT;
155155
break;
156-
case T_ENCAPSED_AND_WHITESPACE:
157-
case T_CONSTANT_ENCAPSED_STRING:
156+
case \T_ENCAPSED_AND_WHITESPACE:
157+
case \T_CONSTANT_ENCAPSED_STRING:
158158
$newType = self::TOKEN_STRING;
159159
break;
160-
case T_WHITESPACE:
160+
case \T_WHITESPACE:
161161
break;
162-
case T_OPEN_TAG:
163-
case T_OPEN_TAG_WITH_ECHO:
164-
case T_CLOSE_TAG:
165-
case T_STRING:
166-
case T_VARIABLE:
162+
case \T_OPEN_TAG:
163+
case \T_OPEN_TAG_WITH_ECHO:
164+
case \T_CLOSE_TAG:
165+
case \T_STRING:
166+
case \T_VARIABLE:
167167
// Constants
168-
case T_DIR:
169-
case T_FILE:
170-
case T_METHOD_C:
171-
case T_DNUMBER:
172-
case T_LNUMBER:
173-
case T_NS_C:
174-
case T_LINE:
175-
case T_CLASS_C:
176-
case T_FUNC_C:
168+
case \T_DIR:
169+
case \T_FILE:
170+
case \T_METHOD_C:
171+
case \T_DNUMBER:
172+
case \T_LNUMBER:
173+
case \T_NS_C:
174+
case \T_LINE:
175+
case \T_CLASS_C:
176+
case \T_FUNC_C:
177177
//case T_TRAIT_C:
178178
$newType = self::TOKEN_DEFAULT;
179179
break;
180180
default:
181181
// Compatibility with PHP 5.3
182-
if (\defined('T_TRAIT_C') && $token[0] === T_TRAIT_C) {
182+
if (\defined('T_TRAIT_C') && $token[0] === \T_TRAIT_C) {
183183
$newType = self::TOKEN_DEFAULT;
184184
} else {
185185
$newType = self::TOKEN_KEYWORD;
@@ -218,7 +218,7 @@ private function splitToLines(array $tokens): array
218218
$lines = $line = [];
219219

220220
foreach ($tokens as $token) {
221-
foreach (explode("\n", $token[1]) as $count => $tokenLine) {
221+
foreach (\explode("\n", $token[1]) as $count => $tokenLine) {
222222
if ($count > 0) {
223223
$lines[] = $line;
224224
$line = [];
@@ -273,10 +273,10 @@ private function colorLines(array $tokenLines): array
273273
*/
274274
private function lineNumbers(array $lines, $markLine = null): string
275275
{
276-
end($lines);
276+
\end($lines);
277277

278278
$snippet = '';
279-
$lineLen = \strlen(key($lines) + 1);
279+
$lineLen = \strlen(\key($lines) + 1);
280280
$lmStyle = $this->defaultTheme[self::ACTUAL_LINE_MARK];
281281
$lnStyle = $this->defaultTheme[self::LINE_NUMBER];
282282

@@ -285,10 +285,10 @@ private function lineNumbers(array $lines, $markLine = null): string
285285
$snippet .= ($markLine === $i + 1 ? Color::apply($lmStyle, ' > ') : '');
286286
$snippet .= Color::apply(
287287
$markLine === $i + 1 ? $lmStyle : $lnStyle,
288-
str_pad($i + 1, $lineLen, '', STR_PAD_LEFT) . '| '
288+
\str_pad($i + 1, $lineLen, '', \STR_PAD_LEFT) . '| '
289289
);
290290
} else {
291-
$snippet .= Color::apply($lnStyle, str_pad($i + 1, $lineLen, '', STR_PAD_LEFT) . '| ');
291+
$snippet .= Color::apply($lnStyle, \str_pad($i + 1, $lineLen, '', \STR_PAD_LEFT) . '| ');
292292
}
293293

294294
$snippet .= $line . PHP_EOL;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /