1
0

example_test.go 891 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package docopt
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func ExampleParse() {
  7. usage := `Usage:
  8. config_example tcp [<host>] [--force] [--timeout=<seconds>]
  9. config_example serial <port> [--baud=<rate>] [--timeout=<seconds>]
  10. config_example -h | --help | --version`
  11. // parse the command line `comfig_example tcp 127.0.0.1 --force`
  12. argv := []string{"tcp", "127.0.0.1", "--force"}
  13. arguments, _ := Parse(usage, argv, true, "0.1.1rc", false)
  14. // sort the keys of the arguments map
  15. var keys []string
  16. for k := range arguments {
  17. keys = append(keys, k)
  18. }
  19. sort.Strings(keys)
  20. // print the argument keys and values
  21. for _, k := range keys {
  22. fmt.Printf("%9s %v\n", k, arguments[k])
  23. }
  24. // output:
  25. // --baud <nil>
  26. // --force true
  27. // --help false
  28. // --timeout <nil>
  29. // --version false
  30. // -h false
  31. // <host> 127.0.0.1
  32. // <port> <nil>
  33. // serial false
  34. // tcp true
  35. }