docopt: Index | Examples | Files | Directories

package docopt

import ""

Package docopt parses command-line arguments based on a help message.

⚠ Use the alias “docopt-go”:

import ""


$ go get



Package Files


func Parse Uses

func Parse(doc string, argv []string, help bool, version string,
    optionsFirst bool, exit ...bool) (map[string]interface{}, error)

Parse `argv` based on the command-line interface described in `doc`.

Given a conventional command-line help message, docopt creates a parser and processes the arguments. See for a description of the help message format. If `argv` is `nil`, `os.Args[1:]` is used.

docopt returns a map of option names to the values parsed from `argv`, and an error or `nil`.

Set `help` to `false` to disable automatic help messages on `-h` or `--help`. If `version` is a non-empty string, it will be printed when `--version` is specified. Set `optionsFirst` to `true` to require that options always come before positional arguments; otherwise they can overlap.

By default, docopt calls `os.Exit(0)` if it handled a built-in option such as `-h` or `--version`. If the user errored with a wrong command or options, docopt exits with a return code of 1. To stop docopt from calling `os.Exit()` and to handle your own return codes, pass an optional last parameter of `false` for `exit`.


usage := `Usage:
  config_example tcp [<host>] [--force] [--timeout=<seconds>]
  config_example serial <port> [--baud=<rate>] [--timeout=<seconds>]
  config_example -h | --help | --version`
// parse the command line `comfig_example tcp --force`
argv := []string{"tcp", "", "--force"}
arguments, _ := Parse(usage, argv, true, "0.1.1rc", false)
// sort the keys of the arguments map
var keys []string
for k := range arguments {
    keys = append(keys, k)
// print the argument keys and values
for _, k := range keys {
    fmt.Printf("%9s %v\n", k, arguments[k])


   --baud <nil>
  --force true
   --help false
--timeout <nil>
--version false
       -h false
   <port> <nil>
   serial false
      tcp true

type LanguageError Uses

type LanguageError struct {
    // contains filtered or unexported fields

LanguageError records an error with the doc string.

func (LanguageError) Error Uses

func (e LanguageError) Error() string

type UserError Uses

type UserError struct {
    Usage string
    // contains filtered or unexported fields

UserError records an error with program arguments.

func (UserError) Error Uses

func (e UserError) Error() string



Package docopt imports 6 packages (graph) and is imported by 28 packages. Updated 2016-07-20. Refresh now. Tools for package owners.