Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 3a1b319

Browse files
committed
update
add some document for console console tool class update
1 parent 90ec6fb commit 3a1b319

File tree

8 files changed

+225
-36
lines changed

8 files changed

+225
-36
lines changed

doc/console.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# console tool
2+
3+
```
4+
use inhere\librarys\console\Input;
5+
use inhere\librarys\console\Output;
6+
7+
$input = new Input;
8+
$output = new Output;
9+
```
10+
11+
## input
12+
13+
example(in terminal):
14+
15+
```
16+
./bin/app image/packTask name=john -d -s=test --debug=true
17+
php bin/cli.php start name=john -d -s=test --debug=true
18+
```
19+
20+
get command info:
21+
22+
```
23+
$script = $input->getScript(); // './bin/app' 'bin/cli.php'
24+
$command = $input->getCommand(); // 'image/packTask' 'start'
25+
26+
// argument
27+
$name = $input->get('name', 'default'); // 'john'
28+
$s = $input->get('s', 'default'); // 'test'
29+
30+
// option
31+
$d = $input->getBool('d') // True
32+
$debug = $input->getBool('debug') // True
33+
$noexists = $input->getBool('noexists') // False
34+
```
35+
36+
get user input:
37+
38+
```
39+
echo "Your name:";
40+
41+
$text = $input->read();
42+
// in terminal
43+
// Your name: simon
44+
45+
echo $text; // 'simon'
46+
```
47+
48+
## output
49+
50+
basic output:
51+
52+
```
53+
$output->write($message);
54+
```
55+
56+
### formatted output
57+
58+
#### use color style
59+
60+
![alt text](images/new-color-style-list.png "Title")
61+
62+
#### specil format
63+
64+
- `$output->title()`
65+
- `$output->section()`
66+
- `$output->panel()`
67+
- `$output->table()`
68+
- `$output->helpPanel()`
69+
70+
![alt text](images/output-panel-table-title.jpg "Title")
71+
72+
## more interactive
73+
74+
in the class `inhere\librarys\console\Interact`
75+
76+
interactive method:
77+
78+
### `Interact::select()` (alias `Interact::chioce()`)
79+
80+
Select one of the options
81+
82+
```
83+
select($description, $options, $default = null, $allowExit=true)
84+
choice($description, $options, $default = null, $allowExit=true)
85+
```
86+
87+
- example 1:
88+
89+
only value, no setting option
90+
91+
```
92+
$select = Interact::select('Your city is ?', [
93+
'chengdu', 'beijing', 'shanghai'
94+
]);
95+
96+
```
97+
98+
output in terminal:
99+
```
100+
Your city is?
101+
0) chengdu
102+
1) beijing
103+
2) shanghai
104+
q) Quit // quit option. is auto add. can setting it by 4th argument.
105+
You choice: chengdu
106+
```
107+
108+
```
109+
echo $select; // '0'
110+
```
111+
112+
- example 2:
113+
114+
custom option, setting a default value.
115+
116+
```
117+
$select = Interact::select('Your city is ?', [
118+
'a' => 'chengdu',
119+
'b' => 'beijing',
120+
'c' => 'shanghai'
121+
], 'a');
122+
```
123+
124+
output in terminal:
125+
126+
```
127+
Your city is?
128+
a) chengdu
129+
b) beijing
130+
c) shanghai
131+
q) Quit // quit option. is auto add. can setting it by 4th argument.
132+
You choice[default:a] : beijing
133+
```
134+
135+
```
136+
echo $select; // 'b'
137+
```
138+
139+
### `Interact::confirm()`
140+
141+
### `Interact::question()`
142+
143+
### `Interact::loopAsk()`
144+
145+
```
146+
147+
```
148+
149+
## formatted output

src/console/ConsoleHelper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ public static function spliceKeyValue(array $data, array $opts = [])
102102
$text .= ( $keyStyle ? "<{$keyStyle}>$key</{$keyStyle}> " : $key ) . $opts['sepChar'];
103103
}
104104

