docopt.go icon indicating copy to clipboard operation
docopt.go copied to clipboard

boolean options don't seem to work

Open tmbdev opened this issue 5 years ago • 4 comments

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
$ 

tmbdev avatar Mar 24 '20 07:03 tmbdev

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>

gwijnja avatar Jun 30 '20 10:06 gwijnja