Skip to content

Commit b4b1df3

Browse files
arjunmahishialecthomas
authored andcommitted
Handle --version just like --help
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 got executed AFTER the default for all the other flags were set. Setting defaults involves validation like checking for file's existence etc. These validation are not necessary when the --version flag is passed. This commit fixes that
1 parent 649b5a2 commit b4b1df3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ func (a *Application) setDefaults(context *ParseContext) error {
413413
if flag.name == "help" {
414414
return nil
415415
}
416+
417+
if flag.name == "version" {
418+
return nil
419+
}
416420
flagElements[flag.name] = element
417421
}
418422
}

app_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,14 @@ func TestCmdValidation(t *testing.T) {
434434
_, err = c.Parse([]string{"cmd", "--a", "A"})
435435
assert.NoError(t, err)
436436
}
437+
438+
func TestVersion(t *testing.T) {
439+
c := newTestApp()
440+
c.Flag("config", "path to config file").Default("config.yaml").ExistingFile()
441+
c.Version("1.0.0")
442+
443+
// the pre-action for version should be executed without running validation
444+
// for ExistingFile
445+
_, err := c.Parse([]string{"--version"})
446+
assert.NoError(t, err)
447+
}

0 commit comments

Comments
 (0)