- This is a fork of https://github.com/docopt/docopt.go
- With release 0.8.0, this fork abandons backward compatibility.
An implementation of docopt in the Go programming language.
docopt helps you create beautiful command-line interfaces easily:
package main
import (
"fmt"
"github.com/marco-m/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.MustParseDoc(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.
To use docopt in your Go code:
import "github.com/marco-m/docopt-go"
To install this fork:
$ go get github.com/marco-m/docopt-go
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.
opts, err := docopt.Parse(usage, args, "1.2.3")
opts := docopt.MustParse(usage, args, "1.2.3")
If the last parameter (version) is a non-empty string, it will be printed when --version
is given in the args slice.
The function returns a map of option names to the values parsed from args, 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 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.
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
.