Skip to content

Commit 0f193df

Browse files
committed
move some cli tools to toolkit/cli-utils
1 parent 8d62fc3 commit 0f193df

File tree

3 files changed

+462
-0
lines changed

3 files changed

+462
-0
lines changed

src/Cli.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/5/1
6+
* Time: 下午5:33
7+
*/
8+
9+
namespace Toolkit\Sys;
10+
11+
/**
12+
* Class Cli
13+
* @package Toolkit\Sys
14+
*/
15+
class Cli
16+
{
17+
/*******************************************************************************
18+
* color render
19+
******************************************************************************/
20+
21+
/**
22+
* @param $text
23+
* @param string|int|array $style
24+
* @return string
25+
*/
26+
public static function color(string $text, $style = null): string
27+
{
28+
return Color::render($text, $style);
29+
}
30+
31+
/**
32+
* @param string $text
33+
* @return string
34+
*/
35+
public static function clearColor($text): string
36+
{
37+
// return preg_replace('/\033\[(?:\d;?)+m/', '' , "\033[0;36mtext\033[0m");
38+
return preg_replace('/\033\[(?:\d;?)+m/', '', strip_tags($text));
39+
}
40+
41+
/*******************************************************************************
42+
* read/write message
43+
******************************************************************************/
44+
45+
/**
46+
* @param mixed $message
47+
* @param bool $nl
48+
* @return string
49+
*/
50+
public static function read($message = null, $nl = false): string
51+
{
52+
if ($message) {
53+
self::write($message, $nl);
54+
}
55+
56+
return trim(fgets(STDIN));
57+
}
58+
59+
/**
60+
* write message to console
61+
* @param $message
62+
* @param bool $nl
63+
* @param bool $quit
64+
*/
65+
public static function write($message, $nl = true, $quit = false)
66+
{
67+
if (\is_array($message)) {
68+
$message = implode($nl ? PHP_EOL : '', $message);
69+
}
70+
71+
self::stdout(Color::parseTag($message), $nl, $quit);
72+
}
73+
74+
/**
75+
* Logs data to stdout
76+
* @param string $message
77+
* @param bool $nl
78+
* @param bool|int $quit
79+
*/
80+
public static function stdout($message, $nl = true, $quit = false)
81+
{
82+
fwrite(\STDOUT, $message . ($nl ? PHP_EOL : ''));
83+
fflush(\STDOUT);
84+
85+
if (($isTrue = true === $quit) || \is_int($quit)) {
86+
$code = $isTrue ? 0 : $quit;
87+
exit($code);
88+
}
89+
}
90+
91+
/**
92+
* Logs data to stderr
93+
* @param string $message
94+
* @param bool $nl
95+
* @param bool|int $quit
96+
*/
97+
public static function stderr($message, $nl = true, $quit = -200)
98+
{
99+
fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
100+
fflush(\STDOUT);
101+
102+
if (($isTrue = true === $quit) || \is_int($quit)) {
103+
$code = $isTrue ? 0 : $quit;
104+
exit($code);
105+
}
106+
}
107+
108+
/**
109+
* Returns true if STDOUT supports colorization.
110+
* This code has been copied and adapted from
111+
* \Symfony\Component\Console\Output\OutputStream.
112+
* @return boolean
113+
*/
114+
public static function isSupportColor(): bool
115+
{
116+
return SysEnv::isSupportColor();
117+
}
118+
119+
/**
120+
* Parses $GLOBALS['argv'] for parameters and assigns them to an array.
121+
* Supports:
122+
* -e
123+
* -e <value>
124+
* --long-param
125+
* --long-param=<value>
126+
* --long-param <value>
127+
* <value>
128+
* @link https://github.com/inhere/php-console/blob/master/src/io/Input.php
129+
* @param array $noValues List of parameters without values
130+
* @param bool $mergeOpts
131+
* @return array
132+
*/
133+
public static function parseArgv(array $noValues = [], $mergeOpts = false): array
134+
{
135+
$params = $GLOBALS['argv'];
136+
reset($params);
137+
138+
$args = $sOpts = $lOpts = [];
139+
$fullScript = implode(' ', $params);
140+
$script = array_shift($params);
141+
142+
// each() will deprecated at 7.2 so,there use current and next instead it.
143+
// while (list(,$p) = each($params)) {
144+
while (false !== ($p = current($params))) {
145+
next($params);
146+
147+
// is options
148+
if ($p{0} === '-') {
149+
$isLong = false;
150+
$opt = substr($p, 1);
151+
$value = true;
152+
153+
// long-opt: (--<opt>)
154+
if ($opt{0} === '-') {
155+
$isLong = true;
156+
$opt = substr($opt, 1);
157+
158+
// long-opt: value specified inline (--<opt>=<value>)
159+
if (strpos($opt, '=') !== false) {
160+
list($opt, $value) = explode('=', $opt, 2);
161+
}
162+
163+
// short-opt: value specified inline (-<opt>=<value>)
164+
} elseif (\strlen($opt) > 2 && $opt{1} === '=') {
165+
list($opt, $value) = explode('=', $opt, 2);
166+
}
167+
168+
// check if next parameter is a descriptor or a value
169+
$nxp = current($params);
170+
171+
if ($value === true && $nxp !== false && $nxp{0} !== '-' && !\in_array($opt, $noValues, true)) {
172+
// list(,$value) = each($params);
173+
$value = current($params);
174+
next($params);
175+
176+
// short-opt: bool opts. like -e -abc
177+
} elseif (!$isLong && $value === true) {
178+
foreach (str_split($opt) as $char) {
179+
$sOpts[$char] = true;
180+
}
181+
182+
continue;
183+
}
184+
185+
if ($isLong) {
186+
$lOpts[$opt] = $value;
187+
} else {
188+
$sOpts[$opt] = $value;
189+
}
190+
191+
// arguments: param doesn't belong to any option, define it is args
192+
} else {
193+
// value specified inline (<arg>=<value>)
194+
if (strpos($p, '=') !== false) {
195+
list($name, $value) = explode('=', $p, 2);
196+
$args[$name] = $value;
197+
} else {
198+
$args[] = $p;
199+
}
200+
}
201+
}
202+
203+
unset($params);
204+
205+
if ($mergeOpts) {
206+
return [$fullScript, $script, $args, array_merge($sOpts, $lOpts)];
207+
}
208+
209+
return [$fullScript, $script, $args, $sOpts, $lOpts];
210+
}
211+
212+
}

0 commit comments

Comments
 (0)