Skip to content

Commit f80f057

Browse files
committed
some update, some bug fixed for parse input argv
1 parent c259f1b commit f80f057

File tree

9 files changed

+264
-177
lines changed

9 files changed

+264
-177
lines changed

README.md

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,76 @@ $app = new App([], $input, $output);
2424
example(in terminal):
2525

2626
```
27-
./bin/app image/packTask name=john -d -s=test --debug=true
28-
php bin/cli.php start name=john -d -s=test --debug=true
27+
$ examples/app home/useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
2928
```
3029

3130
get command info:
3231

3332
```
34-
$script = $input->getScript(); // './bin/app' 'bin/cli.php'
35-
$command = $input->getCommand(); // 'image/packTask' 'start'
33+
echo $input->getScript(); // 'examples/app'
34+
echo $input->getCommand(); // 'home/useArg'
35+
```
36+
37+
get parsed arguments:
38+
39+
```php
40+
var_dump($input->getArgs());
41+
```
42+
43+
output:
44+
45+
```php
46+
array(3) {
47+
'status' => string(1) "2"
48+
'name' => array(3) {
49+
[0] => string(4) "john"
50+
[1] => string(3) "tom"
51+
[2] => string(4) "jack"
52+
}
53+
[0] => string(4) "arg0"
54+
}
55+
```
56+
57+
get parsed options:
58+
59+
```php
60+
var_dump($input->getOpts());
61+
```
62+
63+
output:
64+
65+
```php
66+
array(8) {
67+
's' => string(4) "test"
68+
'page' => string(2) "23"
69+
'id' => array(3) {
70+
[0] => string(2) "23"
71+
[1] => string(3) "154"
72+
[2] => string(3) "456"
73+
}
74+
'd' => bool(true)
75+
'r' => bool(true)
76+
'f' => bool(true)
77+
'debug' => bool(true)
78+
'test' => bool(false)
79+
}
80+
```
81+
82+
more method:
83+
84+
```php
3685

3786
// argument
38-
$name = $input->get('name', 'default'); // 'john'
39-
$s = $input->get('s', 'default'); // 'test'
87+
$first = $input->getFirstArg(); // 'arg0'
88+
$status = $input->get('status', 'default'); // '2'
4089

4190
// option
42-
$d = $input->getBool('d') // True
43-
$debug = $input->getBool('debug') // True
44-
$noexists = $input->getBool('noexists') // False
91+
$page = $input->getOpt('page') // '23'
92+
$debug = $input->boolOpt('debug') // True
93+
$test = $input->boolOpt('test') // False
4594
```
4695

47-
get user input:
96+
### get user input:
4897

4998
```
5099
echo "Your name:";
@@ -183,4 +232,6 @@ var_dump($result); // bool(false)
183232
184233
```
185234

186-
## formatted output
235+
## License
236+
237+
MIT

