• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H11-Jan-2018-821610

.gitignoreH A D11-Jan-2018286 2619

.travis.ymlH A D11-Jan-2018640 3325

LICENSEH A D11-Jan-20181.1 KiB2217

README.mdH A D11-Jan-20183.8 KiB11782

doc.goH A D11-Jan-20181.7 KiB501

docopt.goH A D11-Jan-201815 KiB576494

docopt_test.goH A D11-Jan-201841.1 KiB1,5201,363

error.goH A D11-Jan-2018816 5038

example_test.goH A D11-Jan-20181.4 KiB7042

opts.goH A D11-Jan-20187.6 KiB265182

opts_test.goH A D11-Jan-20187.4 KiB339317

pattern.goH A D11-Jan-201811.7 KiB551495

test_golang.docoptH A D11-Jan-2018126 107

testcases.docoptH A D11-Jan-201811.7 KiB958646

token.goH A D11-Jan-20182.5 KiB127113

README.md

1docopt-go
2=========
3
4[![Build Status](https://travis-ci.org/docopt/docopt.go.svg?branch=master)](https://travis-ci.org/docopt/docopt.go)
5[![Coverage Status](https://coveralls.io/repos/github/docopt/docopt.go/badge.svg)](https://coveralls.io/github/docopt/docopt.go)
6[![GoDoc](https://godoc.org/github.com/docopt/docopt.go?status.svg)](https://godoc.org/github.com/docopt/docopt.go)
7
8An implementation of [docopt](http://docopt.org/) in the [Go](http://golang.org/) programming language.
9
10**docopt** helps you create *beautiful* command-line interfaces easily:
11
12```go
13package main
14
15import (
16	"fmt"
17	"github.com/docopt/docopt-go"
18)
19
20func main() {
21	  usage := `Naval Fate.
22
23Usage:
24  naval_fate ship new <name>...
25  naval_fate ship <name> move <x> <y> [--speed=<kn>]
26  naval_fate ship shoot <x> <y>
27  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
28  naval_fate -h | --help
29  naval_fate --version
30
31Options:
32  -h --help     Show this screen.
33  --version     Show version.
34  --speed=<kn>  Speed in knots [default: 10].
35  --moored      Moored (anchored) mine.
36  --drifting    Drifting mine.`
37
38	  arguments, _ := docopt.ParseDoc(usage)
39	  fmt.Println(arguments)
40}
41```
42
43**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.
44
45## Installation
46
47⚠ Use the alias "docopt-go". To use docopt in your Go code:
48
49```go
50import "github.com/docopt/docopt-go"
51```
52
53To install docopt in your `$GOPATH`:
54
55```console
56$ go get github.com/docopt/docopt-go
57```
58
59## API
60
61Given 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.
62
63This 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:
64
65```go
66docopt.ParseDoc(usage)
67```
68
69This 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:
70
71```go
72docopt.ParseArgs(usage, argv, "1.2.3")
73```
74
75If 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.
76
77```go
78parser := &docopt.Parser{
79  HelpHandler: docopt.PrintHelpOnly,
80  OptionsFirst: true,
81}
82opts, err := parser.ParseArgs(usage, argv, "")
83```
84
85In particular, setting your own custom `HelpHandler` function makes unit testing your own docs with example command line invocations much more enjoyable.
86
87All 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:
88
89```go
90flag, _ := opts.Bool("--flag")
91secs, _ := opts.Int("<seconds>")
92```
93
94Additionally, you can `Bind` these to a struct, assigning option values to the
95exported fields of that struct, all at once.
96
97```go
98var config struct {
99  Command string `docopt:"<cmd>"`
100  Tries   int    `docopt:"-n"`
101  Force   bool   // Gets the value of --force
102}
103opts.Bind(&config)
104```
105
106More documentation is available at [godoc.org](https://godoc.org/github.com/docopt/docopt-go).
107
108## Unit Testing
109
110Unit 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](examples/unit_test/unit_test.go).
111
112## Tests
113
114All tests from the Python version are implemented and passing at [Travis CI](https://travis-ci.org/docopt/docopt-go). New language-agnostic tests have been added to [test_golang.docopt](test_golang.docopt).
115
116To run tests for docopt-go, use `go test`.
117