From 425304205580254c6ea87908f35848c46eb99d5e Mon Sep 17 00:00:00 2001 From: Eric Greer Date: Tue, 20 Aug 2019 18:47:44 -0700 Subject: [PATCH] fixed missing default values in some codepaths for issue #46 --- helpValues.go | 8 +++++++- helpValues_test.go | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/helpValues.go b/helpValues.go index 8a5146b..1db46a6 100644 --- a/helpValues.go +++ b/helpValues.go @@ -173,6 +173,13 @@ func (h *Help) parseFlagsToHelpFlags(flags []*Flag) { continue } + // parse help values out if the flag hasn't been parsed yet + if !f.parsed { + f.parsed = true + // parse the default value as a string and remember it for help output + f.defaultValue, _ = f.returnAssignmentVarValueAsString() + } + // determine the default value based on the assignment variable defaultValue := f.defaultValue @@ -196,7 +203,6 @@ func (h *Help) parseFlagsToHelpFlags(flags []*Flag) { Description: f.Description, DefaultValue: defaultValue, } - h.AddFlagToHelp(newHelpFlag) } } diff --git a/helpValues_test.go b/helpValues_test.go index 60f2c94..d4b9572 100644 --- a/helpValues_test.go +++ b/helpValues_test.go @@ -33,7 +33,7 @@ func TestHelpOutput(t *testing.T) { // defer debugOff() p := flaggy.NewParser("testCommand") - p.Description = "Description goes here. Get more information at http://flaggy.flag." + p.Description = "Description goes here. Get more information at https://github.com/integrii/flaggy." scA := flaggy.NewSubcommand("subcommandA") scA.ShortName = "a" scA.Description = "Subcommand A is a command that does stuff" @@ -51,7 +51,7 @@ func TestHelpOutput(t *testing.T) { scA.AddPositionalValue(&posA, "testPositionalA", 2, false, "Test positional A does some things with a positional value.") scB.AddPositionalValue(&posB, "hiddenPositional", 1, false, "Hidden test positional B does some less than serious things with a positional value.") scB.PositionalFlags[0].Hidden = true - var stringFlag string + var stringFlag = "defaultStringHere" var intFlag int var boolFlag bool var durationFlag time.Duration @@ -61,6 +61,6 @@ func TestHelpOutput(t *testing.T) { p.Duration(&durationFlag, "d", "durationFlag", "This is a test duration flag that does some untimely stuff.") p.AdditionalHelpPrepend = "This is a prepend for help" p.AdditionalHelpAppend = "This is an append for help" - p.ShowHelpWithMessage("This is a help addon message") p.ParseArgs([]string{"subcommandA", "subcommandB", "hiddenPositional1"}) + p.ShowHelpWithMessage("This is a help message on exit") }