You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
891 B

  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. }