@@ -31,19 +31,23 @@ func tryExecuteExtension(cctx *CommandContext, tcmd *TemporalCommand) (error, bo
3131 // Find the deepest matching built-in command and remaining args.
3232 foundCmd , remainingArgs , findErr := tcmd .Command .Find (cctx .Options .Args )
3333
34- // Check if remaining args include positional args (not just flags).
35- // If not, a built-in command fully handles this - no extension needed.
36- hasPosArgs := slices .ContainsFunc (remainingArgs , isPosArg )
37- if findErr == nil && ! hasPosArgs {
38- return nil , false
39- }
34+ // Cobra normally adds --help/-h before parsing, but extension dispatch
35+ // pre-parses flags before Cobra's execution path runs. We Initialize it so that
36+ // help is treated as a normal CLI flag instead of surfacing as pflag.ErrHelp from flag parsing.
37+ foundCmd .InitDefaultHelpFlag ()
4038
4139 // Group args into these lists:
4240 // - cliParseArgs: args to validate (subset of cliPassArgs)
4341 // - cliPassArgs: known CLI args to pass to extension
4442 // - extArgs: args to pass to extension and use for extension lookup
4543 cliParseArgs , cliPassArgs , extArgs := groupArgs (foundCmd , remainingArgs )
4644
45+ // Check if remaining args include positional args (not just flags).
46+ // If not, a built-in command fully handles this - no extension needed.
47+ if findErr == nil && ! slices .ContainsFunc (extArgs , isPosArg ) {
48+ return nil , false
49+ }
50+
4751 // Search for an extension executable.
4852 cmdPrefix := strings .Fields (foundCmd .CommandPath ())
4953 extPath , extArgs := lookupExtension (cmdPrefix , extArgs )
@@ -95,6 +99,13 @@ func groupArgs(foundCmd *cobra.Command, args []string) (cliParseArgs, cliPassArg
9599
96100 name , hasInline := parseFlagArg (arg )
97101 if f , takesValue := lookupFlag (foundCmd , name ); f != nil {
102+ // Help flag after positional args should go to extArgs so it
103+ // gets forwarded to extensions (e.g. "temporal foo bar --help").
104+ // Before positional args it stays in cliPassArgs for Cobra to handle.
105+ if f .Name == "help" && seenPos {
106+ extArgs = append (extArgs , arg )
107+ continue
108+ }
98109 // Known CLI flag: goes to cliPassArgs.
99110 // Flags in cliArgsToParseForExtension also go to cliParseArgs.
100111 shouldParse := cliArgsToParseForExtension [f .Name ]
0 commit comments