A command-line arguments parser that will make you smile.
Find a file
2026-06-02 00:24:38 +02:00
examples Move repo to the public organization 2026-06-01 17:56:58 +02:00
.gitignore Move repo to bwi suisse ag Git server 2026-06-01 17:08:59 +02:00
.travis.yml chore(.travis.yml): add go 1.9 to test matrix. 2018-01-07 10:03:56 -07:00
default.nix Add default.nix via flake-compat 2026-06-02 00:24:38 +02:00
doc.go Bind option keys to fields more intelligently, update docs 2016-12-08 06:38:11 -05:00
docopt.go Remove stray debug print 2025-02-05 05:01:45 +01:00
docopt_test.go Fix tests to expect Opts 2016-12-01 13:24:38 -05:00
error.go Break things up into separate files 2016-11-08 16:12:06 -05:00
example_test.go Update examples to use variadic arguments 2016-12-08 08:03:34 -05:00
flake.lock Add default.nix via flake-compat 2026-06-02 00:24:38 +02:00
flake.nix Add default.nix via flake-compat 2026-06-02 00:24:38 +02:00
go.mod Move repo to the public organization 2026-06-01 17:56:58 +02:00
go.sum Fix module path 2025-02-05 04:44:59 +01:00
LICENSE Update README doc and license 2016-11-07 13:24:27 -05:00
opts.go Bind --double-dash flags to untagged TitleCase fields 2016-12-19 09:53:58 -05:00
opts_test.go Bind --double-dash flags to untagged TitleCase fields 2016-12-19 09:53:58 -05:00
pattern.go Add environment variable capability 2025-02-05 03:56:32 +01:00
README.md Move repo to the public organization 2026-06-01 17:56:58 +02:00
test_golang.docopt add fix for uppercase testing 2013-08-30 12:47:34 -07:00
testcases.docopt add all current code 2013-08-29 17:09:51 -07:00
token.go Break things up into separate files 2016-11-08 16:12:06 -05:00

docopt-go

Build Status Coverage Status GoDoc

An implementation of docopt in the Go programming language.

docopt helps you create beautiful command-line interfaces easily:

package main

import (
	"fmt"
	"git.bwi-suisse.ch/public/docopt.go"
)

func main() {
	  usage := `Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.`

	  arguments, _ := docopt.ParseDoc(usage)
	  fmt.Println(arguments)
}

docopt parses command-line arguments based on a help message. Don't write parser code: a good help message already has all the necessary information in it.

Installation

import "git.bwi-suisse.ch/public/docopt.go"

To install docopt in your $GOPATH:

$ go get git.bwi-suisse.ch/public/docopt.go

API

Given a conventional command-line help message, docopt processes the arguments. See https://github.com/docopt/docopt#help-message-format for a description of the help message format.

This package exposes three different APIs, depending on the level of control required. The first, simplest way to parse your docopt usage is to just call:

docopt.ParseDoc(usage)

This will use os.Args[1:] as the argv slice, and use the default parser options. If you want to provide your own version string and args, then use:

docopt.ParseArgs(usage, argv, "1.2.3")

If the last parameter (version) is a non-empty string, it will be printed when --version is given in the argv slice. Finally, we can instantiate our own docopt.Parser which gives us control over how things like help messages are printed and whether to exit after displaying usage messages, etc.

parser := &docopt.Parser{
  HelpHandler: docopt.PrintHelpOnly,
  OptionsFirst: true,
}
opts, err := parser.ParseArgs(usage, argv, "")

In particular, setting your own custom HelpHandler function makes unit testing your own docs with example command line invocations much more enjoyable.

All three of these return a map of option names to the values parsed from argv, and an error or nil. You can get the values using the helpers, or just treat it as a regular map:

flag, _ := opts.Bool("--flag")
secs, _ := opts.Int("<seconds>")

Additionally, you can Bind these to a struct, assigning option values to the exported fields of that struct, all at once.

var config struct {
  Command string `docopt:"<cmd>"`
  Tries   int    `docopt:"-n"`
  Force   bool   // Gets the value of --force
}
opts.Bind(&config)

More documentation is available at godoc.org.

Unit Testing

Unit testing your own usage docs is recommended, so you can be sure that for a given command line invocation, the expected options are set. An example of how to do this is in the examples folder.

Tests

All tests from the Python version are implemented and passing at Travis CI. New language-agnostic tests have been added to test_golang.docopt.

To run tests for docopt-go, use go test.