example_test.go 2.6 KB

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