Deprecated subcommands
They should still work, but shouldn't show up in help or usage output
This commit is contained in:
parent
69e5f196b5
commit
c3e48f996d
@ -199,6 +199,9 @@ func postscript(out *bytes.Buffer, name string) {
|
|||||||
func writeCommands(cmd *Command, out *bytes.Buffer) {
|
func writeCommands(cmd *Command, out *bytes.Buffer) {
|
||||||
fmt.Fprintf(out, " commands=()\n")
|
fmt.Fprintf(out, " commands=()\n")
|
||||||
for _, c := range cmd.Commands() {
|
for _, c := range cmd.Commands() {
|
||||||
|
if len(c.Deprecated) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
fmt.Fprintf(out, " commands+=(%q)\n", c.Name())
|
fmt.Fprintf(out, " commands+=(%q)\n", c.Name())
|
||||||
}
|
}
|
||||||
fmt.Fprintf(out, "\n")
|
fmt.Fprintf(out, "\n")
|
||||||
@ -291,6 +294,9 @@ func writeRequiredNoun(cmd *Command, out *bytes.Buffer) {
|
|||||||
|
|
||||||
func gen(cmd *Command, out *bytes.Buffer) {
|
func gen(cmd *Command, out *bytes.Buffer) {
|
||||||
for _, c := range cmd.Commands() {
|
for _, c := range cmd.Commands() {
|
||||||
|
if len(c.Deprecated) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
gen(c, out)
|
gen(c, out)
|
||||||
}
|
}
|
||||||
commandName := cmd.CommandPath()
|
commandName := cmd.CommandPath()
|
||||||
|
@ -11,6 +11,12 @@ import (
|
|||||||
var _ = fmt.Println
|
var _ = fmt.Println
|
||||||
var _ = os.Stderr
|
var _ = os.Stderr
|
||||||
|
|
||||||
|
func checkOmit(t *testing.T, found, unexpected string) {
|
||||||
|
if strings.Contains(found, unexpected) {
|
||||||
|
t.Errorf("Unexpected response.\nGot: %q\nBut should not have!\n", unexpected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func check(t *testing.T, found, expected string) {
|
func check(t *testing.T, found, expected string) {
|
||||||
if !strings.Contains(found, expected) {
|
if !strings.Contains(found, expected) {
|
||||||
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
||||||
@ -28,7 +34,7 @@ COMPREPLY=( "hello" )
|
|||||||
func TestBashCompletions(t *testing.T) {
|
func TestBashCompletions(t *testing.T) {
|
||||||
c := initializeWithRootCmd()
|
c := initializeWithRootCmd()
|
||||||
cmdEcho.AddCommand(cmdTimes)
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
c.AddCommand(cmdEcho, cmdPrint)
|
c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated)
|
||||||
|
|
||||||
// custom completion function
|
// custom completion function
|
||||||
c.BashCompletionFunction = bash_completion_func
|
c.BashCompletionFunction = bash_completion_func
|
||||||
@ -71,4 +77,6 @@ func TestBashCompletions(t *testing.T) {
|
|||||||
check(t, str, `must_have_one_noun+=("pods")`)
|
check(t, str, `must_have_one_noun+=("pods")`)
|
||||||
// check for filename extention flags
|
// check for filename extention flags
|
||||||
check(t, str, `flags_completion+=("_filedir '@(json|yaml|yml)'")`)
|
check(t, str, `flags_completion+=("_filedir '@(json|yaml|yml)'")`)
|
||||||
|
|
||||||
|
checkOmit(t, str, cmdDeprecated.Name())
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,15 @@ var cmdEchoSub = &Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cmdDeprecated = &Command{
|
||||||
|
Use: "deprecated [can't do anything here]",
|
||||||
|
Short: "A command which is deprecated",
|
||||||
|
Long: `an absolutely utterly useless command for testing deprecation!.`,
|
||||||
|
Deprecated: "Please use echo instead",
|
||||||
|
Run: func(cmd *Command, args []string) {
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var cmdTimes = &Command{
|
var cmdTimes = &Command{
|
||||||
Use: "times [# times] [string to echo]",
|
Use: "times [# times] [string to echo]",
|
||||||
Short: "Echo anything to the screen more times",
|
Short: "Echo anything to the screen more times",
|
||||||
@ -205,7 +214,7 @@ func fullTester(c *Command, input string) resulter {
|
|||||||
// Testing flag with invalid input
|
// Testing flag with invalid input
|
||||||
c.SetOutput(buf)
|
c.SetOutput(buf)
|
||||||
cmdEcho.AddCommand(cmdTimes)
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun)
|
c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdDeprecated)
|
||||||
c.SetArgs(strings.Split(input, " "))
|
c.SetArgs(strings.Split(input, " "))
|
||||||
|
|
||||||
err := c.Execute()
|
err := c.Execute()
|
||||||
@ -812,3 +821,9 @@ func TestReplaceCommandWithRemove(t *testing.T) {
|
|||||||
t.Errorf("Replacing command should have been called but didn't\n")
|
t.Errorf("Replacing command should have been called but didn't\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeprecatedSub(t *testing.T) {
|
||||||
|
c := fullSetupTest("deprecated")
|
||||||
|
|
||||||
|
checkResultContains(t, c, cmdDeprecated.Deprecated)
|
||||||
|
}
|
||||||
|
10
command.go
10
command.go
@ -48,6 +48,8 @@ type Command struct {
|
|||||||
ValidArgs []string
|
ValidArgs []string
|
||||||
// Custom functions used by the bash autocompletion generator
|
// Custom functions used by the bash autocompletion generator
|
||||||
BashCompletionFunction string
|
BashCompletionFunction string
|
||||||
|
// Is this command deprecated and should print this string when used?
|
||||||
|
Deprecated string
|
||||||
// Full set of flags
|
// Full set of flags
|
||||||
flags *flag.FlagSet
|
flags *flag.FlagSet
|
||||||
// Set of flags childrens of this command will inherit
|
// Set of flags childrens of this command will inherit
|
||||||
@ -231,7 +233,7 @@ Examples:
|
|||||||
{{ .Example }}
|
{{ .Example }}
|
||||||
{{end}}{{ if .HasRunnableSubCommands}}
|
{{end}}{{ if .HasRunnableSubCommands}}
|
||||||
|
|
||||||
Available Commands: {{range .Commands}}{{if .Runnable}}
|
Available Commands: {{range .Commands}}{{if and (.Runnable) (not .Deprecated)}}
|
||||||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{ if .HasLocalFlags}}Flags:
|
{{ if .HasLocalFlags}}Flags:
|
||||||
@ -239,7 +241,7 @@ Available Commands: {{range .Commands}}{{if .Runnable}}
|
|||||||
{{ if .HasInheritedFlags}}Global Flags:
|
{{ if .HasInheritedFlags}}Global Flags:
|
||||||
{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
|
{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
|
||||||
Additional help topics:
|
Additional help topics:
|
||||||
{{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) }}
|
{{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) }}
|
||||||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
||||||
{{end}}{{ if .HasSubCommands }}
|
{{end}}{{ if .HasSubCommands }}
|
||||||
Use "{{.Root.Name}} help [command]" for more information about a command.
|
Use "{{.Root.Name}} help [command]" for more information about a command.
|
||||||
@ -414,6 +416,10 @@ func (c *Command) execute(a []string) (err error) {
|
|||||||
return fmt.Errorf("Called Execute() on a nil Command")
|
return fmt.Errorf("Called Execute() on a nil Command")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(c.Deprecated) > 0 {
|
||||||
|
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
|
||||||
|
}
|
||||||
|
|
||||||
err = c.ParseFlags(a)
|
err = c.ParseFlags(a)
|
||||||
if err == flag.ErrHelp {
|
if err == flag.ErrHelp {
|
||||||
c.Help()
|
c.Help()
|
||||||
|
@ -85,6 +85,9 @@ func GenMarkdown(cmd *Command, out *bytes.Buffer) {
|
|||||||
sort.Sort(byName(children))
|
sort.Sort(byName(children))
|
||||||
|
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
|
if len(child.Deprecated) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
cname := name + " " + child.Name()
|
cname := name + " " + child.Name()
|
||||||
link := cname + ".md"
|
link := cname + ".md"
|
||||||
link = strings.Replace(link, " ", "_", -1)
|
link = strings.Replace(link, " ", "_", -1)
|
||||||
|
@ -14,7 +14,7 @@ var _ = os.Stderr
|
|||||||
func TestGenMdDoc(t *testing.T) {
|
func TestGenMdDoc(t *testing.T) {
|
||||||
c := initializeWithRootCmd()
|
c := initializeWithRootCmd()
|
||||||
// Need two commands to run the command alphabetical sort
|
// Need two commands to run the command alphabetical sort
|
||||||
cmdEcho.AddCommand(cmdTimes, cmdEchoSub)
|
cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
|
||||||
c.AddCommand(cmdPrint, cmdEcho)
|
c.AddCommand(cmdPrint, cmdEcho)
|
||||||
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
||||||
|
|
||||||
@ -59,4 +59,9 @@ func TestGenMdDoc(t *testing.T) {
|
|||||||
if !strings.Contains(found, expected) {
|
if !strings.Contains(found, expected) {
|
||||||
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unexpected := cmdDeprecated.Short
|
||||||
|
if strings.Contains(found, unexpected) {
|
||||||
|
t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user