# HG changeset patch # User Matti Hamalainen # Date 1506418418 -10800 # Node ID 19fe8ef0a902471ea6203dba54102617c6db154f # Parent 9c2cc740ec76e6d1430d5fcbba966d1e69a55f53 fetch_weather: Improve commandline handling, add -force and -debug options. diff -r 9c2cc740ec76 -r 19fe8ef0a902 fetch_weather.pl --- 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 \n". -"Usage: $0 [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 \n". + "Usage: $0 [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);