aviddiviner / docopt-go

A command-line arguments parser that will make you smile.
http://docopt.org/
MIT License
8 stars 1 forks source link

When a line follows `Options:` declaration, options don't parse correctly #3

Open ghostsquad opened 6 years ago

ghostsquad commented 6 years ago

Description

When a non-option line comes after the Options: declaration, parsing fails in weird ways. Not only are options missing, but -h and --help are now split. This doesn't happen at try.docopt.org.

Repro

  1. Start with the options shortcut example
package main

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

func main() {
    usage := `Example of program which uses [options] shortcut in pattern.

Usage:
  options_shortcut_example [options] <port>
  options_shortcut_example -h | --help

Options:
  -h --help                show this help message and exit
  --version                show version and exit
  -n, --number N           use N as a number
  -t, --timeout TIMEOUT    set timeout TIMEOUT seconds
  --apply                  apply changes to database
  -q                       operate in quiet mode`

    arguments, _ := docopt.Parse(usage, nil, false, "1.0.0rc2", false)
    fmt.Println(arguments)
}
  1. Modify as such
    Options:
    +These are things
    -h --help                show this help message and exit

Expected Behavior

$ go run options_shortcut_example.go --help
map[<port>:<nil> --help:true --version:false --number:<nil> --timeout:<nil> --apply:false -q:false]

Actual Behavior

$ go run options_shortcut_example.go --help
map[<port>:<nil> -h:false --help:true]
aviddiviner commented 6 years ago

Hmm, yes, the parser in docopt.go is actually still a bit of a spaghetti mess imho. I started playing around with fuzz-testing it and I found a whole lot of bugs, and then I pretty much left things at that, resolving to maybe one day just rewrite the whole thing.

I'm not going to have time to look at this any time soon, I'm sorry. But I'll leave this issue here for posterity. Maybe we can make a test case from it some day and someone can fix it if they want.