nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: spf13/cobra
Pull request #127 (2015-08-17 05:42:05)

Display pflag.CommandLine flags as if they were declared on the parent
```go
package main

import (
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func main() {
	cmd := &cobra.Command{
		Use:   "min",
		Short: "minimal command",
		Run:   func(_ *cobra.Command, _ []string) {},
	}

	pflag.String("oncmdline", "oncmdline", "oncmdline")
	cmd.Execute()
}
```

Is a minimal cobra program.  When --help is displayed without this patch you get no indication the "oncmdline" is a valid flag. But it does work. With the patch --oncmdline is shows under flags.


Patch:
diff --git a/command.go b/command.go
index 74565c2..cbbc326 100644
--- a/command.go
+++ b/command.go
@@ -158,7 +158,6 @@ func (c *Command) SetHelpTemplate(s string) {
 func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
 	c.Flags().SetNormalizeFunc(n)
 	c.PersistentFlags().SetNormalizeFunc(n)
-	c.LocalFlags().SetNormalizeFunc(n)
 	c.globNormFunc = n
 
 	for _, command := range c.commands {
@@ -873,6 +872,13 @@ func (c *Command) LocalFlags() *flag.FlagSet {
 	c.lflags.VisitAll(func(f *flag.Flag) {
 		local.AddFlag(f)
 	})
+	if !c.HasParent() {
+		flag.CommandLine.VisitAll(func(f *flag.Flag) {
+			if local.Lookup(f.Name) == nil {
+				local.AddFlag(f)
+			}
+		})
+	}
 	return local
 }
 


Test patch:
diff --git a/cobra_test.go b/cobra_test.go
index 7cb4917..4fc3b88 100644
--- a/cobra_test.go
+++ b/cobra_test.go
@@ -963,3 +963,11 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
 		t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
 	}
 }
+
+func TestFlagOnPflagCommandLine(t *testing.T) {
+	flagName := "flagOnCommandLine"
+	pflag.CommandLine.String(flagName, "", "about my flag")
+	r := fullSetupTest("--help")
+
+	checkResultContains(t, r, flagName)
+}
Repository: spf13/cobra
Pull request #160 (2015-09-17 21:04:37)

Provide the length of Args when a -- is found in commandline
This allows a program to know what args came before the -- and what args
came after.


Patch:
diff --git a/command.go b/command.go
index 6f73f71..0cc0b72 100644
--- a/command.go
+++ b/command.go
@@ -470,6 +470,10 @@ func (c *Command) Root() *Command {
 	return findRoot(c)
 }
 
