55/**
66 * Class Options
77 *
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.
1011 *
1112 * @author Andreas Gohr <andi@splitbrain.org>
1213 * @license MIT
@@ -62,7 +63,7 @@ public function __construct( Colors $colors = null ) {
6263 *
6364 * @param string $help
6465 */
65- public function setHelp ( $ help ) {
66+ public function setHelp ( $ help ) : void {
6667 $ this ->setup ['' ]['help ' ] = $ help ;
6768 }
6869
@@ -71,7 +72,7 @@ public function setHelp( $help ) {
7172 *
7273 * @param string $help
7374 */
74- public function setCommandHelp ( $ help ) {
75+ public function setCommandHelp ( $ help ) : void {
7576 $ this ->setup ['' ]['commandhelp ' ] = $ help ;
7677 }
7778
@@ -80,12 +81,13 @@ public function setCommandHelp( $help ) {
8081 *
8182 * @param bool $compact
8283 */
83- public function setCompactHelp ( $ compact = true ) {
84+ public function setCompactHelp ( $ compact = true ) : void {
8485 $ this ->setup ['' ]['compacthelp ' ] = $ compact ;
8586 }
8687
8788 /**
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
8991 *
9092 * This has to be called in the order arguments are expected
9193 *
@@ -95,7 +97,12 @@ public function setCompactHelp( $compact = true ) {
9597 * @param string $command if theses apply to a sub command only
9698 * @throws Exception
9799 */
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 {
99106 if ( !isset ( $ this ->setup [$ command ] ) ) {
100107 throw new Exception ( "Command $ command not registered " );
101108 }
@@ -110,13 +117,14 @@ public function registerArgument( $arg, $help, $required = true, $command = '' )
110117 /**
111118 * This registers a sub command
112119 *
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()).
114122 *
115123 * @param string $command
116124 * @param string $help
117125 * @throws Exception
118126 */
119- public function registerCommand ( $ command , $ help ) {
127+ public function registerCommand ( $ command , $ help ) : void {
120128 if ( isset ( $ this ->setup [$ command ] ) ) {
121129 throw new Exception ( "Command $ command already registered " );
122130 }
@@ -134,11 +142,18 @@ public function registerCommand( $command, $help ) {
134142 * @param string $long multi character option (specified with --)
135143 * @param string $help help text for this option
136144 * @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
138147 * @param string $command what command does this option apply to
139148 * @throws Exception
140149 */
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 {
142157 if ( !isset ( $ this ->setup [$ command ] ) ) {
143158 throw new Exception ( "Command $ command not registered " );
144159 }
@@ -163,11 +178,13 @@ public function registerOption( $long, $help, $short = null, $needsarg = false,
163178 *
164179 * Throws an exception if arguments are missing.
165180 *
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
167183 *
168- * @throws Exception
184+ * @returns bool true if all is ok
185+ * @throws UsageException
169186 */
170- public function checkArguments () {
187+ public function checkArguments () : bool {
171188 $ argc = count ( $ this ->args );
172189
173190 $ req = 0 ;
@@ -179,46 +196,60 @@ public function checkArguments() {
179196 }
180197
181198 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+ );
183202 }
203+
204+ return true ;
184205 }
185206
186207 /**
187208 * Parses the given arguments for known options and command
188209 *
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
192214 *
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
194217 *
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
196220 *
197221 * @throws Exception
198222 */
199- public function parseOptions () {
223+ public function parseOptions () : void {
200224 $ non_opts = [];
201225
202226 $ argc = count ( $ this ->args );
203227 for ( $ i = 0 ; $ i < $ argc ; $ i ++ ) {
204228 $ arg = $ this ->args [$ i ];
205229
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
207232 // and end the loop.
208233 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+ );
210237 break ;
211238 }
212239
213240 // '-' is stdin - a normal argument
214241 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+ );
216245 break ;
217246 }
218247
219248 // first non-option
220249 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+ );
222253 break ;
223254 }
224255
@@ -229,12 +260,17 @@ public function parseOptions() {
229260 $ val = array_shift ( $ arg );
230261
231262 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+ );
233266 }
234267
235268 // argument required?
236269 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+ ) {
238274 $ val = $ this ->args [++$ i ];
239275 }
240276 if ( is_null ( $ val ) ) {
@@ -252,15 +288,21 @@ public function parseOptions() {
252288 // short option
253289 $ opt = substr ( $ arg , 1 );
254290 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+ );
256294 } 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 ];
258297 }
259298
260299 // argument required?
261300 if ( $ this ->setup [$ this ->command ]['opts ' ][$ opt ]['needsarg ' ] ) {
262301 $ 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+ ) {
264306 $ val = $ this ->args [++$ i ];
265307 }
266308 if ( is_null ( $ val ) ) {
@@ -276,8 +318,12 @@ public function parseOptions() {
276318 // parsing is now done, update args array
277319 $ this ->args = $ non_opts ;
278320
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+ ) {
281327 // it is a command!
282328 $ this ->command = array_shift ( $ this ->args );
283329 $ this ->parseOptions (); // second pass
0 commit comments