From d38cac35c8e0aed0a223533bb54e46958c693212 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Sun, 11 Aug 2013 20:27:36 +0200 Subject: [PATCH] Use getopt() to parse command line options This will make it easier to add more options. --- tweeper.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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; } -- 2.1.4