change VersionFlag to a BeforeReset hook
This commit is contained in:
+25
-1
@@ -1544,7 +1544,8 @@ func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
|
|||||||
|
|
||||||
func TestHelpShouldStillWork(t *testing.T) {
|
func TestHelpShouldStillWork(t *testing.T) {
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
Dir string `type:"existingdir" default:"missing-dir"`
|
Dir string `type:"existingdir" default:"missing-dir"`
|
||||||
|
File string `type:"existingfile" default:"testdata/missing.txt"`
|
||||||
}
|
}
|
||||||
var cli CLI
|
var cli CLI
|
||||||
w := &strings.Builder{}
|
w := &strings.Builder{}
|
||||||
@@ -1563,6 +1564,29 @@ func TestHelpShouldStillWork(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVersionFlagShouldStillWork(t *testing.T) {
|
||||||
|
type CLI struct {
|
||||||
|
Dir string `type:"existingdir" default:"missing-dir"`
|
||||||
|
File string `type:"existingfile" default:"testdata/missing.txt"`
|
||||||
|
Version kong.VersionFlag
|
||||||
|
}
|
||||||
|
var cli CLI
|
||||||
|
w := &strings.Builder{}
|
||||||
|
k := mustNew(t, &cli, kong.Writers(w, w))
|
||||||
|
rc := -1 // init nonzero to help assert help hook was called
|
||||||
|
k.Exit = func(i int) {
|
||||||
|
rc = i
|
||||||
|
}
|
||||||
|
_, err := k.Parse([]string{"--version"})
|
||||||
|
t.Log(w.String())
|
||||||
|
// checking return code validates the help hook was called
|
||||||
|
assert.Zero(t, rc)
|
||||||
|
// allow for error propagation from other validation (only for the
|
||||||
|
// sake of this test, due to the exit function not actually exiting the
|
||||||
|
// program; errors will not propagate in the real world).
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
|
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ func (c ConfigFlag) BeforeResolve(kong *Kong, ctx *Context, trace *Path) error {
|
|||||||
// VersionFlag is a flag type that can be used to display a version number, stored in the "version" variable.
|
// VersionFlag is a flag type that can be used to display a version number, stored in the "version" variable.
|
||||||
type VersionFlag bool
|
type VersionFlag bool
|
||||||
|
|
||||||
// BeforeApply writes the version variable and terminates with a 0 exit status.
|
// BeforeReset writes the version variable and terminates with a 0 exit status.
|
||||||
func (v VersionFlag) BeforeApply(app *Kong, vars Vars) error {
|
func (v VersionFlag) BeforeReset(app *Kong, vars Vars) error {
|
||||||
fmt.Fprintln(app.Stdout, vars["version"])
|
fmt.Fprintln(app.Stdout, vars["version"])
|
||||||
app.Exit(0)
|
app.Exit(0)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user