+func (c *Command) ArgsLenAtDash() int {
+	return c.Flags().ArgsLenAtDash()
+}
+
 func (c *Command) execute(a []string) (err error) {
 	if c == nil {
 		return fmt.Errorf("Called Execute() on a nil Command")


Test patch:
diff --git a/cobra_test.go b/cobra_test.go
index 8b43380..c9eef5d 100644
--- a/cobra_test.go
+++ b/cobra_test.go
@@ -416,8 +416,11 @@ func TestGrandChildSameName(t *testing.T) {
 }
 
 func TestFlagLong(t *testing.T) {
-	noRRSetupTest("echo --intone=13 something here")
+	noRRSetupTest("echo --intone=13 something -- here")
 
+	if cmdEcho.ArgsLenAtDash() != 1 {
+		t.Errorf("expected argsLenAtDash: %d but got %d", 1, cmdRootNoRun.ArgsLenAtDash())
+	}
 	if strings.Join(te, " ") != "something here" {
 		t.Errorf("flags didn't leave proper args remaining..%s given", te)
 	}
@@ -430,8 +433,11 @@ func TestFlagLong(t *testing.T) {
 }
 
 func TestFlagShort(t *testing.T) {
-	noRRSetupTest("echo -i13 something here")
+	noRRSetupTest("echo -i13 -- something here")
 
+	if cmdEcho.ArgsLenAtDash() != 0 {
+		t.Errorf("expected argsLenAtDash: %d but got %d", 0, cmdRootNoRun.ArgsLenAtDash())
+	}
 	if strings.Join(te, " ") != "something here" {
 		t.Errorf("flags didn't leave proper args remaining..%s given", te)
 	}
Repository: spf13/cobra
Pull request #411 (2017-04-01 23:18:23)

Avoid storing pointer to nil
Please see commit messages. @spf13 

cc @spencerkimball

tamird: @eparis

Patch:
diff --git a/bash_completions.go b/bash_completions.go
index 8820ba8..59e90f9 100644
--- a/bash_completions.go
+++ b/bash_completions.go
@@ -225,7 +225,7 @@ __handle_command()
     fi
     c=$((c+1))
     __debug "${FUNCNAME[0]}: looking for ${next_command}"
-    declare -F $next_command >/dev/null && $next_command
+    declare -F "$next_command" >/dev/null && $next_command
 }
 
 __handle_word()
diff --git a/command.go b/command.go
index 664bf5a..a5b47d0 100644
--- a/command.go
+++ b/command.go
@@ -113,7 +113,7 @@ type Command struct {
 	flagErrorBuf *bytes.Buffer
 
 	args          []string             // actual args parsed from flags
-	output        *io.Writer           // out writer if set in SetOutput(w)
+	output        io.Writer            // out writer if set in SetOutput(w)
 	usageFunc     func(*Command) error // Usage can be defined by application
 	usageTemplate string               // Can be defined by Application
 	flagErrorFunc func(*Command, error) error
@@ -141,7 +141,7 @@ func (c *Command) SetArgs(a []string) {
 // SetOutput sets the destination for usage and error messages.
 // If output is nil, os.Stderr is used.
 func (c *Command) SetOutput(output io.Writer) {
-	c.output = &output
+	c.output = output
 }
 
 // SetUsageFunc sets usage function. Usage can be defined by application.
@@ -199,7 +199,7 @@ func (c *Command) OutOrStderr() io.Writer {
 
 func (c *Command) getOut(def io.Writer) io.Writer {
 	if c.output != nil {
-		return *c.output
+		return c.output
 	}
 	if c.HasParent() {
 		return c.parent.getOut(def)


Test patch:
diff --git a/command_test.go b/command_test.go
index 7947f6c..8b78ee3 100644
--- a/command_test.go
+++ b/command_test.go
@@ -191,6 +191,14 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
 	EnableCommandSorting = true
 }
 
+func TestSetOutput(t *testing.T) {
+	cmd := &Command{}
+	cmd.SetOutput(nil)
+	if out := cmd.OutOrStdout(); out != os.Stdout {
+		t.Fatalf("expected setting output to nil to revert back to stdout, got %v", out)
+	}
+}
+
 func TestFlagErrorFunc(t *testing.T) {
 
 	cmd := &Command{
Repository: spf13/cobra
Pull request #549 (2017-10-12 17:42:45)

Fix TraverseChildren used with multiple subcommands
This PR fixes a nil dereference when `TraverseChildren` is used with multiple subcommands.

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/spf13/cobra?pullRequest=549) <br/>Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/spf13/cobra?pullRequest=549) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/spf13/cobra?pullRequest=549) it.</sub>

Patch:
diff --git a/command.go b/command.go
index 4d4b391..58e6ceb 100644
--- a/command.go
+++ b/command.go
@@ -1360,6 +1360,9 @@ func (c *Command) ParseFlags(args []string) error {
 		return nil
 	}
 
+	if c.flagErrorBuf == nil {
+		c.flagErrorBuf = new(bytes.Buffer)
+	}
 	beforeErrorBufLen := c.flagErrorBuf.Len()
 	c.mergePersistentFlags()
 	err := c.Flags().Parse(args)


Test patch:
diff --git a/command_test.go b/command_test.go
index 4f798da..dda355f 100644
--- a/command_test.go
+++ b/command_test.go
@@ -439,6 +439,32 @@ func TestTraverseWithBadChildFlag(t *testing.T) {
 	}
 }
 
+func TestTraverseWithTwoSubcommands(t *testing.T) {
+	cmd := &Command{
+		Use:              "do",
+		TraverseChildren: true,
+	}
+
+	sub := &Command{
+		Use:              "sub",
+		TraverseChildren: true,
+	}
+	cmd.AddCommand(sub)
+
+	subsub := &Command{
+		Use: "subsub",
+	}
+	sub.AddCommand(subsub)
+
+	c, _, err := cmd.Traverse([]string{"sub", "subsub"})
+	if err != nil {
+		t.Fatalf("Expected no error: %s", err)
+	}
+	if c.Name() != subsub.Name() {
+		t.Fatalf("wrong command %q expected %q", c.Name(), subsub.Name())
+	}
+}
+
 func TestRequiredFlags(t *testing.T) {
 	c := &Command{Use: "c", Run: func(*Command, []string) {}}
 	output := new(bytes.Buffer)
Repository: spf13/cobra
Pull request #1037 (2020-02-18 00:14:32)

YAML documentation contains "Usage"
When generating documentation in YAML format, include the "Usage" field.

That is present in the Markdown docs, but not in the YAML ones.

claassistantio: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/spf13/cobra?pullRequest=1037) <br/>Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/spf13/cobra?pullRequest=1037) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/spf13/cobra?pullRequest=1037) it.</sub>
CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/signed)](https://cla-assistant.io/spf13/cobra?pullRequest=1037) <br/>All committers have signed the CLA.
wfernandes: Thanks for this PR @ItalyPaleAle 
Could we have a test written for this? I think it can be included in `yaml_docs_test.go`.

github-actions[bot]: This PR is being marked as stale due to a long period of inactivity
ItalyPaleAle: Done. Sorry again for the delay
wfernandes: Thanks for adding the test. Feel free to squash the commits. I think we can merge it after 🙂 
ItalyPaleAle: Done, I squashed the two commits

Patch:
diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go
index ea00af0..96e6ad7 100644
--- a/doc/yaml_docs.go
+++ b/doc/yaml_docs.go
@@ -37,6 +37,7 @@ type cmdDoc struct {
 	Name             string
 	Synopsis         string      `yaml:",omitempty"`
 	Description      string      `yaml:",omitempty"`
+	Usage            string      `yaml:",omitempty"`
 	Options          []cmdOption `yaml:",omitempty"`
 	InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`
 	Example          string      `yaml:",omitempty"`
@@ -98,6 +99,10 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) str
 	yamlDoc.Synopsis = forceMultiLine(cmd.Short)
 	yamlDoc.Description = forceMultiLine(cmd.Long)
 
+	if cmd.Runnable() {
+		yamlDoc.Usage = cmd.UseLine()
+	}
+
 	if len(cmd.Example) > 0 {
 		yamlDoc.Example = cmd.Example
 	}


Test patch:
diff --git a/doc/yaml_docs_test.go b/doc/yaml_docs_test.go
index c5a6359..d08fa4f 100644
--- a/doc/yaml_docs_test.go
+++ b/doc/yaml_docs_test.go
@@ -57,6 +57,17 @@ func TestGenYamlTree(t *testing.T) {
 	}
 }
 
+func TestGenYamlDocRunnable(t *testing.T) {
+	// Testing a runnable command: should contain the "usage" field
+	buf := new(bytes.Buffer)
+	if err := GenYaml(rootCmd, buf); err != nil {
+		t.Fatal(err)
+	}
+	output := buf.String()
+
+	checkStringContains(t, output, "usage: "+rootCmd.Use)
+}
+
 func BenchmarkGenYamlToFile(b *testing.B) {
 	file, err := ioutil.TempFile("", "")
 	if err != nil {
Repository: spf13/cobra
Pull request #1068 (2020-03-26 22:04:05)

Partial Revert of #922
Issue Reference: https://github.com/spf13/cobra/issues/1056
Bug Introduced: #922 

#922 Introduced a new error type that emitted when a command was not runnable. This caused all commands w/o a run function set to error w/ that message and a status code of 1.

This change reverts the addition of that new error. Similar
functionality can be accomplished by leveraging RunE.

jharshman: fixes: #1056 
jharshman: @bogem tagging you as a reviewer here as you were on #922 
bogem: I didn't contribute in cobra since 2018, so I already lost the context :(

Patch:
diff --git a/command.go b/command.go
index 55ddc50..fdac9d2 100644
--- a/command.go
+++ b/command.go
@@ -18,7 +18,6 @@ package cobra
 import (
 	"bytes"
 	"context"
-	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -29,8 +28,6 @@ import (
 	flag "github.com/spf13/pflag"
 )
 
-var ErrSubCommandRequired = errors.New("subcommand is required")
-
 // FParseErrWhitelist configures Flag parse errors to be ignored
 type FParseErrWhitelist flag.ParseErrorsWhitelist
 
@@ -801,7 +798,7 @@ func (c *Command) execute(a []string) (err error) {
 	}
 
 	if !c.Runnable() {
-		return ErrSubCommandRequired
+		return flag.ErrHelp
 	}
 
 	c.preRun()
@@ -952,14 +949,6 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
 			return cmd, nil
 		}
 
-		// If command wasn't runnable, show full help, but do return the error.
-		// This will result in apps by default returning a non-success exit code, but also gives them the option to
-		// handle specially.
-		if err == ErrSubCommandRequired {
-			cmd.HelpFunc()(cmd, args)
-			return cmd, err
-		}
-
 		// If root command has SilentErrors flagged,
 		// all subcommands should respect it
 		if !cmd.SilenceErrors && !c.SilenceErrors {


Test patch:
diff --git a/command_test.go b/command_test.go
index 1215593..abee7ba 100644
--- a/command_test.go
+++ b/command_test.go
@@ -903,8 +903,8 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
 	rootCmd.AddCommand(childCmd)
 
 	output, err := executeCommand(rootCmd, "child")
-	if err != ErrSubCommandRequired {
-		t.Errorf("Expected error")
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
 	}
 
 	checkStringContains(t, output, childCmd.Long)