Skip to content

Commit

Permalink
Silence flag output unless from help
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Nov 10, 2024
1 parent 2ee23e7 commit 3e9b5e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 9 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,20 @@ func FromConfigFileFlagPath(cliFlagName, defaultPath, description string, config
return func(c *options) error {

commandLine := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
commandLine.SetOutput(io.Discard)

configPath := commandLine.String(cliFlagName, defaultPath, description)
if err := commandLine.Parse(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {

// This weirdness is because Parse will print out things if it doesnt recognise them
// as this section of the code has no knowledge of what structs are actually defined
// we want to only print help if asked for it
commandLine.SetOutput(os.Stdout)
fmt.Fprintf(os.Stdout, "Usage of %s:\n", os.Args[0])
commandLine.PrintDefaults()
commandLine.SetOutput(io.Discard)

return flag.ErrHelp
}

Expand Down
1 change: 0 additions & 1 deletion entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ func TestConfigurationHelp(t *testing.T) {
t.Fatal("should return that help was asked for")
}

t.Fail()
}

0 comments on commit 3e9b5e3

Please sign in to comment.