From 68948b2ac14c5cda057c88f1d7cba992ca053a2c Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 31 Mar 2024 12:05:26 -0400 Subject: [PATCH 1/3] restore 100% code coverage --- parse.go | 7 ------- parse_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/parse.go b/parse.go index 0bdddc7..c88faae 100644 --- a/parse.go +++ b/parse.go @@ -85,13 +85,6 @@ func MustParse(dest ...interface{}) *Parser { // mustParse is a helper that facilitates testing func mustParse(config Config, dest ...interface{}) *Parser { - if config.Exit == nil { - config.Exit = os.Exit - } - if config.Out == nil { - config.Out = os.Stdout - } - p, err := NewParser(config, dest...) if err != nil { fmt.Fprintln(config.Out, err) diff --git a/parse_test.go b/parse_test.go index 06e7a76..1512800 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1735,3 +1735,11 @@ func TestSubcommandGlobalFlag_InCommand_Strict_Inner(t *testing.T) { assert.False(t, args.Global) assert.True(t, args.Sub.Guard) } + +func TestExitFunctionAndOutStreamGetFilledIn(t *testing.T) { + var args struct{} + p, err := NewParser(Config{}, &args) + require.NoError(t, err) + assert.NotNil(t, p.config.Exit) // go prohibits function pointer comparison + assert.Equal(t, p.config.Out, os.Stdout) +} From 3ddfffdcd33a4419f68581e515113dbfbcfaaa7d Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Tue, 2 Apr 2024 12:05:00 -0400 Subject: [PATCH 2/3] add test for help and usage when a --version flag is present --- usage_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/usage_test.go b/usage_test.go index 1a64ad4..b6ce8e5 100644 --- a/usage_test.go +++ b/usage_test.go @@ -260,6 +260,39 @@ Options: assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) } +type userDefinedVersionFlag struct { + ShowVersion bool `arg:"--version" help:"this is a user-defined version flag"` +} + +// Version returns the version for this program +func (userDefinedVersionFlag) Version() string { + return "example 3.2.1" +} + +func TestUsageWithUserDefinedVersionFlag(t *testing.T) { + expectedUsage := "example 3.2.1\nUsage: example [--version]" + + expectedHelp := ` +example 3.2.1 +Usage: example [--version] + +Options: + --version this is a user-defined version flag + --help, -h display this help and exit +` + os.Args[0] = "example" + p, err := NewParser(Config{}, &userDefinedVersionFlag{}) + require.NoError(t, err) + + var help bytes.Buffer + p.WriteHelp(&help) + assert.Equal(t, expectedHelp[1:], help.String()) + + var usage bytes.Buffer + p.WriteUsage(&usage) + assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) +} + type described struct{} // Described returns the description for this program From 8a917260c38a68937883b35da0851d3da179d9a2 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Tue, 2 Apr 2024 12:10:52 -0400 Subject: [PATCH 3/3] add a test case with single-level subcommands --- usage_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/usage_test.go b/usage_test.go index b6ce8e5..f613471 100644 --- a/usage_test.go +++ b/usage_test.go @@ -448,6 +448,50 @@ Options: assert.Equal(t, expectedUsage, usage.String()) } +func TestUsageWithSubcommands(t *testing.T) { + expectedUsage := "Usage: example child [--values VALUES]" + + expectedHelp := ` +Usage: example child [--values VALUES] + +Options: + --values VALUES Values + +Global options: + --verbose, -v verbosity level + --help, -h display this help and exit +` + + var args struct { + Verbose bool `arg:"-v" help:"verbosity level"` + Child *struct { + Values []float64 `help:"Values"` + } `arg:"subcommand:child"` + } + + os.Args[0] = "example" + p, err := NewParser(Config{}, &args) + require.NoError(t, err) + + _ = p.Parse([]string{"child"}) + + var help bytes.Buffer + p.WriteHelp(&help) + assert.Equal(t, expectedHelp[1:], help.String()) + + var help2 bytes.Buffer + p.WriteHelpForSubcommand(&help2, "child") + assert.Equal(t, expectedHelp[1:], help2.String()) + + var usage bytes.Buffer + p.WriteUsage(&usage) + assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) + + var usage2 bytes.Buffer + p.WriteUsageForSubcommand(&usage2, "child") + assert.Equal(t, expectedUsage, strings.TrimSpace(usage2.String())) +} + func TestUsageWithNestedSubcommands(t *testing.T) { expectedUsage := "Usage: example child nested [--enable] OUTPUT"