5
5
/**
6
6
* Class Options
7
7
*
8
- * Parses command line options passed to the CLI script. Allows CLI scripts to easily register all accepted options and
9
- * commands and even generates a help text from this setup.
8
+ * Parses command line options passed to the CLI script. Allows CLI
9
+ * scripts to easily register all accepted options and commands and
10
+ * even generates a help text from this setup.
10
11
*
11
12
* @author Andreas Gohr <andi@splitbrain.org>
12
13
* @license MIT
@@ -62,7 +63,7 @@ public function __construct( Colors $colors = null ) {
62
63
*
63
64
* @param string $help
64
65
*/
65
- public function setHelp ( $ help ) {
66
+ public function setHelp ( $ help ) : void {
66
67
$ this ->setup ['' ]['help ' ] = $ help ;
67
68
}
68
69
@@ -71,7 +72,7 @@ public function setHelp( $help ) {
71
72
*
72
73
* @param string $help
73
74
*/
74
- public function setCommandHelp ( $ help ) {
75
+ public function setCommandHelp ( $ help ) : void {
75
76
$ this ->setup ['' ]['commandhelp ' ] = $ help ;
76
77
}
77
78
@@ -80,12 +81,13 @@ public function setCommandHelp( $help ) {
80
81
*
81
82
* @param bool $compact
82
83
*/
83
- public function setCompactHelp ( $ compact = true ) {
84
+ public function setCompactHelp ( $ compact = true ) : void {
84
85
$ this ->setup ['' ]['compacthelp ' ] = $ compact ;
85
86
}
86
87
87
88
/**
88
- * Register the names of arguments for help generation and number checking
89
+ * Register the names of arguments for help generation and number
90
+ * checking
89
91
*
90
92
* This has to be called in the order arguments are expected
91
93
*
@@ -95,7 +97,12 @@ public function setCompactHelp( $compact = true ) {
95
97
* @param string $command if theses apply to a sub command only
96
98
* @throws Exception
97
99
*/
98
- public function registerArgument ( $ arg , $ help , $ required = true , $ command = '' ) {
100
+ public function registerArgument (
101
+ string $ arg ,
102
+ string $ help ,
103
+ bool $ required = true ,
104
+ string $ command = ''
105
+ ) :void {
99
106
if ( !isset ( $ this ->setup [$ command ] ) ) {
100
107
throw new Exception ( "Command $ command not registered " );
101
108
}
@@ -110,13 +117,14 @@ public function registerArgument( $arg, $help, $required = true, $command = '' )
110
117
/**
111
118
* This registers a sub command
112
119
*
113
- * Sub commands have their own options and use their own function (not main()).
120
+ * Sub commands have their own options and use their own function
121
+ * (not main()).
114
122
*
115
123
* @param string $command
116
124
* @param string $help
117
125
* @throws Exception
118
126
*/
119
- public function registerCommand ( $ command , $ help ) {
127
+ public function registerCommand ( $ command , $ help ) : void {
120
128
if ( isset ( $ this ->setup [$ command ] ) ) {
121
129
throw new Exception ( "Command $ command already registered " );
122
130
}
@@ -134,11 +142,18 @@ public function registerCommand( $command, $help ) {
134
142
* @param string $long multi character option (specified with --)
135
143
* @param string $help help text for this option
136
144
* @param string|null $short one character option (specified with -)
137
- * @param bool|string $needsarg does this option require an argument? give it a name here
145
+ * @param bool|string $needsarg does this option require an argument?
146
+ * give it a name here
138
147
* @param string $command what command does this option apply to
139
148
* @throws Exception
140
149
*/
141
- public function registerOption ( $ long , $ help , $ short = null , $ needsarg = false , $ command = '' ) {
150
+ public function registerOption (
151
+ string $ long ,
152
+ string $ help ,
153
+ ?string $ short = null ,
154
+ $ needsarg = false ,
155
+ string $ command = ''
156
+ ) :void {
142
157
if ( !isset ( $ this ->setup [$ command ] ) ) {
143
158
throw new Exception ( "Command $ command not registered " );
144
159
}
@@ -163,11 +178,13 @@ public function registerOption( $long, $help, $short = null, $needsarg = false,
163
178
*
164
179
* Throws an exception if arguments are missing.
165
180
*
166
- * This is run from CLI automatically and usually does not need to be called directly
181
+ * This is run from CLI automatically and usually does not need to
182
+ * be called directly
167
183
*
168
- * @throws Exception
184
+ * @returns bool true if all is ok
185
+ * @throws UsageException
169
186
*/
170
- public function checkArguments () {
187
+ public function checkArguments () : bool {
171
188
$ argc = count ( $ this ->args );
172
189
173
190
$ req = 0 ;
@@ -179,46 +196,60 @@ public function checkArguments() {
179
196
}
180
197
181
198
if ( $ req > $ argc ) {
182
- throw new Exception ( "Not enough arguments " , Exception::E_OPT_ARG_REQUIRED );
199
+ throw new UsageException (
200
+ "Not enough arguments " , Exception::E_OPT_ARG_REQUIRED
201
+ );
183
202
}
203
+
204
+ return true ;
184
205
}
185
206
186
207
/**
187
208
* Parses the given arguments for known options and command
188
209
*
189
- * The given $args array should NOT contain the executed file as first item anymore! The $args
190
- * array is stripped from any options and possible command. All found otions can be accessed via the
191
- * getOpt() function
210
+ * The given $args array should NOT contain the executed file as
211
+ * first item anymore! The $args array is stripped from any
212
+ * options and possible command. All found otions can be accessed
213
+ * via the getOpt() function
192
214
*
193
- * Note that command options will overwrite any global options with the same name
215
+ * Note that command options will overwrite any global options
216
+ * with the same name
194
217
*
195
- * This is run from CLI automatically and usually does not need to be called directly
218
+ * This is run from CLI automatically and usually does not need to
219
+ * be called directly
196
220
*
197
221
* @throws Exception
198
222
*/
199
- public function parseOptions () {
223
+ public function parseOptions () : void {
200
224
$ non_opts = [];
201
225
202
226
$ argc = count ( $ this ->args );
203
227
for ( $ i = 0 ; $ i < $ argc ; $ i ++ ) {
204
228
$ arg = $ this ->args [$ i ];
205
229
206
- // The special element '--' means explicit end of options. Treat the rest of the arguments as non-options
230
+ // The special element '--' means explicit end of
231
+ // options. Treat the rest of the arguments as non-options
207
232
// and end the loop.
208
233
if ( $ arg == '-- ' ) {
209
- $ non_opts = array_merge ( $ non_opts , array_slice ( $ this ->args , $ i + 1 ) );
234
+ $ non_opts = array_merge (
235
+ $ non_opts , array_slice ( $ this ->args , $ i + 1 )
236
+ );
210
237
break ;
211
238
}
212
239
213
240
// '-' is stdin - a normal argument
214
241
if ( $ arg == '- ' ) {
215
- $ non_opts = array_merge ( $ non_opts , array_slice ( $ this ->args , $ i ) );
242
+ $ non_opts = array_merge (
243
+ $ non_opts , array_slice ( $ this ->args , $ i )
244
+ );
216
245
break ;
217
246
}
218
247
219
248
// first non-option
220
249
if ( $ arg {0 } != '- ' ) {
221
- $ non_opts = array_merge ( $ non_opts , array_slice ( $ this ->args , $ i ) );
250
+ $ non_opts = array_merge (
251
+ $ non_opts , array_slice ( $ this ->args , $ i )
252
+ );
222
253
break ;
223
254
}
224
255
@@ -229,12 +260,17 @@ public function parseOptions() {
229
260
$ val = array_shift ( $ arg );
230
261
231
262
if ( !isset ( $ this ->setup [$ this ->command ]['opts ' ][$ opt ] ) ) {
232
- throw new Exception ( "No such option ' $ opt' " , Exception::E_UNKNOWN_OPT );
263
+ throw new UsageException (
264
+ "No such option ' $ opt' " , Exception::E_UNKNOWN_OPT
265
+ );
233
266
}
234
267
235
268
// argument required?
236
269
if ( $ this ->setup [$ this ->command ]['opts ' ][$ opt ]['needsarg ' ] ) {
237
- if ( is_null ( $ val ) && $ i + 1 < $ argc && !preg_match ( '/^--?[\w]/ ' , $ this ->args [$ i + 1 ] ) ) {
270
+ if (
271
+ is_null ( $ val ) && $ i + 1 < $ argc &&
272
+ !preg_match ( '/^--?[\w]/ ' , $ this ->args [$ i + 1 ] )
273
+ ) {
238
274
$ val = $ this ->args [++$ i ];
239
275
}
240
276
if ( is_null ( $ val ) ) {
@@ -252,15 +288,21 @@ public function parseOptions() {
252
288
// short option
253
289
$ opt = substr ( $ arg , 1 );
254
290
if ( !isset ( $ this ->setup [$ this ->command ]['short ' ][$ opt ] ) ) {
255
- throw new Exception ( "No such option $ arg " , Exception::E_UNKNOWN_OPT );
291
+ throw new UsageException (
292
+ "No such option $ arg " , Exception::E_UNKNOWN_OPT
293
+ );
256
294
} else {
257
- $ opt = $ this ->setup [$ this ->command ]['short ' ][$ opt ]; // store it under long name
295
+ // store it under long name
296
+ $ opt = $ this ->setup [$ this ->command ]['short ' ][$ opt ];
258
297
}
259
298
260
299
// argument required?
261
300
if ( $ this ->setup [$ this ->command ]['opts ' ][$ opt ]['needsarg ' ] ) {
262
301
$ val = null ;
263
- if ( $ i + 1 < $ argc && !preg_match ( '/^--?[\w]/ ' , $ this ->args [$ i + 1 ] ) ) {
302
+ if (
303
+ $ i + 1 < $ argc &&
304
+ !preg_match ( '/^--?[\w]/ ' , $ this ->args [$ i + 1 ] )
305
+ ) {
264
306
$ val = $ this ->args [++$ i ];
265
307
}
266
308
if ( is_null ( $ val ) ) {
@@ -276,8 +318,12 @@ public function parseOptions() {
276
318
// parsing is now done, update args array
277
319
$ this ->args = $ non_opts ;
278
320
279
- // if not done yet, check if first argument is a command and reexecute argument parsing if it is
280
- if ( !$ this ->command && $ this ->args && isset ( $ this ->setup [$ this ->args [0 ]] ) ) {
321
+ // if not done yet, check if first argument is a command and
322
+ // reexecute argument parsing if it is
323
+ if (
324
+ !$ this ->command && $ this ->args &&
325
+ isset ( $ this ->setup [$ this ->args [0 ]] )
326
+ ) {
281
327
// it is a command!
282
328
$ this ->command = array_shift ( $ this ->args );
283
329
$ this ->parseOptions (); // second pass
0 commit comments