example_test.go 3.0 KB

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