nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: rsteube/carapace
Pull request #779 (2023-05-18 21:30:26)

help completion


Patch:
diff --git a/complete.go b/complete.go
index d65829a..26060cb 100644
--- a/complete.go
+++ b/complete.go
@@ -13,6 +13,7 @@ func complete(cmd *cobra.Command, args []string) (string, error) {
 	case 1:
 		return Gen(cmd).Snippet(args[0])
 	default:
+		initHelpCompletion(cmd)
 		action, context := traverse(cmd, args[2:])
 		if err := config.Load(); err != nil {
 			action = ActionMessage("failed to load config: " + err.Error())
diff --git a/internalActions.go b/internalActions.go
index 322d10f..0b90ba0 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -132,3 +132,26 @@ func actionSubcommands(cmd *cobra.Command) Action {
 		return batch.ToA()
 	})
 }
+
+func initHelpCompletion(cmd *cobra.Command) {
+	helpCmd, _, err := cmd.Find([]string{"help"})
+	if err != nil {
+		return
+	}
+
+	if helpCmd.Name() != "help" ||
+		helpCmd.Short != "Help about any command" ||
+		!strings.HasPrefix(helpCmd.Long, `Help provides help for any command in the application.`) {
+		return
+	}
+
+	Gen(helpCmd).PositionalAnyCompletion(
+		ActionCallback(func(c Context) Action {
+			lastCmd, _, err := cmd.Find(c.Args)
+			if err != nil {
+				return ActionMessage(err.Error())
+			}
+			return actionSubcommands(lastCmd)
+		}),
+	)
+}
diff --git a/pkg/sandbox/sandbox.go b/pkg/sandbox/sandbox.go
index 2df1ea0..bb6b1df 100644
--- a/pkg/sandbox/sandbox.go
+++ b/pkg/sandbox/sandbox.go
@@ -183,6 +183,7 @@ func Package(t *testing.T, pkg string) (f func(func(s *Sandbox))) {
 	return Command(t, func() *cobra.Command {
 		cmd := &cobra.Command{DisableFlagParsing: true}
 		cmd.CompletionOptions.DisableDefaultCmd = true
+		cmd.SetHelpCommand(&cobra.Command{})
 
 		carapace.Gen(cmd).PositionalAnyCompletion(
 			carapace.ActionCallback(func(c carapace.Context) carapace.Action {


Test patch:
diff --git a/example/cmd/help_test.go b/example/cmd/help_test.go
new file mode 100644
index 0000000..c8d71af
--- /dev/null
+++ b/example/cmd/help_test.go
@@ -0,0 +1,20 @@
+package cmd
+
+import (
+	"testing"
+
+	"github.com/rsteube/carapace"
+	"github.com/rsteube/carapace/pkg/sandbox"
+	"github.com/rsteube/carapace/pkg/style"
+)
+
+func TestHelp(t *testing.T) {
+	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
+		s.Run("help", "a").
+			Expect(carapace.ActionValuesDescribed(
+				"action", "action example",
+				"alias", "action example",
+			).Style(style.Blue).Tag("main commands").
+				Usage("help [command]"))
+	})
+}