summaryrefslogtreecommitdiff
path: root/args.go
blob: 3b2bbddc48360cdc2b4a9ce07caabdcd4e135841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main

import (
	"errors"
	"flag"
	"fmt"
	"strconv"
)

const ProgramVersion = "1.0.0-beta"

// The arguments to this program
type args struct {
	Radix          uint
	Compact        bool
	FullMap        bool
	ExactMTCLarge  bool
	TotativeDigits bool
	DigitMapOnly   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")
	flag.BoolVar(&a.DigitMapOnly, "d", false,
		"Show only the digit map; incompatible with -m and -t")
	flag.BoolVar(&a.FullMap, "f", false,
		fmt.Sprintf("Show full digit map (up to %d) for every radix",
			maxSmallRadix))
	flag.BoolVar(&a.ExactMTCLarge, "m", false,
		fmt.Sprintf("Calculate exact MTC for very large radices (up to %d instead of %d), which may take a while.",
			maxExtended, maxNormal))
	flag.BoolVar(&a.TotativeDigits, "t", false,
		"Calculate the radix's totative digits instead of just how many there are; incompatible with -c.")
	help := flag.Bool("?", false,
		"Get information about program usage then exit")
	version := flag.Bool("V", false,
		"Print program version then exit")
	flag.Parse()

	if *help {
		printHelp()
		return args{Exit: true}, nil
	} else if *version {
		fmt.Println("Radix Info version", ProgramVersion)
		return args{Exit: true}, nil
	} else if a.Compact && a.TotativeDigits {
		return args{}, errors.New("You cannot use both -t and -c at the same time.")
	} else if a.DigitMapOnly && (a.TotativeDigits || a.ExactMTCLarge) {
		return args{}, errors.New("You cannot use both -d and -t or -m at the same time.")
	} 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{}, 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{}, 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()
}