Skip to content

Commit cf82404

Browse files
committed
some update, add new input class
1 parent 6127703 commit cf82404

File tree

6 files changed

+150
-26
lines changed

6 files changed

+150
-26
lines changed

src/IO/FixedInput.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/12/24 0024
6+
* Time: 18:39
7+
*/
8+
9+
namespace Inhere\Console\IO;
10+
11+
/**
12+
* Class FixedInput
13+
* - 初始化时不全部解析,只取出 '-h' '--help' 还有命令名
14+
* - 到运行命令时根据命令的参数选项配置(InputDefinition)来进行解析
15+
* - @todo un-completed
16+
* @package Inhere\Console\IO
17+
*/
18+
class FixedInput extends Input
19+
{
20+
/**
21+
* the prepare parsed options.
22+
* @see AbstractApplication::$internalOptions
23+
* @var array
24+
*/
25+
private $preParsed =[
26+
// opt name => has value
27+
'h' => false,
28+
'V' => false,
29+
'help' => false,
30+
'debug' => true,
31+
'profile' => false,
32+
'version' => false,
33+
];
34+
35+
/** @var array */
36+
private $cleanedTokens;
37+
38+
/**
39+
* FixedInput constructor.
40+
* @param null|array $argv
41+
*/
42+
public function __construct($argv = null)
43+
{
44+
if (null === $argv) {
45+
$argv = $_SERVER['argv'];
46+
}
47+
48+
parent::__construct($argv, false);
49+
50+
$copy = $argv;
51+
52+
// command name
53+
if (!empty($copy[1]) && $copy[1][0] !== '-' && false === strpos($copy[1], '=')) {
54+
$this->setCommand($copy[1]);
55+
56+
// unset command
57+
unset($copy[1]);
58+
}
59+
60+
// pop script name
61+
array_shift($copy);
62+
63+
$this->cleanedTokens = $copy;
64+
$this->collectPreParsed($copy);
65+
}
66+
67+
private function collectPreParsed(array $tokens)
68+
{
69+
foreach ($this->preParsed as $name => $hasVal) {
70+
71+
}
72+
}
73+
74+
/**
75+
* @param array $allowArray
76+
* @param array $noValues
77+
*/
78+
public function parseTokens(array $allowArray = [], array $noValues = [])
79+
{
80+
$params = $this->getTokens();
81+
array_shift($params); // pop script name
82+
}
83+
84+
/**
85+
* @return array
86+
*/
87+
public function getPreParsed(): array
88+
{
89+
return $this->preParsed;
90+
}
91+
92+
/**
93+
* @param array $preParsed
94+
*/
95+
public function setPreParsed(array $preParsed)
96+
{
97+
$this->preParsed = $preParsed;
98+
}
99+
100+
/**
101+
* @return array|null
102+
*/
103+
public function getCleanedTokens(): array
104+
{
105+
return $this->cleanedTokens;
106+
}
107+
108+
}

src/IO/Input.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@
88

99
namespace Inhere\Console\IO;
1010

11-
use Inhere\Console\Utils\CommandLineParse;
11+
use Inhere\Console\Utils\CommandLine;
1212

1313
/**
14-
* Class Input
14+
* Class Input - the input information. by parse global var $argv.
1515
* @package Inhere\Console\IO
1616
*/
1717
class Input implements InputInterface
1818
{
1919
/**
20-
* @var @resource
20+
* @var resource
2121
*/
22-
protected $inputStream = STDIN;
22+
protected $inputStream = \STDIN;
2323

2424
/**
25-
* @var
25+
* @var string
2626
*/
2727
private $pwd;
2828

2929
/**
30+
* eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
3031
* @var string
3132
*/
3233
private $fullScript;
@@ -72,23 +73,25 @@ class Input implements InputInterface
7273
/**
7374
* Input constructor.
7475
* @param null|array $argv
76+
* @param bool $parsing
7577
*/
76-
public function __construct($argv = null)
78+
public function __construct($argv = null, $parsing = true)
7779
{
7880
if (null === $argv) {
7981
$argv = $_SERVER['argv'];
8082
}
8183

8284
$this->pwd = $this->getPwd();
83-
85+
$this->tokens = $argv;
8486
$this->fullScript = implode(' ', $argv);
8587
$this->script = array_shift($argv);
86-
$this->tokens = $argv;
8788

88-
list($this->args, $this->sOpts, $this->lOpts) = CommandLineParse::byArgv($argv);
89+
if ($parsing) {
90+
list($this->args, $this->sOpts, $this->lOpts) = CommandLine::parseByArgv($argv);
8991

90-
// collect command `server`
91-
$this->command = isset($this->args[0]) ? array_shift($this->args) : null;
92+
// collect command. it is first argument.
93+
$this->command = isset($this->args[0]) ? array_shift($this->args) : null;
94+
}
9295
}
9396

9497
/**
@@ -98,11 +101,11 @@ public function __toString()
98101
{
99102
$tokens = array_map(function ($token) {
100103
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
101-
return $match[1] . CommandLineParse::escapeToken($match[2]);
104+
return $match[1] . CommandLine::escapeToken($match[2]);
102105
}
103106

104107
if ($token && $token[0] !== '-') {
105-
return CommandLineParse::escapeToken($token);
108+
return CommandLine::escapeToken($token);
106109
}
107110

108111
return $token;
@@ -641,4 +644,12 @@ public function getPwd(): string
641644

642645
return $this->pwd;
643646
}
647+
648+
/**
649+
* @return array
650+
*/
651+
public function getTokens(): array
652+
{
653+
return $this->tokens;
654+
}
644655
}

