changeset 442:19fe8ef0a902

fetch_weather: Improve commandline handling, add -force and -debug options.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Sep 2017 12:33:38 +0300
parents 9c2cc740ec76
children 1f16d27c571d
files fetch_weather.pl
diffstat 1 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/fetch_weather.pl	Tue Sep 05 19:44:56 2017 +0300
+++ b/fetch_weather.pl	Tue Sep 26 12:33:38 2017 +0300
@@ -35,6 +35,7 @@
 ### Configuration settings
 ###
 my %settings = (
+  "force_update" => 0,
   "debug" => 0,
   "opt_fmi" => 0,
   "opt_tiehallinto" => 0,
@@ -285,15 +286,40 @@
 ### Main program begins
 ###
 my $weatherdata = {};
+my $opt_cfgfile;
 
-die(
-"Weather Fetch v0.8 by ccr/TNSP <ccr\@tnsp.org>\n".
-"Usage: $0 <config file> [force]\n"
-) unless scalar(@ARGV) >= 1;
+while (scalar(@ARGV) > 0)
+{
+  my $arg = shift;
+  if ($arg eq "-force")
+  {
+    $settings{"force_update"} = 1;
+  }
+  elsif ($arg eq "-debug")
+  {
+    $settings{"debug"} = 1;
+  }
+  else
+  {
+    die("Configuration file already specified!\n") if defined($opt_cfgfile);
+    $opt_cfgfile = $arg;
+  }
+}
 
-my $cfgfile = shift;
-opt_read_config($cfgfile) == 0 or die("Errors while parsing configuration file '".$cfgfile."'.\n");
-my $force_update = scalar(@ARGV) >= 1 && (shift eq "force");
+if (!defined($opt_cfgfile))
+{
+  die(
+    "Weather Fetch v0.8 by ccr/TNSP <ccr\@tnsp.org>\n".
+    "Usage: $0 <config file> [options]\n".
+    "\n".
+    " -force      : Force updating of all data\n".
+    " -debug      : Enable debug\n".
+    "\n"
+  );
+}
+
+opt_read_config($opt_cfgfile) == 0 or die("Errors while parsing configuration file '".$opt_cfgfile."'.\n");
+print STDERR "Forcing update of all data.\n" if $settings{"force_update"};
 
 
 ###
@@ -357,7 +383,7 @@
 
       # Fetch or read the cache
       my $meta_str;
-      if ($fetch_meta || $force_update)
+      if ($fetch_meta || opt_chk_book("force_update"))
       {
         my $uri = "http://tie.digitraffic.fi/api/v1/metadata/weather-stations";
         print STDERR "Fetching Tiehallinto static meta data from $uri\n" if (opt_get_int("debug") > 1);