You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 28, 2018. It is now read-only.
**docopt** parses command-line arguments based on a help message. Don't
45
-
write parser code: a good help message already has all the necessary
46
-
information in it.
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.
47
44
48
45
## Installation
49
46
50
-
⚠ Use the alias “docopt-go”. To use docopt in your Go code:
47
+
⚠ Use the alias "docopt-go". To use docopt in your Go code:
51
48
52
49
```go
53
50
import"github.com/docopt/docopt-go"
54
51
```
55
52
56
-
To install docopt according to your `$GOPATH`:
53
+
To install docopt in your `$GOPATH`:
57
54
58
55
```console
59
56
$ go get github.com/docopt/docopt-go
60
57
```
61
58
62
59
## API
63
60
61
+
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.
62
+
63
+
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:
64
+
65
+
```go
66
+
docopt.ParseDoc(usage)
67
+
```
68
+
69
+
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:
70
+
64
71
```go
65
-
funcParse(doc string, argv []string, help bool, version string,
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.
76
+
77
+
```go
78
+
parser:= &docopt.Parser{
79
+
HelpHandler: docopt.PrintHelpOnly,
80
+
OptionsFirst: true,
81
+
}
82
+
opts, err:= parser.ParseArgs(usage, argv, "")
83
+
```
84
+
85
+
In particular, setting your own custom `HelpHandler` function makes unit testing your own docs with example command line invocations much more enjoyable.
86
+
87
+
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:
88
+
89
+
```go
90
+
flag, _:= opts.Bool("--flag")
91
+
secs, _:= opts.Int("<seconds>")
92
+
```
93
+
94
+
Additionally, you can `Bind` these to a struct, assigning option values to the
95
+
exported fields of that struct, all at once.
96
+
97
+
```go
98
+
varconfigstruct {
99
+
Commandstring`docopt:"<cmd>"`
100
+
Triesint`docopt:"-n"`
101
+
Forcebool// Gets the value of --force
102
+
}
103
+
opts.Bind(&config)
67
104
```
68
-
Parse `argv` based on the command-line interface described in `doc`.
69
105
70
-
Given a conventional command-line help message, docopt creates a parser and
71
-
processes the arguments. See
72
-
https://github.com/docopt/docopt#help-message-format for a description of the
73
-
help message format. If `argv` is `nil`, `os.Args[1:]` is used.
106
+
More documentation is available at [godoc.org](https://godoc.org/github.com/docopt/docopt-go).
74
107
75
-
docopt returns a map of option names to the values parsed from `argv`, and an
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](examples/unit_test/unit_test.go).
80
111
81
-
## Testing
112
+
## Tests
82
113
83
-
All tests from the Python version are implemented and passing
84
-
at [Travis CI](https://travis-ci.org/docopt/docopt.go). New
85
-
language-agnostic tests have been added
86
-
to [test_golang.docopt](test_golang.docopt).
114
+
All 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).
0 commit comments