/
args.go
65 lines (55 loc) · 1.46 KB
/
args.go
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
package main
import (
"fmt"
"io"
"strings"
"github.com/spf13/pflag"
"github.com/wkhere/fractx"
)
func parseArgs(args []string) (conf config, err error) {
var (
colorAvail = strings.Join(fractx.ImageBuilderNames(), ", ")
color string
help bool
)
flag := pflag.NewFlagSet("flags", pflag.ContinueOnError)
flag.SortFlags = false
conf.size = fractx.DefaultSize
conf.bounds = fractx.DefaultBounds
flag.VarP(&conf.size, "size", "s", "pixel-plane size")
flag.VarP(&conf.bounds, "bounds", "b", "float-plane bounds")
flag.UintVarP(&conf.maxi, "maxi", "i", fractx.DefaultMaxI,
"max number of iterations")
flag.StringVarP(&color, "color", "c", "col1",
"one of: "+colorAvail)
flag.StringVarP(&conf.filename, "output", "o", "mandelbrot.png",
"output file or '-' for stdout")
flag.BoolVarP(&conf.overwrite, "overwrite", "O", true,
"overwrite output file")
flag.BoolVarP(&help, "help", "h", false,
"show this help and exit")
flag.Usage = func() {
fmt.Fprintln(flag.Output(), "Generate Mandelbrot fractal.")
fmt.Fprintln(flag.Output(), "Usage:", prog, "[FLAGS]")
flag.PrintDefaults()
}
err = flag.Parse(args)
if err != nil {
return conf, err
}
if help {
conf.help = func(w io.Writer) {
flag.SetOutput(w)
flag.Usage()
}
return conf, nil
}
var ok bool
conf.newImage, ok = fractx.ImageBuilders[color]
if !ok {
return conf, fmt.Errorf(
"wrong color: %s, should be one of: %s", color, colorAvail,
)
}
return conf, nil
}