Skip to content

Commit d354fed

Browse files
committed
fix error on set some prop
1 parent a458692 commit d354fed

File tree

6 files changed

+392
-209
lines changed

6 files changed

+392
-209
lines changed

example/not-stop_on_first.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
// run: php example/not-stop_on_first.php
3+
4+
use Toolkit\PFlag\Flags;
5+
6+
require dirname(__DIR__) . '/test/bootstrap.php';
7+
8+
$fs = Flags::new();
9+
10+
$fs->addOptsByRules([
11+
'name' => 'string',
12+
'age' => 'int',
13+
]);
14+
$flags = ['--name', 'inhere', '--age', '90', 'arg0', 'arg1'];
15+
16+
// set stopOnFirstArg=false
17+
$fs->setStopOnFistArg(false);
18+
19+
$fs->parse($flags);
20+
vdump($fs->toArray());
21+
22+
$fs->resetResults();
23+
24+
// move an arg in middle
25+
$flags1 = ['--name', 'INHERE', 'arg0', '--age', '980', 'arg1'];
26+
27+
// will skip 'arg0' and continue parse '--age', '90'
28+
$fs->parse($flags1);
29+
vdump($fs->toArray());

src/AbstractFlags.php

Lines changed: 187 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ abstract class AbstractFlags implements ParserInterface
5656

5757
public const STATUS_HELP = 3; // found `-h|--help` flag
5858

59-
public const SHORT_STYLE_GUN = 'gnu';
60-
59+
/**
60+
* Special short option style
61+
*
62+
* - gnu: `-abc` will expand: `-a -b -c`
63+
* - posix: `-abc` will expand: `-a=bc`
64+
*/
65+
public const SHORT_STYLE_GUN = 'gnu';
6166
public const SHORT_STYLE_POSIX = 'posix';
6267

6368
public const DEFINE_ITEM = [
@@ -141,37 +146,38 @@ abstract class AbstractFlags implements ParserInterface
141146
'descNlOnOptLen' => self::OPT_MAX_WIDTH,
142147
];
143148

144-
// -------------------- settings for parse --------------------
149+
// -------------------- settings for parse option --------------------
145150

146151
/**
147-
* Special short style
148-
* gnu: `-abc` will expand: `-a -b -c`
149-
* posix: `-abc` will expand: `-a=bc`
152+
* Special short option style
153+
*
154+
* - gnu: `-abc` will expand: `-a -b -c`
155+
* - posix: `-abc` will expand: `-a=bc`
150156
*
151157
* @var string
152158
*/
153-
protected $shortStyle = 'posix';
159+
protected $shortStyle = self::SHORT_STYLE_GUN;
154160

155161
/**
156162
* Stop parse option on found first argument.
157-
* - useful for support multi commands. eg: `top --opt ... sub --opt ...`
163+
*
164+
* - Useful for support multi commands. eg: `top --opt ... sub --opt ...`
158165
*
159166
* @var bool
160167
*/
161168
protected $stopOnFistArg = true;
162169

163-
protected $errOnUndefined = false;
164-
165170
/**
166-
* Whether stop parse option on found undefined option
171+
* Skip on found undefined option.
172+
*
173+
* - FALSE will throw FlagException error.
174+
* - TRUE will skip it and collect as raw arg, then continue parse next.
167175
*
168176
* @var bool
169177
*/
170-
protected $stopOnUndefined = true;
171-
172-
protected $skipUndefined = false;
178+
protected $skipOnUndefined = false;
173179

174-
protected $ignoreUnknown = false;
180+
// -------------------- settings for render help --------------------
175181

176182
/**
177183
* Auto render help on provide '-h', '--help'
@@ -194,6 +200,52 @@ abstract class AbstractFlags implements ParserInterface
194200
*/
195201
protected $helpRenderer;
196202

