package main import ( "errors" "flag" "fmt" "strconv" ) const ProgramVersion = "1.0.0-alpha+dev" // 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...] ") fmt.Println("Options:") flag.PrintDefaults() }