This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
76 lines (63 loc) · 1.59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
)
const DefaultDisplayCount = 10
const DefaultDisplayCountEnv = "TURENGO_DEFAULT_DISPLAY_COUNT"
type Flags struct {
DisplayCount int // Max display count
TypeFilters []WordType
}
func main() {
flags := getFlags()
pageContent, err := Translate(strings.Join(flag.Args(), " "), flags)
if err != nil {
log.Fatal(err)
}
pageContent.PrintAsTable()
}
func getFlags() *Flags {
displayCount := flag.Int("c", DefaultDisplayCount, "Max display count")
verbFlag := flag.Bool("v", false, "Filter verbs")
nounFlag := flag.Bool("n", false, "Filter nouns")
adverbFlag := flag.Bool("adv", false, "Filter adverbs")
adjectiveFlag := flag.Bool("adj", false, "Filter adjectives")
flag.Usage = func() {
fmt.Printf("Usage: %s TEXT \n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
flags := &Flags{DisplayCount: *displayCount}
// Read displayCount from ENV if flag not specified and env var exists
if *displayCount == DefaultDisplayCount {
if dc := os.Getenv(DefaultDisplayCountEnv); dc != "" {
i, _ := strconv.Atoi(dc)
flags.DisplayCount = i
}
}
if *verbFlag {
flags.TypeFilters = append(flags.TypeFilters, VERB)
}
if *nounFlag {
flags.TypeFilters = append(flags.TypeFilters, NOUN)
}
if *adjectiveFlag {
flags.TypeFilters = append(flags.TypeFilters, ADJECTIVE)
}
if *adverbFlag {
flags.TypeFilters = append(flags.TypeFilters, ADVERB)
}
if len(flags.TypeFilters) == 0 {
flags.TypeFilters = []WordType{VERB, NOUN, ADJECTIVE, ADVERB, UNKNOWN}
}
return flags
}