From 9679e6ae8b28c733f817c09b269e746e56f62a0b Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 24 Oct 2023 17:05:52 +0300 Subject: [PATCH] Adding simple opt arguments related routines --- helpers.inc.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/helpers.inc.php b/helpers.inc.php index 1f6ee69..089abf7 100644 --- a/helpers.inc.php +++ b/helpers.inc.php @@ -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() { echo "Are you sure you want to proceed?(type YES): ";