examples/HomeController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ public function fmtMsgCommand()
9191
* a example for use arguments on command
9292
* @usage home/useArg [arg1=val1 arg2=arg2] [options]
9393
* @example home/useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false
94+
* home/useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
9495
*/
9596
public function useArgCommand()
9697
{
9798
$this->write('input arguments:');
98-
var_dump($this->input->get());
99+
var_dump($this->input->getArgs());
99100

100101
$this->write('input options:');
101102
var_dump($this->input->getOpts());

src/AbstractApp.php

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
use inhere\console\io\Input;
1212
use inhere\console\io\Output;
13+
use inhere\console\utils\TraitInputOutput;
1314

1415
/**
1516
* Class AbstractApp
1617
* @package inhere\console
1718
*/
1819
abstract class AbstractApp
1920
{
21+
use TraitInputOutput;
22+
2023
// event name list
2124
const ON_BEFORE_RUN = 'beforeRun';
2225
const ON_AFTER_RUN = 'afterRun';
@@ -47,16 +50,6 @@ abstract class AbstractApp
4750
'timeZone' => 'Asia/Shanghai',
4851
];
4952

50-
/**
51-
* @var Input
52-
*/
53-
public $input;
54-
55-
/**
56-
* @var Output
57-
*/
58-
public $output;
59-
6053
/**
6154
* @var array
6255
*/
@@ -161,7 +154,7 @@ public function stop($code = 0)
161154
* @param string $controller The controller class
162155
* @return static
163156
*/
164-
public function controller($name, $controller)
157+
public function controller(string $name, string $controller)
165158
{
166159
if (!$name || !$controller) {
167160
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
@@ -194,7 +187,7 @@ public function controllers(array $controllers)
194187
* @param string|\Closure $handler
195188
* @return $this
196189
*/
197-
public function command($name, $handler)
190+
public function command(string $name, $handler)
198191
{
199192
if (!$name || !$handler) {
200193
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
@@ -221,7 +214,7 @@ public function commands(array $commands)
221214
/**
222215
* @return array
223216
*/
224-
public static function hooks()
217+
public static function hooks(): array
225218
{
226219
return array_keys(self::$hooks);
227220
}
@@ -230,7 +223,7 @@ public static function hooks()
230223
* @param $event
231224
* @param callable $handler
232225
*/
233-
public function on($event, callable $handler)
226+
public function on(string $event, callable $handler)
234227
{
235228
if (isset(self::$hooks[$event])) {
236229
self::$hooks[$event] = $handler;
@@ -240,7 +233,7 @@ public function on($event, callable $handler)
240233
/**
241234
* @return array
242235
*/
243-
public function getInternalCommands()
236+
public function getInternalCommands(): array
244237
{
245238
return $this->internalCommands;
246239
}
@@ -249,7 +242,7 @@ public function getInternalCommands()
249242
* @param $name
250243
* @return bool
251244
*/
252-
public function isInternalCommand($name)
245+
public function isInternalCommand(string $name): bool
253246
{
254247
return isset($this->internalCommands[$name]);
255248
}
@@ -303,7 +296,7 @@ public function setConfig(array $config)
303296
* get config
304297
* @return array
305298
*/
306-
public function getConfig()
299+
public function getConfig(): array
307300
{
308301
return $this->config;
309302
}
@@ -312,44 +305,16 @@ public function getConfig()
312305
* is Debug
313306
* @return boolean
314307
*/
315-
public function isDebug()
308+
public function isDebug(): bool
316309
{
317310
return (bool)$this->config['debug'];
318311
}
319312

320313
/**
321-
* @return Input
322-
*/
323-
public function getInput()
324-
{
325-
return $this->input;
326-
}
327-
328-
/**
329-
* @param Input $input
330-
*/
331-
public function setInput(Input $input)
332-
{
333-
$this->input = $input;
334-
}
335-
336-
/**
337-
* @return Output
338-
*/
339-
public function getOutput()
340-
{
341-
return $this->output;
342-
}
343-
344-
/**
345-
* @param Output $output
314+
* @param $name
315+
* @param bool $isGroup
346316
*/
347-
public function setOutput(Output $output)
348-
{
349-
$this->output = $output;
350-
}
351-
352-
protected function checkName($name, $isGroup = false)
317+
protected function checkName(string $name, $isGroup = false)
353318
{
354319
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
355320

src/AbstractCommand.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace inhere\console;
1010

11+
use inhere\console\io\Input;
12+
use inhere\console\io\Output;
1113
use inhere\console\utils\TraitInputOutput;
1214
use inhere\console\utils\TraitInteract;
1315

@@ -36,28 +38,39 @@ abstract class AbstractCommand
3638
*/
3739
protected static $allowTags = ['description', 'usage', 'example'];
3840

41+
/**
42+
* Command constructor.
43+
* @param Input $input
44+
* @param Output $output
45+
*/
46+
public function __construct(Input $input, Output $output)
47+
{
48+
$this->input = $input;
49+
$this->output = $output;
50+
}
51+
3952
abstract public function run($arg = '');
4053

4154
/**
4255
* @param string $name
4356
*/
44-
public function setName($name)
57+
public function setName(string $name)
4558
{
4659
$this->name = $name;
4760
}
4861

4962
/**
5063
* @return string
5164
*/
52-
public function getName()
65+
public function getName(): string
5366
{
5467
return $this->name;
5568
}
5669

5770
/**
5871
* @return array
5972
*/
60-
public static function getAllowTags()
73+
public static function getAllowTags(): array
6174
{
6275
return self::$allowTags;
6376
}

src/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function showVersionInfo($quit = true)
232232
*/
233233
public function showCommandList($quit = true)
234234
{
235-
$script = $this->input->getScriptName();
235+
$script = $this->getScriptName();
236236
$controllerArr = $commandArr = [];
237237

238238
// built in commands

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function run($name = '')
3131
{
3232
$this->setName($name);
3333

34-
if ($this->input->getBool('h') || $this->input->getBool('help')) {
34+
if ($this->input->boolOpt('h') || $this->input->boolOpt('help')) {
3535
return $this->showHelp();
3636
}
3737

src/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class Controller extends AbstractCommand
3737
*/
3838
public function run($action = '')
3939
{
40-
$showCmdHelp = $action && ($this->input->getBool('h') || $this->input->getBool('help'));
40+
$showCmdHelp = $action && ($this->input->boolOpt('h') || $this->input->boolOpt('help'));
4141

4242
if ($showCmdHelp) {
4343
return $this->helpCommand($action);

0 commit comments

Comments
 (0)