From: Antonio Ospite Date: Sun, 11 Aug 2013 18:27:36 +0000 (+0200) Subject: Use getopt() to parse command line options X-Git-Tag: v0.1~12^2~9 X-Git-Url: https://git.ao2.it/tweeper.git/commitdiff_plain/d38cac35c8e0aed0a223533bb54e46958c693212?ds=sidebyside Use getopt() to parse command line options This will make it easier to add more options. --- diff --git a/tweeper.php b/tweeper.php index 11e062b..45bc786 100644 --- a/tweeper.php +++ b/tweeper.php @@ -137,7 +137,7 @@ function usage($argv) if (php_sapi_name() != 'cli') $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url="); else - $usage = "{$argv[0]} \n"; + $usage = "{$argv[0]} [-h|--help] \n"; return "usage: $usage"; } @@ -149,10 +149,19 @@ function parse_options_cli($argv, $argc) if ($argc < 2) return $options; - if ($argv[1] == "-h" || $argv[1] == "--help") - echo usage($argv); - else - $options['src_url'] = $argv[1]; + $cli_options = getopt("h", array("help")); + foreach ($cli_options as $opt => $val) { + switch ($opt) { + case 'h': + case 'help': + echo usage($argv); + exit(0); + default: + die(usage($argv)); + } + } + + $options['src_url'] = $argv[count($cli_options) + 1]; return $options; }