@@ -97,7 +97,7 @@ class CLI
9797 protected static $ segments = [];
9898
9999 /**
100- * @var array<string, string|null>
100+ * @var array<string, list<string|null>| string|null>
101101 */
102102 protected static $ options = [];
103103
@@ -925,8 +925,11 @@ public static function getSegments(): array
925925 }
926926
927927 /**
928- * Gets a single command-line option. Returns TRUE if the option
929- * exists, but doesn't have a value, and is simply acting as a flag.
928+ * Gets the value of an individual option.
929+ *
930+ * * If the option was passed without a value, this will return `true`.
931+ * * If the option was not passed at all, this will return `null`.
932+ * * If the option was an array of values, this will return the last value passed for that option.
930933 *
931934 * @return string|true|null
932935 */
@@ -936,17 +939,34 @@ public static function getOption(string $name)
936939 return null ;
937940 }
938941
939- // If the option didn't have a value, simply return TRUE
940- // so they know it was set, otherwise return the actual value.
941- $ val = static ::$ options [$ name ] ?? true ;
942+ $ value = static ::$ options [$ name ] ?? true ;
943+
944+ if (! is_array ($ value )) {
945+ return $ value ;
946+ }
947+
948+ return $ value [count ($ value ) - 1 ] ?? true ;
949+ }
950+
951+ /**
952+ * Gets the raw value of an individual option, which may be a string,
953+ * a list of `string|null`, or `true` if the option was passed without a value.
954+ *
955+ * @return list<string|null>|string|true|null
956+ */
957+ public static function getRawOption (string $ name ): array |string |true |null
958+ {
959+ if (! array_key_exists ($ name , static ::$ options )) {
960+ return null ;
961+ }
942962
943- return $ val ;
963+ return static :: $ options [ $ name ] ?? true ;
944964 }
945965
946966 /**
947967 * Returns the raw array of options found.
948968 *
949- * @return array<string, string|null>
969+ * @return array<string, list<string|null>| string|null>
950970 */
951971 public static function getOptions (): array
952972 {
@@ -966,27 +986,33 @@ public static function getOptionString(bool $useLongOpts = false, bool $trim = f
966986 return '' ;
967987 }
968988
969- $ out = '' ;
989+ $ out = [] ;
970990
971- foreach (static ::$ options as $ name => $ value ) {
972- if ($ useLongOpts && mb_strlen ($ name ) > 1 ) {
973- $ out .= "-- {$ name } " ;
991+ $ valueCallback = static function (?string $ value , string $ name ) use (&$ out ): void {
992+ if ($ value === null ) {
993+ $ out [] = $ name ;
994+ } elseif (str_contains ($ value , ' ' )) {
995+ $ out [] = sprintf ('%s "%s" ' , $ name , $ value );
974996 } else {
975- $ out .= " - { $ name} " ;
997+ $ out[] = sprintf ( ' %s %s ' , $ name, $ value ) ;
976998 }
999+ };
9771000
978- if ($ value === null ) {
979- continue ;
980- }
1001+ foreach (static ::$ options as $ name => $ value ) {
1002+ $ name = $ useLongOpts && mb_strlen ($ name ) > 1 ? "-- {$ name }" : "- {$ name }" ;
9811003
982- if (mb_strpos ($ value , ' ' ) !== false ) {
983- $ out .= "\"{$ value }\" " ;
984- } elseif ($ value !== null ) {
985- $ out .= "{$ value } " ;
1004+ if (is_array ($ value )) {
1005+ foreach ($ value as $ val ) {
1006+ $ valueCallback ($ val , $ name );
1007+ }
1008+ } else {
1009+ $ valueCallback ($ value , $ name );
9861010 }
9871011 }
9881012
989- return $ trim ? trim ($ out ) : $ out ;
1013+ $ output = implode (' ' , $ out );
1014+
1015+ return $ trim ? $ output : "{$ output } " ;
9901016 }
9911017
9921018 /**
0 commit comments