ansiweather

Weather in terminal, with ANSI colors and Unicode symbols
Log | Files | Refs | README | LICENSE

commit 1ab172bc0aa18ee03561de7700b44a0ce4f940fb
parent e48dee31112adc24f7d2acd8b6dc01554fbd6a26
Author: Lucas Hoffmann <l-m-h@web.de>
Date:   Thu, 20 Aug 2015 01:35:47 +0200

Parse command line before setting options.

The setting of the default options might query the current location from a
geoip service over the internet which is potentially slow.  This is especially
problematic if the user only wants to see the help message (which does not
need the current location).

Diffstat:
Mansiweather | 108+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 61 insertions(+), 47 deletions(-)

diff --git a/ansiweather b/ansiweather @@ -40,7 +40,59 @@ fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s") -###[ Check if bc and jq are installed ]####################################### +###[ Parse the command line ]################################################## + +# Set all variables that can be set from the command line to an empty value. +location= +units= +symbols= +forecast= +forecast= +daylight= + +# Get config options from command line flags +while getopts l:u:s:f:Fd:h option +do + case "${option}" + in + l) location=${OPTARG};; + u) units=${OPTARG};; + s) symbols=${OPTARG};; + f) forecast=${OPTARG};; + F) forecast="5";; + d) daylight=${OPTARG};; + h) usage=true;; + esac +done + + + +#### [ Display usage ]######################################################### + +if [ "$usage" = true ] +then + printf "%s\n" \ + "" \ + "AnsiWeather 1.02 (c) by Frederic Cambus 2013-2015" \ + "" \ + "USAGE: ansiweather [options]" \ + "" \ + "Options are :" \ + "" \ + " -l Specify location" \ + " -u Specify unit system to use (metric or imperial)" \ + " -s Toggle symbol display" \ + " -f Toggle forecast mode for the specified number of upcoming days" \ + " -F Toggle forecast mode for the next five days" \ + " -d Toggle daylight data display" \ + " -h Display usage" \ + "EXAMPLES: ansiweather -l Moscow,RU -u metric -s true -f 5 -d true" + exit +fi + + + +###[ Check if bc and jq are installed ]######################################## jqpath=$(which jq) if [ "$jqpath" = "" ] @@ -58,7 +110,7 @@ fi -###[ Auto-Location Logic ]#################################################### +###[ Auto-Location Logic ]##################################################### geo_api_proto=$(get_config "geo_api_proto" || echo "http") geo_api_url=$(get_config "geo_api_url" || echo "www.telize.com/geoip") #geo location service @@ -83,64 +135,26 @@ auto_locate() { fi } +###[ Set options that are not set from command line ]########################## + # Location : example "Moscow,RU" -location=$(get_config "location" || auto_locate || echo "Moscow,RU") +[ -z "$location" ] && location=$(get_config "location" || auto_locate || echo "Moscow,RU") # System of Units : "metric" or "imperial" -units=$(get_config "units" || echo "metric") +[ -z "$units" ] && units=$(get_config "units" || echo "metric") # Display symbols : "true" or "false" (requires an Unicode capable display) -symbols=$(get_config "symbols" || echo true) +[ -z "$symbols" ] && symbols=$(get_config "symbols" || echo true) # Show forecast : How many days, example "5". "0" is standard output -forecast=$(get_config "forecast" || echo 0) +[ -z "$forecast" ] && forecast=$(get_config "forecast" || echo 0) # Show daylight : "true" or "false" -daylight=$(get_config "daylight" || echo false) +[ -z "$daylight" ] && daylight=$(get_config "daylight" || echo false) dateformat=$(get_config "dateformat" || echo "%a %b %d") timeformat=$(get_config "timeformat" || echo "%b %d %r") -# Or get config options from command line flags -while getopts l:u:s:f:Fd:h option -do - case "${option}" - in - l) location=${OPTARG};; - u) units=${OPTARG};; - s) symbols=${OPTARG};; - f) forecast=${OPTARG};; - F) forecast="5";; - d) daylight=${OPTARG};; - h) usage=true;; - esac -done - - - -#### [ Display usage ]######################################################### - -if [ "$usage" = true ] -then - printf "%s\n" \ - "" \ - "AnsiWeather 1.02 (c) by Frederic Cambus 2013-2015" \ - "" \ - "USAGE: ansiweather [options]" \ - "" \ - "Options are :" \ - "" \ - " -l Specify location" \ - " -u Specify unit system to use (metric or imperial)" \ - " -s Toggle symbol display" \ - " -f Toggle forecast mode for the specified number of upcoming days" \ - " -F Toggle forecast mode for the next five days" \ - " -d Toggle daylight data display" \ - " -h Display usage" \ - "EXAMPLES: ansiweather -l Moscow,RU -u metric -s true -f 5 -d true" - exit -fi - #### [ Colors and characters ]#################################################