105+
// if value is array, translate array to string
106+
if ( is_array($value) ) {
107+
$temp = '';
108+
109+
foreach ($value as $key => $val) {
110+
if (is_bool($val)) {
111+
$val = $val ? 'True' : 'False';
112+
} else {
113+
$val = (string)$val;
114+
}
115+
116+
$temp .= (!is_numeric($key) ? "$key: " : '') . "<info>$val</info>, ";
117+
}
118+
119+
$value = rtrim($temp, ' ,');
120+
} else if (is_bool($value)) {
121+
$value = $value ? 'True' : 'False';
122+
} else {
123+
$value = (string)$value;
124+
}
125+
105126
$text .= "$value\n";
106127
}
107128

src/console/Input.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* Class Input
1313
* @package inhere\librarys\console
1414
* e.g:
15-
* ./bin/app image/packTask test -d -s=df --debug=true
16-
* php bin/cli.php start test -d -s=df --debug=true
15+
* ./bin/app image/packTask name=john -d -s=test --debug=true
16+
* php bin/cli.php start name=john -d -s=test --debug=true
1717
*/
1818
class Input
1919
{
@@ -75,6 +75,10 @@ public function get($name=null, $default = null)
7575

7676
return isset($this->data[$name]) ? $this->data[$name] : $default;
7777
}
78+
public function getOption($name=null, $default = null)
79+
{
80+
return $this->get($name, $default);
81+
}
7882

