Use getopt() to parse command line options
[tweeper.git] / tweeper.php
index 11e062b..45bc786 100644 (file)
@@ -137,7 +137,7 @@ function usage($argv)
   if (php_sapi_name() != 'cli')
     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>");
   else
-    $usage = "{$argv[0]} <src_url>\n";
+    $usage = "{$argv[0]} [-h|--help] <src_url>\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;
 }