summaryrefslogtreecommitdiff
path: root/args.go
blob: 517cb2166d0bccb1f9c46f4937bcfb87ed7143ca (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
package main

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

// The arguments to this program
type args struct {
	Radix uint
	Compact bool
}

func parseArgs() (args, error) {
	var a args
	flag.BoolVar(&a.Compact, "c", false, "Compact the output display")
	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
			} 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.")
	}
}