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

app.Usage() ignores default command and doesn't recognize flags related to the default command. #189

Open
kashishm opened this issue Jun 9, 2017 · 1 comment

Comments

@kashishm
Copy link

kashishm commented Jun 9, 2017

package main
import (
	"gopkg.in/alecthomas/kingpin.v2"
	"os"
)
var (
	app     = kingpin.New("app", "A command line tool.")
	old     = app.Command("old", "").Hidden().Default()
	verbose = old.Flag("verbose", "verbose mode").Hidden().Bool()
)
func main() {
	kingpin.MustParse(app.Parse(os.Args[1:]))
	app.Usage(os.Args[1:])
}

Command

go run app.go old --verbose

Output

usage: App old [<flags>]

Flags:
  --help  Show context-sensitive help (also try --help-long and --help-man).

Command

go run app.go --verbose

Output

app: error: unknown long flag '--verbose'
exit status 1

Expected behavior

go run app.go --verbose and go run app.go old --verbose should both display the same usage without any error as old is a default command.

app.parse works fine as it doesn't ignore default command.

@alecthomas
Copy link
Owner

alecthomas commented Jul 27, 2017

I see what you're saying, and it is an issue, but the solution complicates the parsing logic considerably. Currently, the parser is effectively a state machine. Each argument is parsed, and the state changes. When a command is encountered, the flags for that command are added to the set of flags that are eligible for evaluation, and parsing continues.

To fix this issue, the parser would need to look forward in the command line to see if a command is provided, then, if not, rewind and parse the flags as if the default command were provided.

I'm somewhat inclined to simply remove support for default commands in v3, as it has been an unexpected source of complexity.

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

2 participants