Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong optional keyword match #71

Open
haamed opened this issue Nov 16, 2019 · 0 comments
Open

Wrong optional keyword match #71

haamed opened this issue Nov 16, 2019 · 0 comments

Comments

@haamed
Copy link

haamed commented Nov 16, 2019

It seems if a command-like argument (like a keyword) is use in an optional block, it matches the wrong thing.
In the following repro, "cmd" is matched with "opt1" if "keyword" is not specified. If it is specified it works fine.
Note that if instead of a keyword, an option (like "--option") is specified it works fine.

package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
)

func main() {
	usage := `To match optional with keyword.

Usage:
  keyword-match: [keyword <opt1>] [<args>...]
`
	args, err := docopt.ParseArgs(usage, []string{"cmd"}, "")
	fmt.Printf("%#v\n", args) // BUG prints: docopt.Opts{"<opt1>":"cmd", "keyword":false}
	fmt.Printf("%#v\n", err)  // prints: <nil>

	args, err = docopt.ParseArgs(usage, []string{"keyword", "opt1", "cmd"}, "")
	fmt.Printf("%#v\n", args) // prints: docopt.Opts{"<args>":[]string{"cmd"}, "<opt1>":"opt1", "keyword":true}
	fmt.Printf("%#v\n", err)  // prints: <nil>

	usage = `To match optional with option.

Usage:
  keyword-match: [--option=<opt1>] [<args>...]
`
	args, err = docopt.ParseArgs(usage, []string{"cmd"}, "")
	fmt.Printf("%#v\n", args) // prints: docopt.Opts{"--option":interface {}(nil), "<args>":[]string{"cmd"}}
	fmt.Printf("%#v\n", err)  // prints: <nil>

	args, err = docopt.ParseArgs(usage, []string{"--option", "opt1", "cmd"}, "")
	fmt.Printf("%#v\n", args) // prints: docopt.Opts{"--option":"opt1", "<args>":[]string{"cmd"}}
	fmt.Printf("%#v\n", err)  // prints: <nil>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant