diff options
author | Nick White <git@njw.name> | 2021-02-05 13:31:33 +0000 |
---|---|---|
committer | Nick White <git@njw.name> | 2021-02-05 13:31:33 +0000 |
commit | 3413df247aee70fc9ba217dfaa24bb217fff4248 (patch) | |
tree | 52d3a2d0b31762b5e2de0062c4c026ffa9254513 | |
parent | d7dc8bb54b289500ed4a2a915bff4459c85ccac3 (diff) | |
download | weather-3413df247aee70fc9ba217dfaa24bb217fff4248.tar.bz2 weather-3413df247aee70fc9ba217dfaa24bb217fff4248.zip |
Fix argument parsing so choosing a source is clear and works properly
-rw-r--r-- | weather.go | 29 |
1 files changed, 16 insertions, 13 deletions
@@ -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) |