Skip to content

Commit 6b0881c

Browse files
committed
add some new idea for flags parse
1 parent 5181a37 commit 6b0881c

File tree

3 files changed

+155
-54
lines changed

3 files changed

+155
-54
lines changed

src/Flag/Flags.php

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Inhere\Console\Exception\FlagException;
77
use Inhere\Console\Flag\Traits\FlagArgumentsTrait;
88
use Inhere\Console\Flag\Traits\FlagOptionsTrait;
9+
use Inhere\Console\Flag\Traits\FlagParsingTrait;
10+
use Toolkit\Stdlib\Obj\AbstractObj;
911
use function array_shift;
1012
use function count;
1113
use function ltrim;
@@ -17,10 +19,11 @@
1719
*
1820
* @package Inhere\Console\Flag
1921
*/
20-
class Flags
22+
class Flags extends AbstractObj
2123
{
2224
use FlagArgumentsTrait;
2325
use FlagOptionsTrait;
26+
use FlagParsingTrait;
2427
use NameAliasTrait;
2528

2629
/**
@@ -33,38 +36,11 @@ class Flags
3336
*/
3437
private $helpRenderer;
3538

36-
/**
37-
* @var bool
38-
*/
39-
private $parsed = false;
40-
4139
/**
4240
* @var bool
4341
*/
4442
private $autoBindArgs = false;
4543

46-
/**
47-
* The raw input args
48-
*
49-
* @var array
50-
*/
51-
private $rawArgs = [];
52-
53-
/**
54-
* The remaining args on parsed
55-
*
56-
* @var array
57-
*/
58-
private $args = [];
59-
60-
/**
61-
* @return $this
62-
*/
63-
public static function new(): self
64-
{
65-
return new self();
66-
}
67-
6844
/**
6945
* @return $this
7046
*/
@@ -143,7 +119,7 @@ public function parse(array $args = null): array
143119
* - found `-h|--help` flag
144120
* - found first arg(not an option)
145121
*
146-
* @return array [bool, status]
122+
* @return array [goon: bool, status: int]
147123
*/
148124
protected function parseOne(): array
149125
{
@@ -166,6 +142,11 @@ protected function parseOne(): array
166142
return [false, self::STATUS_OK];
167143
}
168144

145+
// NOTICE: will stop parse option on found '--'
146+
if ($arg === '--') {
147+
return [false, self::STATUS_OK];
148+
}
149+
169150
$name = ltrim($arg, '-');
170151

171152
// invalid arg. eg: '--' // ignore
@@ -209,7 +190,7 @@ protected function parseOne(): array
209190
if (!$hasVal && count($this->args) > 0) {
210191
// value is next arg
211192
$hasVal = true;
212-
$ntArg = $this->args[0];
193+
$ntArg = $this->args[0];
213194

214195
// is not an option value.
215196
if ($ntArg[0] === '-') {
@@ -242,7 +223,7 @@ public function reset(bool $clearDefined = false): void
242223
}
243224

244225
// clear match results
245-
$this->parsed = false;
226+
$this->parsed = false;
246227
$this->matched = [];
247228
$this->rawArgs = $this->args = [];
248229
}
@@ -316,22 +297,6 @@ public function setHelpRenderer(callable $helpRenderer): void
316297
$this->helpRenderer = $helpRenderer;
317298
}
318299

319-
/**
320-
* @return array
321-
*/
322-
public function getRawArgs(): array
323-
{
324-
return $this->rawArgs;
325-
}
326-
327-
/**
328-
* @return array
329-
*/
330-
public function getArgs(): array
331-
{
332-
return $this->args;
333-
}
334-
335300
/**
336301
* @return bool
337302
*/
@@ -348,11 +313,4 @@ public function setAutoBindArgs(bool $autoBindArgs): void
348313
$this->autoBindArgs = $autoBindArgs;
349314
}
350315

351-
/**
352-
* @return bool
353-
*/
354-
public function isParsed(): bool
355-
{
356-
return $this->parsed;
357-
}
358316
}

src/Flag/SFlags.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Flag;
4+
5+
use Inhere\Console\Flag\Traits\FlagParsingTrait;
6+
use Toolkit\Stdlib\Obj\AbstractObj;
7+
use function array_merge;
8+
9+
/**
10+
* Class SFlags
11+
* @package Inhere\Console\Flag
12+
*/
13+
class SFlags extends AbstractObj
14+
{
15+
use FlagParsingTrait;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $settings = [
21+
// List of parameters without values(bool option keys)
22+
'boolOpts' => [], // ['debug', 'h']
23+
// Whether merge short-opts and long-opts
24+
'mergeOpts' => false,
25+
// Only want parsed options.
26+
// if not empty, will ignore no matched
27+
'wantParsedOpts' => [],
28+
// List of option allow array values.
29+
'arrayOpts' => [], // ['names', 'status']
30+
// Special short style
31+
// posix: -abc will expand: -a -b -c
32+
// unix: -abc will expand: -a=bc
33+
'shortStyle' => 'posix',
34+
];
35+
36+
/**
37+
* @param array $rawArgs
38+
* @param array $settings
39+
*
40+
* @return array
41+
*/
42+
public function parse(array $rawArgs, array $settings = []): array
43+
{
44+
$this->setSettings($settings);
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function getSettings(): array
51+
{
52+
return $this->settings;
53+
}
54+
55+
/**
56+
* @param array $settings
57+
*/
58+
public function setSettings(array $settings): void
59+
{
60+
$this->settings = array_merge($this->settings, $settings);
61+
}
62+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Flag\Traits;
4+
5+
/**
6+
* Trait FlagParsingTrait
7+
* @package Inhere\Console\Flag\Traits
8+
*/
9+
trait FlagParsingTrait
10+
{
11+
/**
12+
* @var bool
13+
*/
14+
private $parsed = false;
15+
16+
/**
17+
* Whether stop parse option on first argument
18+
*
19+
* @var bool
20+
*/
21+
// private $stopOnNoOption = true;
22+
// private $stopOnNoOpt = true;
23+
private $stopOnArg = true;
24+
25+
/**
26+
* The remaining args on option parsed
27+
*
28+
* @var array
29+
*/
30+
private $args = [];
31+
32+
/**
33+
* The raw input args
34+
*
35+
* @var array
36+
*/
37+
private $rawArgs = [];
38+
39+
/**
40+
* @return array
41+
*/
42+
public function getArgs(): array
43+
{
44+
return $this->args;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function getRawArgs(): array
51+
{
52+
return $this->rawArgs;
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
public function isParsed(): bool
59+
{
60+
return $this->parsed;
61+
}
62+
63+
/**
64+
* @return bool
65+
*/
66+
public function isStopOnArg(): bool
67+
{
68+
return $this->stopOnArg;
69+
}
70+
71+
/**
72+
* @param bool $stopOnArg
73+
*
74+
* @return static
75+
*/
76+
public function setStopOnArg(bool $stopOnArg): self
77+
{
78+
$this->stopOnArg = $stopOnArg;
79+
return $this;
80+
}
81+
}

0 commit comments

Comments
 (0)