X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Fcommandline.inc;h=392166a98088dae0ca2f563b0a1709b68ccc7e9b;hb=ab93bb009c8533c8847aafe76ba9774d9d74e7ca;hp=4a7757fb98f13e43c47bf55cdd0841da848304fd;hpb=97373d845cd68d6145f3751c9f602b13f04b37df;p=quix0rs-gnu-social.git diff --git a/scripts/commandline.inc b/scripts/commandline.inc index 4a7757fb98..392166a980 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -1,7 +1,7 @@ getopt($argv, $shortoptions, $longoptions); +$result = $parser->getopt($argv, $shortoptions, $longoptions); + +if (PEAR::isError($result)) { + print $result->getMessage()."\n"; + exit(1); +} else { + list($options, $args) = $result; +} function show_help() { global $helptext; $_default_help_text = << 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; + } + } + + 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; + } + } + + return false; +} + +function get_option_value($opt, $alt=null) +{ + global $options; + + $matches = _make_matches($opt, $alt); + + foreach ($options as $option) { + if (in_array($option[0], $matches)) { + return $option[1]; + } + } + + return null; +} + +class NoUserArgumentException extends Exception { - global $options; - foreach ($options as $option) { - if ($option[0] == $str) { - return true; - } - } - return false; } -function get_option_value($str) +function getUser() { - global $options; - foreach ($options as $option) { - if ($option[0] == $str) { - return $option[1]; - } - } - return null; -} \ No newline at end of file + $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); +}