Skip to content

Commit ed8da95

Browse files
committed
input opt and args parse logic update
1 parent 94fec5c commit ed8da95

File tree

12 files changed

+531
-179
lines changed

12 files changed

+531
-179
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ $app = new App([], $input, $output);
2424
example(in terminal):
2525

2626
```
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
27+
$ examples/app home/useArg status=2 name=john arg0 -s=test --page=23 --id=23 --id=154 -e dev -v vvv -d -rf --debug --test=false
2828
```
2929

30+
**NOTICE:**
31+
32+
- These words will be as a Boolean(`true`) value: `on|yes|true`
33+
- These words will be as a Boolean(`false`) value: `off|no|false`
34+
3035
get command info:
3136

3237
```

examples/HomeController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace inhere\console\examples;
4+
35
use inhere\console\Controller;
46
use inhere\console\utils\Download;
57
use inhere\console\utils\Interact;

examples/TestCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Time: 18:58
77
*/
88

9+
namespace inhere\console\examples;
10+
911
use inhere\console\Command;
1012
use inhere\console\utils\AnsiCode;
1113

examples/app

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defined('STDERR') or define('STDERR', fopen('php://stderr', 'wb'));
88

99
define('PROJECT_PATH', dirname(__DIR__));
1010

11-
require dirname(__DIR__) . '/../../autoload.php';
11+
require __DIR__ . '/s-autoload.php';
1212

1313
// create app instance
1414
$app = new \inhere\console\App();

examples/baks/OldInput.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace inhere\console\examples\baks;
4+
5+
/**
6+
* Created by PhpStorm.
7+
* User: inhere
8+
* Date: 2017/5/29
9+
* Time: 下午10:16
10+
*/
11+
class OldInput
12+
{
13+
14+
/////////////////////////////////////////////////////////////////////////////////////////
15+
/// argument and option parser
16+
/////////////////////////////////////////////////////////////////////////////////////////
17+
18+
/**
19+
* @param bool $fillToGlobal
20+
* @return array
21+
*/
22+
public static function parseGlobalArgv($fillToGlobal = false)
23+
{
24+
// eg: `./bin/app server name=john city -s=test --page=23 -d -rf --debug`
25+
// eg: `php cli.php server name=john city -s=test --page=23 -d -rf --debug`
26+
global $argv;
27+
28+
$tmp = $argv;
29+
$fullScript = implode(' ', $tmp);
30+
$script = array_shift($tmp);
31+
32+
$args = $opts = [];
33+
34+
// parse query params
35+
// `./bin/app server start name=john city=chengdu -s=test --page=23 -d -rf --debug --task=off`
36+
// parse to
37+
// $args = [ 'name' => 'john', 0 => 'start', 'city' => 'chengdu' ];
38+
// $opts = [ 'd' => true, 'f' => true, 'r' => true, 's' => 'test', 'debug' => true, 'task' => false ]
39+
if ($tmp) {
40+
foreach ($tmp as $item) {
41+
// is a option
42+
if ($item{0} === '-') {
43+
static::parseOption($item, $opts);
44+
45+
// is a argument
46+
} else {
47+
static::parseArgument($item, $args);
48+
}
49+
}
50+
51+
if ($fillToGlobal) {
52+
$_REQUEST = $_GET = $args;
53+
}
54+
}
55+
56+
// collect command `server`
57+
$command = isset($args[0]) ? array_shift($args) : '';
58+
59+
unset($tmp);
60+
return [$fullScript, $script, $command, $args, $opts];
61+
}
62+
63+
/**
64+
* will parse option
65+
*
66+
* eg: `-s=test --page=23 -d -rf --debug --task=false --id=23 --id=154`
67+
*
68+
* to:
69+
*
70+
* ```
71+
* $opts = [
72+
* 'd' => true,
73+
* 'f' => true,
74+
* 'r' => true,
75+
* 's' => 'test',
76+
* 'debug' => true,
77+
* 'task' => false
78+
* ]
79+
* ```
80+
* @param $item
81+
* @param $opts
82+
*/
83+
protected static function parseOption($item, &$opts)
84+
{
85+
// is a have value option. eg: `-s=test --page=23`
86+
if (strpos($item, '=')) {
87+
$item = trim($item, '-= ');
88+
[$name, $val] = explode('=', $item);
89+
$tVal = strtolower($val);
90+
91+
// check it is a bool value.
92+
if ($tVal === 'on' || $tVal === 'yes' || $tVal === 'true') {
93+
$opts[$name] = true;
94+
} elseif ($tVal === 'off' || $tVal === 'no' || $tVal === 'false') {
95+
$opts[$name] = false;
96+
97+
// is array. eg: `--id=23 --id=154`
98+
} elseif (isset($opts[$name])) {
99+
if (is_array($opts[$name])) {
100+
$opts[$name][] = $val;
101+
102+
// expect bool option. so not use `else`
103+
} elseif (is_string($opts[$name])) {
104+
$prev = $opts[$name];
105+
$opts[$name] = [$prev, $val];
106+
}
107+
} else {
108+
$opts[$name] = $val;
109+
}
110+
111+
// is a no value option
112+
} else {
113+
// is a short option. eg: `-d -rf`
114+
if ($item{1} !== '-') {
115+
$item = trim($item, '-');
116+
foreach (str_split($item) as $char) {
117+
$opts[$char] = true;
118+
}
119+
120+
// is a long option. eg: `--debug`
121+
} else {
122+
$item = trim($item, '-');
123+
$opts[$item] = true;
124+
}
125+
}
126+
}
127+
128+
/**
129+
* parse argument list
130+
*
131+
* eg: `start name=john name=tom city=chengdu`
132+
*
133+
* to:
134+
*
135+
* ```
136+
* [ 'name' => ['john', 'tom'], 0 => 'start', 'city' => 'chengdu' ];
137+
* ```
138+
*
139+
* @param $item
140+
* @param array $args
141+
*/
142+
protected static function parseArgument($item, &$args)
143+
{
144+
$item = trim($item, '= ');
145+
146+
// eg: `name=john`
147+
if (strpos($item, '=')) {
148+
[$name, $val] = explode('=', $item);
149+
150+
// is array. eg: `name=john name=tom`
151+
if (isset($args[$name])) {
152+
if (is_array($args[$name])) {
153+
$args[$name][] = $val;
154+
} else {
155+
$prev = $args[$name];
156+
$args[$name] = [$prev, $val];
157+
}
158+
} else {
159+
$args[$name] = $val;
160+
}
161+
162+
// only value. eg: `city`
163+
} else {
164+
$args[] = $item;
165+
}
166+
}
167+
}

examples/cli-routes.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
* @var inhere\console\App $app
99
*/
1010

11-
require 'TestCommand.php';
12-
require 'HomeController.php';
11+
use inhere\console\examples\HomeController;
12+
use inhere\console\examples\TestCommand;
13+
use inhere\console\io\Input;
14+
use inhere\console\io\Output;
1315

14-
$app->command('demo', function (\inhere\console\io\Input $in, \inhere\console\io\Output $out) {
16+
$app->command('demo', function (Input $in, Output $out) {
1517
$cmd = $in->getCommand();
1618

1719
$out->info('hello, this is a test command: ' . $cmd);

examples/opt_arg_parse.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/5/29
6+
* Time: 下午9:29
7+
*/
8+
use inhere\console\examples\baks\OldInput;
9+
use inhere\console\Helper;
10+
use inhere\console\io\Input;
11+
12+
require __DIR__ . '/s-autoload.php';
13+
14+
echo Helper::dumpVar(new Input());die;
15+
16+
//$ret1 = OldInput::parseGlobalArgv();
17+
$ret2 = Input::parseOptArgs();
18+
19+
//echo "parseGlobalArgv:\n" . Helper::dumpVar($ret1);
20+
21+
echo "parseOptArgs:\n" . Helper::dumpVar($ret2);

examples/s-autoload.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/5/28
6+
* Time: 下午10:36
7+
*/
8+
9+
error_reporting(E_ALL | E_STRICT);
10+
date_default_timezone_set('Asia/Shanghai');
11+
12+
spl_autoload_register(function($class)
13+
{
14+
if (0 === strpos($class,'inhere\console\examples\\')) {
15+
$path = str_replace('\\', '/', substr($class, strlen('inhere\console\examples\\')));
16+
$file =__DIR__ . "/{$path}.php";
17+
18+
if (is_file($file)) {
19+
include $file;
20+
}
21+
22+
} elseif (0 === strpos($class,'inhere\console\\')) {
23+
$path = str_replace('\\', '/', substr($class, strlen('inhere\console\\')));
24+
$file = dirname(__DIR__) . "/src/{$path}.php";
25+
26+
if (is_file($file)) {
27+
include $file;
28+
}
29+
}
30+
});

src/Helper.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,37 @@ public static function wrapText($text, $indent = 0, $width = 0)
335335

336336
return $pad . ' ' . implode("\n", $lines);
337337
}
338+
339+
340+
/**
341+
* dump vars
342+
* @param array ...$args
343+
* @return string
344+
*/
345+
public static function dumpVar(...$args)
346+
{
347+
ob_start();
348+
var_dump(...$args);
349+
$string = ob_get_clean();
350+
351+
return preg_replace("/=>\n\s+/", '=> ', $string);
352+
}
353+
354+
/**
355+
* print vars
356+
* @param array ...$args
357+
* @return string
358+
*/
359+
public static function printR(...$args)
360+
{
361+
ob_start();
362+
363+
foreach ($args as $arg) {
364+
print_r($arg);
365+
}
366+
367+
$string = ob_get_clean();
368+
369+
return preg_replace("/Array\n\s+\(/", 'Array (', $string);
370+
}
338371
}

0 commit comments

Comments
 (0)