src/IO/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class Output implements OutputInterface
2525
* 正常输出流
2626
* Property outStream.
2727
*/
28-
protected $outputStream = STDOUT;
28+
protected $outputStream = \STDOUT;
2929

3030
/**
3131
* 错误输出流
3232
* Property errorStream.
3333
*/
34-
protected $errorStream = STDERR;
34+
protected $errorStream = \STDERR;
3535

3636
/**
3737
* 控制台窗口(字体/背景)颜色添加处理

src/Utils/CommandLineParse.php renamed to src/Utils/CommandLine.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Inhere\Console\Utils;
1010

1111
/**
12-
* Class CommandLineParse - console argument and option parse
12+
* Class CommandLine - console argument and option parse
1313
* @package Inhere\Console\Utils
1414
*/
15-
final class CommandLineParse
15+
final class CommandLine
1616
{
1717
/**
1818
* These words will be as a Boolean value
@@ -51,7 +51,7 @@ final class CommandLineParse
5151
* @param bool $mergeOpts Whether merge short-opts and long-opts
5252
* @return array
5353
*/
54-
public static function byArgv(array $params, array $noValues = [], $mergeOpts = false): array
54+
public static function parseByArgv(array $params, array $noValues = [], $mergeOpts = false): array
5555
{
5656
$args = $sOpts = $lOpts = [];
5757

@@ -125,11 +125,16 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
125125
return [$args, $sOpts, $lOpts];
126126
}
127127

128+
public static function parseByDefinition(array $tokens, array $allowArray = [], array $noValues = [])
129+
{
130+
131+
}
132+
128133
/**
129134
* parse custom array params
130135
*
131136
* ```php
132-
* $result = CommandLineParse::byArray([
137+
* $result = CommandLine::parseByArray([
133138
* 'arg' => 'val',
134139
* '--lp' => 'val2',
135140
* '--s' => 'val3',
@@ -139,7 +144,7 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
139144
* @param array $params
140145
* @return array
141146
*/
142-
public static function byArray(array $params)
147+
public static function parseByArray(array $params)
143148
{
144149
$args = $sOpts = $lOpts = [];
145150

@@ -163,12 +168,12 @@ public static function byArray(array $params)
163168
/**
164169
*
165170
* ```php
166-
* $result = CommandLineParse::byString('foo --bar="foobar"');
171+
* $result = CommandLine::parseByString('foo --bar="foobar"');
167172
* ```
168173
* @todo ...
169174
* @param string $string
170175
*/
171-
public static function byString(string $string)
176+
public static function parseByString(string $string)
172177
{
173178

174179
}

src/Utils/Helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public static function isRoot(): bool
6666
/**
6767
* @return bool
6868
*/
69-
public static function supportColor()
69+
public static function isSupportColor()
7070
{
71-
return self::isSupportColor();
71+
return self::supportColor();
7272
}
7373

7474
/**
@@ -77,7 +77,7 @@ public static function supportColor()
7777
* \Symfony\Component\Console\Output\OutputStream.
7878
* @return boolean
7979
*/
80-
public static function isSupportColor()
80+
public static function supportColor()
8181
{
8282
if (DIRECTORY_SEPARATOR === '\\') {
8383
return

src/Utils/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ public static function progressBar($total, array $opts = [])
10251025
{
10261026
$current = 0;
10271027
$finished = false;
1028-
$tplPrefix = Helper::isSupportColor() ? "\x0D\x1B[2K" : "\x0D\r";
1028+
$tplPrefix = Helper::supportColor() ? "\x0D\x1B[2K" : "\x0D\r";
10291029
$opts = array_merge([
10301030
'doneChar' => '=',
10311031
'waitChar' => ' ',

0 commit comments

Comments
 (0)