Handle null input in parseArgument method & refactored getArguments() method

This commit is contained in:
netkas 2024-09-24 12:29:37 -04:00
parent 6e7f9089d2
commit 23790bd189

View file

@ -38,65 +38,67 @@
/** /**
* Parses the input arguments into an array of flags and values * Parses the input arguments into an array of flags and values
* *
* @param array|string $input array The input arguments * @param array|string|null $input array The input arguments
* @param int $max_arguments The maximum number of arguments to parse * @param int $max_arguments The maximum number of arguments to parse
* @return array The parsed arguments * @return array The parsed arguments
*/ */
public static function parseArgument(array|string $input, int $max_arguments=1000): array public static function parseArgument(array|string|null $input, int $max_arguments = 1000): array
{ {
$flags = '';
if (is_string($input)) if (is_string($input))
{ {
$flags = $input; $flags = $input;
} }
elseif(is_array($input)) elseif (is_array($input))
{ {
$flags = implode(' ', $input); $flags = implode(' ', $input);
} }
else else
{ {
global $argv; global $argv;
if(isset($argv) && count($argv) > 1) if (isset($argv) && count($argv) > 1)
{ {
array_shift($argv); array_shift($argv);
}
$flags = implode(' ', $argv); $flags = implode(' ', $argv);
} }
}
$configs = array(); $configs = [];
// Assuming self::$regex is always defined and valid
preg_match_all(self::$regex, $flags, $matches, PREG_SET_ORDER); preg_match_all(self::$regex, $flags, $matches, PREG_SET_ORDER);
foreach ($matches as $index => $match) foreach ($matches as $index => $match)
{ {
if(isset($match['value']) && $match['value'] !== '') $value = true;
if (!empty($match['value']))
{ {
$value = $match['value']; $value = $match['value'];
} }
elseif(isset($match['string']) && $match['string'] !== '') elseif (!empty($match['string']))
{ {
// fix escaped quotes // fix escaped quotes
$value = str_replace(["\\\"", "\\'"], ["\"", "'"], $match['string']); $value = str_replace(["\\\"", "\\'"], ["\"", "'"], $match['string']);
} }
else
{
$value = true;
}
if(isset($match['bigflag']) && $match['bigflag'] !== '') if (!empty($match['bigflag']))
{ {
$configs[$match['bigflag']] = $value; $configs[$match['bigflag']] = $value;
} }
if(isset($match['smallflag']) && $match['smallflag'] !== '') if (!empty($match['smallflag']))
{ {
$configs[$match['smallflag']] = $value; $configs[$match['smallflag']] = $value;
} }
if(isset($match['unmatched']) && $match['unmatched'] !== '') if (!empty($match['unmatched']))
{ {
$configs[$match['unmatched']] = true; $configs[$match['unmatched']] = true;
} }
if($index >= ($max_arguments + 1)) if ($index >= $max_arguments)
{ {
break; break;
} }
@ -138,7 +140,6 @@
} }
$after_index = array_search($after, array_keys(self::$args_cache), true); $after_index = array_search($after, array_keys(self::$args_cache), true);
if($after_index === false) if($after_index === false)
{ {
return []; return [];