-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (41 loc) · 1.03 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"
"fmt"
"os"
"github.com/fatih/color"
)
func main() {
redPtr := flag.Bool("r", false, "Print in red to stderr and exit with error code 2")
greenPtr := flag.Bool("g", false, "Print in green (default)")
bluePtr := flag.Bool("b", false, "Print in blue")
yellowPtr := flag.Bool("y", false, "Print in yellow")
helpPtr := flag.Bool("h", false, "Display help")
// Parse command line arguments
flag.Parse()
if *helpPtr {
flag.Usage()
os.Exit(0)
}
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(1)
}
text := args[0]
// Choose color
var printFunc func(a ...interface{}) string
printFunc = color.New(color.FgGreen).SprintFunc()
w := os.Stdout
if *redPtr {
printFunc = color.New(color.FgRed).SprintFunc()
w = os.Stderr
} else if *greenPtr {
printFunc = color.New(color.FgGreen).SprintFunc()
} else if *bluePtr {
printFunc = color.New(color.FgBlue).SprintFunc()
} else if *yellowPtr {
printFunc = color.New(color.FgYellow).SprintFunc()
}
fmt.Fprintln(w, printFunc(text))
}