From 3413df247aee70fc9ba217dfaa24bb217fff4248 Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 5 Feb 2021 13:31:33 +0000 Subject: Fix argument parsing so choosing a source is clear and works properly --- weather.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'weather.go') diff --git a/weather.go b/weather.go index 4fd1c36..b0ca2da 100644 --- a/weather.go +++ b/weather.go @@ -133,7 +133,7 @@ type Weather struct { } var ( - bbc = flag.Bool("b", true, "use bbc as data source") + src = flag.String("s", "bbc", "data source provider (valid options: 'bbc', 'metoffice')") numdays = flag.Int("n", 2, "number of days to show") verbose = flag.Bool("v", false, "verbose: show all weather details") ) @@ -203,25 +203,28 @@ func processMet(b []byte) []Weather { func main() { var err error var id string + var url string var resp *http.Response var weather []Weather flag.Parse() + + switch *src { + case "bbc": + id = bbcdefid + url = bbcurl + case "metoffice": + id = metdefid + url = meturl + default: + log.Fatalf("data source %s not supported; use either 'bbc' or 'metoffice'\n", *src) + } + if flag.NArg() > 0 { id = flag.Arg(0) - } else { - if *bbc { - id = bbcdefid - } else { - id = metdefid - } } - if *bbc { - resp, err = http.Get(fmt.Sprintf(bbcurl, id)) - } else { - resp, err = http.Get(fmt.Sprintf(meturl, id)) - } + resp, err = http.Get(fmt.Sprintf(url, id)) if err != nil { log.Fatal(err) } @@ -231,7 +234,7 @@ func main() { } b, err := ioutil.ReadAll(resp.Body) - if *bbc { + if *src == "bbc" { weather = processBBC(b) } else { weather = processMet(b) -- cgit v1.2.3