nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: spf13/cobra
Pull request #2229 (2025-02-02 17:19:01)

Allow to reset the templates to the default
Follow-up to #1956.

This commit allows a program to reset any of the three templates to their default behaviour, as it was possible to do before the change of #1956.

For example, one could imagine that a program could call `SetUsageTemplate("{{A short help}}")`, but if a particular set of arguments is passed in by the user, the program would want to reset to the default help by calling `SetUsageTemplate("")` (basically cancelling the previous call to `SetUsageTemplate()`).

This used to be possible before #1956, and this PR brings back this support.

Granted, this is a bit of a corner case, but with so many programs using Cobra, let's not make any assumptions and avoid unexpected changes. 

cc @aarzilli @jpmcb 


Patch:
diff --git a/command.go b/command.go
index 6904bfb..60b9c45 100644
--- a/command.go
+++ b/command.go
@@ -315,6 +315,10 @@ func (c *Command) SetUsageFunc(f func(*Command) error) {
 
 // SetUsageTemplate sets usage template. Can be defined by Application.
 func (c *Command) SetUsageTemplate(s string) {
+	if s == "" {
+		c.usageTemplate = nil
+		return
+	}
 	c.usageTemplate = tmpl(s)
 }
 
@@ -351,11 +355,19 @@ func (c *Command) SetCompletionCommandGroupID(groupID string) {
 
 // SetHelpTemplate sets help template to be used. Application can use it to set custom template.
 func (c *Command) SetHelpTemplate(s string) {
+	if s == "" {
+		c.helpTemplate = nil
+		return
+	}
 	c.helpTemplate = tmpl(s)
 }
 
 // SetVersionTemplate sets version template to be used. Application can use it to set custom template.
 func (c *Command) SetVersionTemplate(s string) {
+	if s == "" {
+		c.versionTemplate = nil
+		return
+	}
 	c.versionTemplate = tmpl(s)
 }
 


Test patch:
diff --git a/command_test.go b/command_test.go
index 0b0d6c6..a379460 100644
--- a/command_test.go
+++ b/command_test.go
@@ -1047,6 +1047,18 @@ func TestSetHelpTemplate(t *testing.T) {
 	if got != expected {
 		t.Errorf("Expected %q, got %q", expected, got)
 	}
+
+	// Reset the root command help template and make sure
+	// it falls back to the default
+	rootCmd.SetHelpTemplate("")
+	got, err = executeCommand(rootCmd, "--help")
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
+	}
+
+	if !strings.Contains(got, "Usage:") {
+		t.Errorf("Expected to contain %q, got %q", "Usage:", got)
+	}
 }
 
 func TestHelpFlagExecuted(t *testing.T) {
@@ -1139,6 +1151,18 @@ func TestSetUsageTemplate(t *testing.T) {
 
 	expected = "WORKS " + childCmd.UseLine()
 	checkStringContains(t, got, expected)
+
+	// Reset the root command usage template and make sure
+	// it falls back to the default
+	rootCmd.SetUsageTemplate("")
+	got, err = executeCommand(rootCmd, "--invalid")
+	if err == nil {
+		t.Errorf("Expected error but did not get one")
+	}
+
+	if !strings.Contains(got, "Usage:") {
+		t.Errorf("Expected to contain %q, got %q", "Usage:", got)
+	}
 }
 
 func TestVersionFlagExecuted(t *testing.T) {
Repository: spf13/cobra
Pull request #2241 (2025-02-20 15:05:06)

Flow context to command in SetHelpFunc
Fixes https://github.com/spf13/cobra/issues/2240


Patch:
diff --git a/command.go b/command.go
index dbb2c29..140fbfd 100644
--- a/command.go
+++ b/command.go
@@ -1296,6 +1296,11 @@ Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
 					c.Printf("Unknown help topic %#q\n", args)
 					CheckErr(c.Root().Usage())
 				} else {
+					// FLow the context down to be used in help text
+					if cmd.ctx == nil {
+						cmd.ctx = c.ctx
+					}
+
 					cmd.InitDefaultHelpFlag()    // make possible 'help' flag to be shown
 					cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown
 					CheckErr(cmd.Help())


Test patch:
diff --git a/command_test.go b/command_test.go
index 7e7c341..156df9e 100644
--- a/command_test.go
+++ b/command_test.go
@@ -2921,3 +2921,34 @@ func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {
 		})
 	}
 }
+
+func TestHelpFuncExecuted(t *testing.T) {
+	helpText := "Long description"
+
+	// Create a context that will be unique, not just the background context
+	//nolint:golint,staticcheck // We can safely use a basic type as key in tests.
+	executionCtx := context.WithValue(context.Background(), "testKey", "123")
+
+	child := &Command{Use: "child", Run: emptyRun}
+	child.SetHelpFunc(func(cmd *Command, args []string) {
+		_, err := cmd.OutOrStdout().Write([]byte(helpText))
+		if err != nil {
+			t.Error(err)
+		}
+
+		// Test for https://github.com/spf13/cobra/issues/2240
+		if cmd.Context() != executionCtx {
+			t.Error("Context doesn't equal the execution context")
+		}
+	})
+
+	rootCmd := &Command{Use: "root", Run: emptyRun}
+	rootCmd.AddCommand(child)
+
+	output, err := executeCommandWithContext(executionCtx, rootCmd, "help", "child")
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
+	}
+
+	checkStringContains(t, output, helpText)
+}