diff options
author | Andinus <andinus@nand.sh> | 2020-04-24 19:52:18 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-04-24 19:52:18 +0530 |
commit | 53d4a70ed2d1db9e7ebb98ec734d414539a74fc8 (patch) | |
tree | 521d3f87c08cc695d4207b1925f0de17f416a3ff /parseargs.go | |
parent | a01ce7b96892849d91911a2ef143a08b8e4e57c5 (diff) | |
download | cetus-53d4a70ed2d1db9e7ebb98ec734d414539a74fc8.tar.gz |
Move app func to main
Diffstat (limited to 'parseargs.go')
-rw-r--r-- | parseargs.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/parseargs.go b/parseargs.go new file mode 100644 index 0000000..d47d30d --- /dev/null +++ b/parseargs.go @@ -0,0 +1,70 @@ +package main + +import ( + "flag" + "fmt" + "math/rand" + "os" + "time" +) + +// parseArgs will be parsing the arguments, it will verify if they are +// correct. Flag values are also set by parseArgs. +func parseArgs() { + // Running just `cetus` would've paniced the program if length + // of os.Args was not checked beforehand because there would + // be no os.Args[1]. + switch os.Args[1] { + case "version", "-version", "--version", "-v": + fmt.Printf("Cetus %s\n", version) + os.Exit(0) + + case "help", "-help", "--help", "-h": + // If help was passed then the program shouldn't exit + // with non-zero error code. + printUsage() + os.Exit(0) + + case "set", "fetch": + // If command & service was not passed then print + // usage and exit. + if len(os.Args) < 3 { + printUsage() + os.Exit(1) + } + + default: + fmt.Printf("Invalid command: %q\n", os.Args[1]) + printUsage() + os.Exit(1) + } + + rand.Seed(time.Now().Unix()) + + // If the program has reached this far then that means a valid + // command was passed & now we should check if a valid service + // was passed and parse the flags. + cetus := flag.NewFlagSet("cetus", flag.ExitOnError) + + // We first declare common flags then service specific flags. + cetus.BoolVar(&dump, "dump", false, "Dump the response") + cetus.BoolVar(¬ify, "notify", false, "Send a desktop notification with info") + cetus.BoolVar(&print, "print", false, "Print information") + cetus.BoolVar(&random, "random", false, "Choose a random image") + + switch os.Args[2] { + case "apod", "nasa": + defDate := time.Now().UTC().Format("2006-01-02") + cetus.StringVar(&apodDate, "date", defDate, "Date of NASA APOD to retrieve") + cetus.Parse(os.Args[3:]) + + execAPOD() + case "bpod", "bing": + cetus.Parse(os.Args[3:]) + execBPOD() + default: + fmt.Printf("Invalid service: %q\n", os.Args[2]) + printUsage() + os.Exit(1) + } +} |