-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
46 lines (39 loc) · 1.22 KB
/
main.go
File metadata and controls
46 lines (39 loc) · 1.22 KB
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
// The `so/flag` package provides a way
// to parse command-line arguments.
package main
import (
"solod.dev/so/flag"
"solod.dev/so/fmt"
"solod.dev/so/os"
)
func main() {
// Basic flag declarations are available for string,
// integer, and boolean options.
// Here we declare a string flag `word` with a default
// value "foo" and a short description. This flag.StringVar
// function takes a pointer to a string variable which will
// hold the value of the flag after parsing.
var word string
flag.StringVar(&word, "word", "foo", "a string")
// Declare `num` and `ok` flags using a similar approach.
var num int
flag.IntVar(&num, "num", 42, "an int")
var ok bool
flag.BoolVar(&ok, "ok", false, "a bool")
// Once all flags are declared, call flag.Parse
// to parse the command-line arguments.
flag.Parse()
// Here we'll just dump out the parsed options and
// any trailing positional arguments.
fmt.Printf("word = %s\n", word)
fmt.Printf("num = %d\n", num)
fmt.Printf("ok = %d\n", ok)
for i, arg := range flag.Args() {
fmt.Printf("tail[%d] = %s\n", i, arg)
}
// The raw command-line arguments are available in os.Args.
fmt.Println("os.Args:")
for i, arg := range os.Args {
fmt.Printf("%d: %s\n", i, arg)
}
}