doc.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Package docopt parses command-line arguments based on a help message.
  3. Given a conventional command-line help message, docopt processes the arguments.
  4. See https://github.com/docopt/docopt#help-message-format for a description of
  5. the help message format.
  6. This package exposes three different APIs, depending on the level of control
  7. required. The first, simplest way to parse your docopt usage is to just call:
  8. docopt.ParseDoc(usage)
  9. This will use os.Args[1:] as the argv slice, and use the default parser
  10. options. If you want to provide your own version string and args, then use:
  11. docopt.ParseArgs(usage, argv, "1.2.3")
  12. If the last parameter (version) is a non-empty string, it will be printed when
  13. --version is given in the argv slice. Finally, we can instantiate our own
  14. docopt.Parser which gives us control over how things like help messages are
  15. printed and whether to exit after displaying usage messages, etc.
  16. parser := &docopt.Parser{
  17. HelpHandler: docopt.PrintHelpOnly,
  18. OptionsFirst: true,
  19. }
  20. opts, err := parser.ParseArgs(usage, argv, "")
  21. In particular, setting your own custom HelpHandler function makes unit testing
  22. your own docs with example command line invocations much more enjoyable.
  23. All three of these return a map of option names to the values parsed from argv,
  24. and an error or nil. You can get the values using the helpers, or just treat it
  25. as a regular map:
  26. flag, _ := opts.Bool("--flag")
  27. secs, _ := opts.Int("<seconds>")
  28. Additionally, you can `Bind` these to a struct, assigning option values to the
  29. exported fields of that struct, all at once.
  30. var config struct {
  31. Command string `docopt:"<cmd>"`
  32. Tries int `docopt:"-n"`
  33. Force bool // Gets the value of --force
  34. }
  35. opts.Bind(&config)
  36. */
  37. package docopt