-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
53 lines (42 loc) · 1.12 KB
/
main.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
package main
import (
"flag"
"io"
"log"
"os"
"path/filepath"
"github.com/jmank88/ca/lib"
)
func main() {
var config ca.Config
flag.IntVar(&config.Cells, "cells", ca.Default.Cells, "number of cells")
flag.IntVar(&config.Generations, "gens", ca.Default.Generations, "generations")
flag.StringVar(&config.Format, "format", "", "output format; override file extension; one of: txt, svg, gif, json, png, jpg, jpeg")
flag.BoolVar(&config.Random, "rand", ca.Default.Random, "randomized initial state")
flag.IntVar(&config.Rule, "r", ca.Default.Rule, "rule (0-255)")
flag.IntVar(&config.Size, "s", ca.Default.Size, "size in format units (>0)")
var file string
flag.StringVar(&file, "file", "", "output filename")
flag.Parse()
var out io.Writer
if file == "" {
out = os.Stdout
} else {
if f, err := os.Create(file); err != nil {
log.Fatal(err)
} else {
defer f.Close()
out = f
}
}
if config.Format == "" {
if ext := filepath.Ext(file); ext != "" {
config.Format = ext[1:len(ext)]
} else {
config.Format = ca.Default.Format
}
}
if err := config.Print(out); err != nil {
log.Fatal(err)
}
}