Skip to content

Commit

Permalink
OCM-6646 | fix: detect presence of --help processing args
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McKay committed Mar 12, 2024
1 parent 7e77d47 commit a143544
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions pkg/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,13 @@ func PreprocessUnknownFlagsWithId(cmd *cobra.Command, argv []string) error {
var validArgs []string
var upcomingValue bool

// If help is called, regardless of other flags, return we want help.
// Also say we need help if the command isn't runnable.
helpVal, err := cmd.Flags().GetBool("help")
if err != nil {
// should be impossible to get here as we always declare a help
// flag in InitDefaultHelpFlag()
panic(fmt.Errorf("\"help\" flag is incorrectly declared as non-bool. Please correct your code. Error: %w", err))
}
if helpVal {
return pflag.ErrHelp
}

foundId := false
for i, arg := range argv {
switch {
// If help is called, regardless of other flags, return we want help.
// Also say we need help if the command isn't runnable.
case arg == "--help":
return pflag.ErrHelp
// Upcoming value from a space-separated value
case upcomingValue:
if strings.HasPrefix(arg, "-") {
Expand Down Expand Up @@ -259,7 +251,7 @@ func PreprocessUnknownFlagsWithId(cmd *cobra.Command, argv []string) error {
}
}

err = flags.Parse(validArgs)
err := flags.Parse(validArgs)
if err != nil {
return err
}
Expand Down

0 comments on commit a143544

Please sign in to comment.