203+
// -------------------- rules --------------------
204+
205+
/**
206+
* The options rules
207+
* - type see FlagType::*
208+
*
209+
* ```php
210+
* [
211+
* // v: only value, as name and use default type FlagType::STRING
212+
* // k-v: key is name, value can be string|array
213+
* // - string value is rule(format: 'type;required;default;desc').
214+
* // - array is define item self::DEFINE_ITEM
215+
* 'long,s',
216+
* // name => rule
217+
* // TIP: name 'long,s' - first is the option name. remaining is shorts.
218+
* 'long,s' => int,
219+
* 'f' => bool,
220+
* 'long' => string,
221+
* 'tags' => array, // can also: ints, strings
222+
* 'name' => 'type;required;default;the description message', // with default, desc, required
223+
* ]
224+
* ```
225+
*
226+
* @var array
227+
*/
228+
protected $optRules = [];
229+
230+
/**
231+
* The arguments rules
232+
*
233+
* ```php
234+
* [
235+
* // v: only value, as rule - use default type FlagType::STRING
236+
* // k-v: key is name, value is rule(format: 'type;required;default;desc').
237+
* // - type see FlagType::*
238+
* 'type',
239+
* 'name' => 'type',
240+
* 'name' => 'type;required', // arg option
241+
* 'name' => 'type;required;default;the description message', // with default, desc, required
242+
* ]
243+
* ```
244+
*
245+
* @var array
246+
*/
247+
protected $argRules = [];
248+
197249
/**
198250
* Class constructor.
199251
*
@@ -652,6 +704,110 @@ protected function parseRuleOptName(string $key): array
652704
return [$name, $shorts];
653705
}
654706

707+
/****************************************************************
708+
* add rule methods
709+
***************************************************************/
710+
711+
/**
712+
* @param array $rules
713+
*/
714+
public function addOptsByRules(array $rules): void
715+
{
716+
foreach ($rules as $name => $rule) {
717+
$this->addOptByRule($name, $rule);
718+
}
719+
}
720+
721+
/**
722+
* Add and option by rule
723+
*
724+
* rule:
725+
* - string is rule string. (format: 'type;required;default;desc').
726+
* - array is define item {@see Flags::DEFINE_ITEM}
727+
*
728+
* @param string $name
729+
* @param string|array $rule
730+
*
731+
* @return $this
732+
*/
733+
public function addOptByRule(string $name, $rule): self
734+
{
735+
$this->optRules[$name] = $rule;
736+
737+
return $this;
738+
}
739+
740+
/**
741+
* @return array
742+
*/
743+
public function getOptRules(): array
744+
{
745+
return $this->optRules;
746+
}
747+
748+
/**
749+
* @param array $optRules
750+
*
751+
* @see optRules
752+
*/
753+
public function setOptRules(array $optRules): void
754+
{
755+
$this->optRules = $optRules;
756+
}
757+
758+
/**
759+
* @return array
760+
*/
761+
public function getArgRules(): array
762+
{
763+
return $this->argRules;
764+
}
765+
766+
/**
767+
* @param array $argRules
768+
*
769+
* @see argRules
770+
*/
771+
public function setArgRules(array $argRules): void
772+
{
773+
$this->argRules = $argRules;
774+
}
775+
776+
/**
777+
* @param array $rules
778+
*
779+
* @see addArgByRule()
780+
*/
781+
public function addArgsByRules(array $rules): void
782+
{
783+
foreach ($rules as $name => $rule) {
784+
$this->addArgByRule($name, $rule);
785+
}
786+
}
787+
788+
/**
789+
* Add and argument by rule
790+
*
791+
* rule:
792+
* - string is rule string. (format: 'type;required;default;desc')
793+
* - array is define item {@see Flags::DEFINE_ITEM}
794+
*
795+
* @param string $name
796+
* @param string|array $rule
797+
*
798+
* @return $this
799+
*/
800+
public function addArgByRule(string $name, $rule): self
801+
{
802+
if ($name) {
803+
$this->argRules[$name] = $rule;
804+
} else {
805+
$this->argRules[] = $rule;
806+
}
807+
808+
return $this;
809+
}
810+
655811
/****************************************************************
656812
* getter/setter methods
657813
***************************************************************/
@@ -744,6 +900,22 @@ public function setStopOnFistArg(bool $stopOnFistArg): void
744900
$this->stopOnFistArg = $stopOnFistArg;
745901
}
746902

903+
/**
904+
* @return bool
905+
*/
906+
public function isSkipOnUndefined(): bool
907+
{
908+
return $this->skipOnUndefined;
909+
}
910+
911+
/**
912+
* @param bool $skipOnUndefined
913+
*/
914+
public function setSkipOnUndefined(bool $skipOnUndefined): void
915+
{
916+
$this->skipOnUndefined = $skipOnUndefined;
917+
}
918+
747919
/**
748920
* @return string
749921
*/

0 commit comments

Comments
 (0)