docopt.go
docopt.go copied to clipboard
boolean options don't seem to work
Boolean options don't seem to work the way they should. Here is an example:
package main
import (
"fmt"
"github.com/docopt/docopt-go"
)
var usage string = `
Mytest.
Usage:
mytest [options]
Options:
--flag a flag
`
func main() {
opts, _ := docopt.ParseDoc(usage)
flag, err := opts.Bool("--flag")
fmt.Println(flag)
fmt.Println(err)
}
$ go run mytest.go
false
key: "--flag" failed type conversion
$ go run mytest.go --flag
--flag requires argument
Usage:
mytest [options]
exit status 1
$ go run mytest.go --flag=true
false
key: "--flag" failed type conversion
$ go version
go version go1.14.1 linux/amd64
$
You are using a tab between --flag and a. The docopt website says:
Use two spaces to separate options with their informal description.
If you replace the tab with two (or more) spaces, it works:
package main
import (
"fmt"
"github.com/docopt/docopt-go"
)
var usage string = `
Mytest.
Usage:
mytest [options]
Options:
--flag a flag
`
func main() {
opts, _ := docopt.ParseDoc(usage)
flag, err := opts.Bool("--flag")
fmt.Println(flag)
fmt.Println(err)
}
$ go run mytest.go
false
<nil>
$ go run mytest.go --flag
true
<nil>