7983
/**
8084
* @param $key
@@ -100,6 +104,14 @@ public function getScriptName()
100104
return self::$scriptName;
101105
}
102106

107+
/**
108+
* @return string
109+
*/
110+
public function getScript()
111+
{
112+
return self::$scriptName;
113+
}
114+
103115
/**
104116
* @return string
105117
*/
@@ -128,8 +140,8 @@ public function getInputStream()
128140
*/
129141
public static function parseGlobalArgv($fixServer = false, $fillToGlobal = false)
130142
{
131-
// ./bin/app image/packTask start test -d -s=df --debug=true
132-
// php bin/cli.php image/packTask start test -d -s=df --debug=true
143+
// ./bin/app image/packTask start name=john -d -s=test --debug=true
144+
// php bin/cli.php image/packTask start name=john -d -s=test --debug=true
133145
global $argv;
134146
$args = $argv;
135147

@@ -165,13 +177,15 @@ public static function parseGlobalArgv($fixServer = false, $fillToGlobal = false
165177
$data = [];
166178

167179
// parse query params
168-
// ./bin/app image/packTask start test -d -s=df --debug=true
180+
// ./bin/app image/packTask start name=john -d -s=test --debug=true
169181
// parse to
170-
// ./bin/app image/packTask?start&test&d&s=df&debug=true
182+
// ./bin/app image/packTask?start&name=john&d&s=test&debug=true
171183
if ($args) {
172-
$url = preg_replace('/&[-]+/', '&', implode('&',$args));
184+
$args = array_map(function($val){
185+
return trim($val,'- ');
186+
}, $args);
173187

174-
parse_str($url, $data);
188+
parse_str(implode('&',$args), $data);
175189

176190
if ($fillToGlobal) {
177191
$_REQUEST = $_GET = $data;

src/console/Interact.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
namespace inhere\librarys\console;
12+
1213
use inhere\librarys\exceptions\InvalidArgumentException;
1314

1415
/**
@@ -23,7 +24,7 @@ class Interact
2324
/////////////////////////////////////////////////////////////////
2425

2526
/**
26-
* 多选一
27+
* Select one of the options 在多个选项中选择一个
2728
* @param string $description 说明
2829
* @param mixed $options 选项数据
2930
* e.g
@@ -50,7 +51,7 @@ public static function choice($description, $options, $default = null, $allowExi
5051

5152
$keys = [];
5253
$optStr = '';
53-
$options = is_array($options) ? $options : explode(',', $options);
54+
$options = is_array($options) ? $options : explode(',', $options);
5455

5556
// If defaut option is error
5657
if ( null === $default && !isset($options[$default]) ) {
@@ -67,9 +68,10 @@ public static function choice($description, $options, $default = null, $allowExi
6768
$optStr .= "\n q) quit";
6869
}
6970

70-
$r = self::read($optStr . "\n You choice : ");
71+
$defaultText = $default ? "[default:<comment>{$default}</comment>]" : '';
72+
$r = self::read($optStr . "\n You choice{$defaultText} : ");
7173

72-
// error, allow try again
74+
// error, allow try again once.
7375
if ( !in_array($r, $keys) ) {
7476
$r = self::read("Warning! Option <info>$r</info>) don't exists! Please entry again! : ");
7577
}
@@ -348,7 +350,7 @@ public static function helpPanel($usage, $commands = [], $options = [], $example
348350

349351
// examples list
350352
if ( $examples ) {
351-
$examples = is_array($examples) ? implode(PHP_EOL, $examples) : $examples;
353+
$examples = is_array($examples) ? implode(PHP_EOL . ' ', $examples) : (string)$examples;
352354
self::write("<comment>Examples</comment>:\n {$examples}\n");
353355
}
354356

@@ -359,11 +361,12 @@ public static function helpPanel($usage, $commands = [], $options = [], $example
359361

360362
/**
361363
* Show information data panel
362-
* @param mixed $data
364+
* @param mixed $data
363365
* @param string $title
366+
* @param string $borderChar
364367
* @return void
365368
*/
366-
public static function panel($data, $title='Information Panel', $char = '*')
369+
public static function panel($data, $title='Information Panel', $borderChar = '*')
367370
{
368371
$data = is_array($data) ? array_filter($data) : [trim($data)];
369372
$title = trim($title);
@@ -396,17 +399,14 @@ public static function panel($data, $title='Information Panel', $char = '*')
396399
$value = rtrim($temp, ' ,');
397400
} else if (is_bool($value)) {
398401
$value = $value ? 'True' : 'False';
402+
} else {
403+
$value = trim((string)$value);
399404
}
400405

401406
// get value width
402-
if ( is_string($value) || is_numeric($value) ) {
403-
$value = trim($value);
404-
$width = mb_strlen(strip_tags($value), 'UTF-8'); // must clear style tag
405-
$valueMaxWidth = $width > $valueMaxWidth ? $width : $valueMaxWidth;
406-
} else {
407-
de((string)$value);
408-
throw new \Exception('Panel data value only allow [array|string|number]');
409-
}
407+
$value = trim($value);
408+
$width = mb_strlen(strip_tags($value), 'UTF-8'); // must clear style tag
409+
$valueMaxWidth = $width > $valueMaxWidth ? $width : $valueMaxWidth;
410410

411411
$panelData[$label] = $value;
412412
}
@@ -423,12 +423,14 @@ public static function panel($data, $title='Information Panel', $char = '*')
423423
}
424424

425425
// output panel top border
426-
$border = str_pad($char, $panelWidth + (3*3), $char);
427-
self::write(' ' . $border);
426+
if ($borderChar) {
427+
$border = str_pad($borderChar, $panelWidth + (3*3), $borderChar);
428+
self::write(' ' . $border);
429+
}
428430

429431
// output panel body
430432
$panelStr = ConsoleHelper::spliceKeyValue($panelData, [
431-
'leftChar' => " $char ",
433+
'leftChar' => " $borderChar ",
432434
'sepChar' => ' | ',
433435
'keyMaxWidth' => $labelMaxWidth,
434436
]);
@@ -437,7 +439,9 @@ public static function panel($data, $title='Information Panel', $char = '*')
437439
self::write($panelStr, false);
438440

439441
// output panel bottom border
440-
self::write(" $border\n");
442+
if (isset($border)) {
443+
self::write(" $border\n");
444+
}
441445

442446
unset($data, $panelData);
443447
}
@@ -519,7 +523,8 @@ public static function table(array $data, $title='Info List', $showBorder = true
519523

520524
self::write($headStr);
521525

522-
if ($showBorder) {
526+
// border: split head and body
527+
if (isset($border)) {
523528
self::write(' ' . $border);
524529
}
525530

@@ -543,7 +548,7 @@ public static function table(array $data, $title='Info List', $showBorder = true
543548
}
544549

545550
// output table bottom border
546-
if ($showBorder) {
551+
if (isset($border)) {
547552
self::write(' ' . $border);
548553
}
549554

@@ -600,9 +605,9 @@ public static function error($messages, $quit = false)
600605
{
601606
static::block($messages, 'ERROR', 'error', $quit);
602607
}
603-
public static function comment($messages, $quit = false)
608+
public static function notice($messages, $quit = false)
604609
{
605-
static::block($messages, 'COMMENT', 'comment', $quit);
610+
static::block($messages, 'NOTICE', 'comment', $quit);
606611
}
607612

608613
/////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)