summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-01 16:27:55 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-01 16:31:00 -0500
commitc9a0a5fd748778cb068f88c4bd29147e5b8f28e9 (patch)
tree0611ef185f490ba95ab5b5ba410f1f1daee93355
parentaf9680f0ed8d9282a70d5b4099445133d7c69af5 (diff)
Add cmdline help & version info
-rw-r--r--args.go52
-rw-r--r--radix_info.go4
2 files changed, 42 insertions, 14 deletions
diff --git a/args.go b/args.go
index 517cb21..10c28b1 100644
--- a/args.go
+++ b/args.go
@@ -2,37 +2,61 @@ package main
import (
"errors"
- "fmt"
"flag"
+ "fmt"
"strconv"
)
+const ProgramVersion = "1.0.0-alpha+dev"
+
// The arguments to this program
type args struct {
- Radix uint
+ Radix uint
Compact bool
+ // If true, exit the program immediately after parsing args.
+ Exit bool
}
func parseArgs() (args, error) {
var a args
flag.BoolVar(&a.Compact, "c", false, "Compact the output display")
+ help := flag.Bool("?", false,
+ "Get information about program usage then exit")
+ version := flag.Bool("V", false,
+ "Print program version then exit")
flag.Parse()
- if flag.NArg() == 1 {
- if radix, err := strconv.ParseUint(flag.Arg(0), 0, 0); err == nil {
- if radix > 1 {
- a.Radix = uint(radix)
- return a, nil
+ if *help {
+ printHelp()
+ return args{Exit: true}, nil
+ } else if *version {
+ fmt.Println("Radix Info version", ProgramVersion)
+ return args{Exit: true}, nil
+ } else {
+ if flag.NArg() == 1 {
+ if radix, err := strconv.ParseUint(flag.Arg(0), 0, 0); err == nil {
+ if radix > 1 {
+ a.Radix = uint(radix)
+ return a, nil
+ } else {
+ return args{}, errors.New("Radix must be an integer above 1.")
+ }
} else {
- return args{}, errors.New("Radix must be an integer above 1.")
+ return args{}, fmt.Errorf(
+ "Argument must be an integer above 1 [%w].", err)
}
+ } else if flag.NArg() < 1 {
+ return args{}, errors.New("Please provide an argument (radix to study).")
} else {
- return args{}, fmt.Errorf(
- "Argument must be an integer above 1 [%w].", err)
+ return args{}, errors.New("Too many arguments provided.")
}
- } else if flag.NArg() < 1 {
- return args{}, errors.New("Please provide an argument (radix to study).")
- } else {
- return args{}, errors.New("Too many arguments provided.")
}
}
+
+func printHelp() {
+ fmt.Println("Get important information about a radix.")
+ fmt.Println("You must provide one argument, which is the radix to get information about.")
+ fmt.Println("Usage: radix_info [flags...] <radix>")
+ fmt.Println("Options:")
+ flag.PrintDefaults()
+}
diff --git a/radix_info.go b/radix_info.go
index 04a8228..fb1caef 100644
--- a/radix_info.go
+++ b/radix_info.go
@@ -7,6 +7,10 @@ import (
func main() {
args, err := parseArgs()
+ if args.Exit {
+ return
+ }
+
if err == nil {
factorInfo := GetFactorInfo(args.Radix)
if args.Compact {