example_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // These examples demonstrate more intricate uses of the flag package.
  5. package flag_test
  6. import (
  7. "errors"
  8. "flag"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // Example 1: A single string flag called "species" with default value "gopher".
  14. var species = flag.String("species", "gopher", "the species we are studying")
  15. // Example 2: Two flags sharing a variable, so we can have a shorthand.
  16. // The order of initialization is undefined, so make sure both use the
  17. // same default value. They must be set up with an init function.
  18. var gopherType string
  19. func init() {
  20. const (
  21. defaultGopher = "pocket"
  22. usage = "the variety of gopher"
  23. )
  24. flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
  25. flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
  26. }
  27. // Example 3: A user-defined flag type, a slice of durations.
  28. type interval []time.Duration
  29. // String is the method to format the flag's value, part of the flag.Value interface.
  30. // The String method's output will be used in diagnostics.
  31. func (i *interval) String() string {
  32. return fmt.Sprint(*i)
  33. }
  34. // Set is the method to set the flag value, part of the flag.Value interface.
  35. // Set's argument is a string to be parsed to set the flag.
  36. // It's a comma-separated list, so we split it.
  37. func (i *interval) Set(value string) error {
  38. // If we wanted to allow the flag to be set multiple times,
  39. // accumulating values, we would delete this if statement.
  40. // That would permit usages such as
  41. // -deltaT 10s -deltaT 15s
  42. // and other combinations.
  43. if len(*i) > 0 {
  44. return errors.New("interval flag already set")
  45. }
  46. for _, dt := range strings.Split(value, ",") {
  47. duration, err := time.ParseDuration(dt)
  48. if err != nil {
  49. return err
  50. }
  51. *i = append(*i, duration)
  52. }
  53. return nil
  54. }
  55. // Define a flag to accumulate durations. Because it has a special type,
  56. // we need to use the Var function and therefore create the flag during
  57. // init.
  58. var intervalFlag interval
  59. func init() {
  60. // Tie the command-line flag to the intervalFlag variable and
  61. // set a usage message.
  62. flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
  63. }
  64. func Example() {
  65. // All the interesting pieces are with the variables declared above, but
  66. // to enable the flag package to see the flags defined there, one must
  67. // execute, typically at the start of main (not init!):
  68. // flag.Parse()
  69. // We don't run it here because this is not a main function and
  70. // the testing suite has already parsed the flags.
  71. }