X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fcommandline.inc;h=d7ee1588017bbf9c35a2ace7ca2cc4f9483d6a77;hb=8aa783241dd8a476b2b25ea75e0148fac287ce72;hp=bca09216d5842c872ccc7c9d61fcd33c6340762b;hpb=7a0d33ab5fa29993cc7e05e41a26a26ca3ffd1e8;p=quix0rs-gnu-social.git diff --git a/scripts/commandline.inc b/scripts/commandline.inc index bca09216d5..d7ee158801 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -1,7 +1,7 @@ 1 && 0 != strncmp($opt, '--', 2)) { @@ -142,6 +145,15 @@ function have_option($opt, $alt=null) } } + return $matches; +} + +function have_option($opt, $alt=null) +{ + global $options; + + $matches = _make_matches($opt, $alt); + foreach ($options as $option) { if (in_array($option[0], $matches)) { return true; @@ -151,25 +163,11 @@ function have_option($opt, $alt=null) return false; } -function get_option_value($str, $alt=null) +function get_option_value($opt, $alt=null) { global $options; - $matches = array(); - - if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { - $matches[] = '--'.$opt; - } else { - $matches[] = $opt; - } - - if (!empty($alt)) { - if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { - $matches[] = '--'.$alt; - } else { - $matches[] = $alt; - } - } + $matches = _make_matches($opt, $alt); foreach ($options as $option) { if (in_array($option[0], $matches)) { @@ -179,3 +177,70 @@ function get_option_value($str, $alt=null) return null; } + +class NoUserArgumentException extends Exception +{ +} + +function getUser() +{ + $user = null; + + if (have_option('i', 'id')) { + $id = get_option_value('i', 'id'); + $user = User::getKV('id', $id); + if (empty($user)) { + throw new Exception("Can't find user with id '$id'."); + } + } else if (have_option('n', 'nickname')) { + $nickname = get_option_value('n', 'nickname'); + $user = User::getKV('nickname', $nickname); + if (empty($user)) { + throw new Exception("Can't find user with nickname '$nickname'"); + } + } else { + throw new NoUserArgumentException("No user argument specified."); + } + + return $user; +} + +/** "Printf not quiet" */ + +function printfnq() +{ + if (have_option('q', 'quiet')) { + return null; + } + + $cargs = func_num_args(); + + if ($cargs == 0) { + return 0; + } + + $args = func_get_args(); + $format = array_shift($args); + + return vprintf($format, $args); +} + +/** "Print when verbose" */ + +function printfv() +{ + if (!have_option('v', 'verbose')) { + return null; + } + + $cargs = func_num_args(); + + if ($cargs == 0) { + return 0; + } + + $args = func_get_args(); + $format = array_shift($args); + + return vprintf($format, $args); +}