/
flags03.go
37 lines (29 loc) · 1.03 KB
/
flags03.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
/* Alta3 Research | RZFeeser
Flags - Strings, Integers, Booleans
and alternatives techniques to parsing the CLI */
package main
import (
"flag" // basic CLI parsing
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("clear") // Clear the term screen before program run
cmd.Stdout = os.Stdout
cmd.Run()
// these all return pointers
wordPtr := flag.String("word", "Don't panic!", "a string")
numbPtr := flag.Int("numb", 42, "an int")
boolPtr := flag.Bool("truthy", false, "a bool")
// example of passing a pointer to one of the "flag.*Var()" functions
var spiderman string // define the string variable "spiderman"
flag.StringVar(&spiderman, "spiderman", "Friendly neighborhood Spider-Man", "Catchphrase for Spider-Man")
flag.Parse()
// display the values captured
fmt.Println("word:", *wordPtr)
fmt.Println("numb:", *numbPtr)
fmt.Println("truthy:", *boolPtr)
fmt.Println("spiderman:", spiderman)
fmt.Println("tail:", flag.Args())
}