alecthomas / kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser
MIT License
3.5k stars 272 forks source link

Handle --version just like --help #338

Closed arjunmahishi closed 1 year ago

arjunmahishi commented 1 year ago

When the version flag is passed, the application should do nothing but print the version and exit. But currently, the pre-action for the version flag gets executed AFTER the default for all the other flags are set.

Setting defaults involves validation like checking for file's existence etc. These validation are not necessary when the --version flag is passed. This PR fixes that

Example scenario where it was failing

var (
    config = kingpin.Flag("config", "path to config").Short('c').Default("config.yaml").ExistingFile()
)

func main() {
    kingpin.Version("0.0.1")
    kingpin.Parse()

    // ...
}

Would throw this error:

 ➜  go run . --version
main: error: path 'config.yaml' does not exist, try --help
exit status 1
iamFrancescoFerro commented 4 months ago

Hasn't this introduced a bug (or a backward compatibility issue) ?

We had several commands using the --version flag

./cli deploy --region east --version 1.0.0

(which is a simple string for us) and with this commit the Default(..) for all the flags are not being honored anymore because the parsing exist before when the version flag is detected