Debian Codegraph @

example_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Example of use of the flags package.
2 package flags_test
3
4 import (
5 "fmt"
6 "github.com/jessevdk/go-flags"
7 "os"
8 "os/exec"
9 "strings"
10 )
11
12 func Example() {
13 var opts struct {
14 // Slice of bool will append 'true' each time the option
15 // is encountered (can be set multiple times, like -vvv)
16 Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
17
18 // Example of automatic marshalling to desired type (uint)
19 Offset uint `long:"offset" description:"Offset"`
20
21 // Example of a callback, called each time the option is found.
22 Call func(string) `short:"c" description:"Call phone number"`
23
24 // Example of a required flag
25 Name string `short:"n" long:"name" description:"A name" required:"true"`
26
27 // Example of a value name
28 File string `short:"f" long:"file" description:"A file" value-name:"FILE"`
29
30 // Example of a pointer
31 Ptr *int `short:"p" description:"A pointer to an integer"`
32
33 // Example of a slice of strings
34 StringSlice []string `short:"s" description:"A slice of strings"`
35
36 // Example of a slice of pointers
37 PtrSlice []*string `long:"ptrslice" description:"A slice of pointers to string"`
38
39 // Example of a map
40 IntMap map[string]int `long:"intmap" description:"A map from string to int"`
41 }
42
43 // Callback which will invoke callto:<argument> to call a number.
44 // Note that this works just on OS X (and probably only with
45 // Skype) but it shows the idea.
46 opts.Call = func(num string) {
47 cmd := exec.Command("open", "callto:"+num)
48 cmd.Start()
49 cmd.Process.Release()
50 }
51
52 // Make some fake arguments to parse.
53 args := []string{
54 "-vv",
55 "--offset=5",
56 "-n", "Me",
57 "-p", "3",
58 "-s", "hello",
59 "-s", "world",
60 "--ptrslice", "hello",
61 "--ptrslice", "world",
62 "--intmap", "a:1",
63 "--intmap", "b:5",
64 "arg1",
65 "arg2",
66 "arg3",
67 }
68
69 // Parse flags from `args'. Note that here we use flags.ParseArgs for
70 // the sake of making a working example. Normally, you would simply use
71 // flags.Parse(&opts) which uses os.Args
72 args, err := flags.ParseArgs(&opts, args)
73
74 if err != nil {
75 panic(err)
76 os.Exit(1)
77 }
78
79 fmt.Printf("Verbosity: %v\n", opts.Verbose)
80 fmt.Printf("Offset: %d\n", opts.Offset)
81 fmt.Printf("Name: %s\n", opts.Name)
82 fmt.Printf("Ptr: %d\n", *opts.Ptr)
83 fmt.Printf("StringSlice: %v\n", opts.StringSlice)
84 fmt.Printf("PtrSlice: [%v %v]\n", *opts.PtrSlice[0], *opts.PtrSlice[1])
85 fmt.Printf("IntMap: [a:%v b:%v]\n", opts.IntMap["a"], opts.IntMap["b"])
86 fmt.Printf("Remaining args: %s\n", strings.Join(args, " "))
87
88 // Output: Verbosity: [true true]
89 // Offset: 5
90 // Name: Me
91 // Ptr: 3
92 // StringSlice: [hello world]
93 // PtrSlice: [hello world]
94 // IntMap: [a:1 b:5]
95 // Remaining args: arg1 arg2 arg3
96 }