Skip to content

Wrong optional keyword match #71

Open
@haamed

Description

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>
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions