Adding simple opt arguments related routines

This commit is contained in:
Pavel Shevaev 2023-10-24 17:05:52 +03:00
parent ac3122efed
commit 9679e6ae8b
1 changed files with 23 additions and 0 deletions

View File

@ -1135,6 +1135,29 @@ if(!function_exists('str_ends_with'))
} }
} }
function arg_opt(array &$args, $opt, $default, $conv_fn = null)
{
foreach($args as $idx => $arg)
{
if(strpos($arg, $opt) === 0)
{
$arg = substr($arg, strlen($opt));
unset($args[$idx]);
return $conv_fn === null ? $arg : $conv_fn($arg);
}
}
return $default;
}
function arg_opt_check_no_trailing(array $args)
{
foreach($args as $arg)
{
if(preg_match('~(--\w+)=.*~', $arg, $m))
throw new Exception("Unknown option '{$m[1]}'");
}
}
function are_you_sure() function are_you_sure()
{ {
echo "Are you sure you want to proceed?(type YES): "; echo "Are you sure you want to proceed?(type YES): ";