@@ -56,8 +56,13 @@ abstract class AbstractFlags implements ParserInterface
56
56
57
57
public const STATUS_HELP = 3 ; // found `-h|--help` flag
58
58
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 ' ;
61
66
public const SHORT_STYLE_POSIX = 'posix ' ;
62
67
63
68
public const DEFINE_ITEM = [
@@ -141,37 +146,38 @@ abstract class AbstractFlags implements ParserInterface
141
146
'descNlOnOptLen ' => self ::OPT_MAX_WIDTH ,
142
147
];
143
148
144
- // -------------------- settings for parse --------------------
149
+ // -------------------- settings for parse option --------------------
145
150
146
151
/**
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`
150
156
*
151
157
* @var string
152
158
*/
153
- protected $ shortStyle = ' posix ' ;
159
+ protected $ shortStyle = self :: SHORT_STYLE_GUN ;
154
160
155
161
/**
156
162
* 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 ...`
158
165
*
159
166
* @var bool
160
167
*/
161
168
protected $ stopOnFistArg = true ;
162
169
163
- protected $ errOnUndefined = false ;
164
-
165
170
/**
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.
167
175
*
168
176
* @var bool
169
177
*/
170
- protected $ stopOnUndefined = true ;
171
-
172
- protected $ skipUndefined = false ;
178
+ protected $ skipOnUndefined = false ;
173
179
174
- protected $ ignoreUnknown = false ;
180
+ // -------------------- settings for render help --------------------
175
181
176
182
/**
177
183
* Auto render help on provide '-h', '--help'
@@ -194,6 +200,52 @@ abstract class AbstractFlags implements ParserInterface
194
200
*/
195
201
protected $ helpRenderer ;
196
202
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
+
197
249
/**
198
250
* Class constructor.
199
251
*
@@ -652,6 +704,110 @@ protected function parseRuleOptName(string $key): array
652
704
return [$ name , $ shorts ];
653
705
}
654
706
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
+
655
811
/****************************************************************
656
812
* getter/setter methods
657
813
***************************************************************/
@@ -744,6 +900,22 @@ public function setStopOnFistArg(bool $stopOnFistArg): void
744
900
$ this ->stopOnFistArg = $ stopOnFistArg ;
745
901
}
746
902
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
+
747
919
/**
748
920
* @return string
749
921
*/
0 commit comments