Skip to content
Myr

Merge pull request #90 from eparis/deprecated-subcommands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Deprecated subcommands

authored by Eric Paris · committed by Eric Paris · parents 7c7837e, c3e48f9 · Browse files · 6 files+48 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+48 added−5 removed

  • other:+48 added−5 removed

6 files+48 added−5 removed

modified:bash_completions_test.go+9 added-1 removed
9 unmodified lines
1011var _ = fmt.Println12var _ = os.Stderr1314func checkOmit(t *testing.T, found, unexpected string) {15 if strings.Contains(found, unexpected) {16 t.Errorf("Unexpected response.\nGot: %q\nBut should not have!\n", unexpected)17 }18}1920func check(t *testing.T, found, expected string) {21 if !strings.Contains(found, expected) {22 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)23 }
9 unmodified lines
3334func TestBashCompletions(t *testing.T) {35 c := initializeWithRootCmd()36 cmdEcho.AddCommand(cmdTimes)31 c.AddCommand(cmdEcho, cmdPrint)37 c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated)3839 // custom completion function40 c.BashCompletionFunction = bash_completion_func41
34 unmodified lines
76 // check for required nouns77 check(t, str, `must_have_one_noun+=("pods")`)78 // check for filename extention flags79 check(t, str, `flags_completion+=("_filedir '@(json|yaml|yml)'")`)8081 checkOmit(t, str, cmdDeprecated.Name())82}
modified:bash_completions.go+6 added
197 unmodified lines
198199func writeCommands(cmd *Command, out *bytes.Buffer) {200 fmt.Fprintf(out, " commands=()\n")201 for _, c := range cmd.Commands() {202 if len(c.Deprecated) > 0 {203 continue204 }205 fmt.Fprintf(out, " commands+=(%q)\n", c.Name())206 }207 fmt.Fprintf(out, "\n")208}
84 unmodified lines
293}294295func gen(cmd *Command, out *bytes.Buffer) {296 for _, c := range cmd.Commands() {297 if len(c.Deprecated) > 0 {298 continue299 }300 gen(c, out)301 }302 commandName := cmd.CommandPath()303 commandName = strings.Replace(commandName, " ", "_", -1)
48 unmodified lines
modified:cobra_test.go+16 added-1 removed
49 unmodified lines
50 Run: func(cmd *Command, args []string) {51 },52}5354var cmdDeprecated = &Command{55 Use: "deprecated [can't do anything here]",56 Short: "A command which is deprecated",57 Long: `an absolutely utterly useless command for testing deprecation!.`,58 Deprecated: "Please use echo instead",59 Run: func(cmd *Command, args []string) {60 },61}6263var cmdTimes = &Command{64 Use: "times [# times] [string to echo]",65 Short: "Echo anything to the screen more times",66 Long: `a slightly useless command for testing.`,
146 unmodified lines
213 buf := new(bytes.Buffer)214 // Testing flag with invalid input215 c.SetOutput(buf)216 cmdEcho.AddCommand(cmdTimes)208 c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun)217 c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdDeprecated)218 c.SetArgs(strings.Split(input, " "))219220 err := c.Execute()221 output := buf.String()
598 unmodified lines
820 if versionUsed != 2 {821 t.Errorf("Replacing command should have been called but didn't\n")822 }823}824825func TestDeprecatedSub(t *testing.T) {826 c := fullSetupTest("deprecated")827828 checkResultContains(t, c, cmdDeprecated.Deprecated)829}
modified:command.go+8 added-2 removed
46 unmodified lines
47 // List of all valid non-flag arguments, used for bash completions *TODO* actually validate these48 ValidArgs []string49 // Custom functions used by the bash autocompletion generator50 BashCompletionFunction string51 // Is this command deprecated and should print this string when used?52 Deprecated string53 // Full set of flags54 flags *flag.FlagSet55 // Set of flags childrens of this command will inherit56 pflags *flag.FlagSet
175 unmodified lines
232Examples:233{{ .Example }}234{{end}}{{ if .HasRunnableSubCommands}}235234Available Commands: {{range .Commands}}{{if .Runnable}}236Available Commands: {{range .Commands}}{{if and (.Runnable) (not .Deprecated)}}237 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}238{{end}}239{{ if .HasLocalFlags}}Flags:240{{.LocalFlags.FlagUsages}}{{end}}241{{ if .HasInheritedFlags}}Global Flags:242{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}243Additional help topics:242{{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}244{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}245 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}246{{end}}{{ if .HasSubCommands }}247Use "{{.Root.Name}} help [command]" for more information about a command.248{{end}}`
166 unmodified lines
415 if c == nil {416 return fmt.Errorf("Called Execute() on a nil Command")417 }418419 if len(c.Deprecated) > 0 {420 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)421 }422423 err = c.ParseFlags(a)424 if err == flag.ErrHelp {425 c.Help()426 return nil
539 unmodified lines
modified:md_docs_test.go+6 added-1 removed
12 unmodified lines
1314func TestGenMdDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub)17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18 c.AddCommand(cmdPrint, cmdEcho)19 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)2021 out := new(bytes.Buffer)
36 unmodified lines
58 expected = cmdEchoSub.Short59 if !strings.Contains(found, expected) {60 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)61 }6263 unexpected := cmdDeprecated.Short64 if strings.Contains(found, unexpected) {65 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)66 }67}
modified:md_docs.go+3 added
83 unmodified lines
84 children := cmd.Commands()85 sort.Sort(byName(children))8687 for _, child := range children {88 if len(child.Deprecated) > 0 {89 continue90 }91 cname := name + " " + child.Name()92 link := cname + ".md"93 link = strings.Replace(link, " ", "_", -1)94 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, link, child.Short)
30 unmodified lines