forked from mirror/cobra
Add support for ignoring parse errors (#662)
This commit is contained in:
parent
a1f051bc3e
commit
4dab30cb33
10
command.go
10
command.go
|
@ -27,6 +27,9 @@ import (
|
|||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// FParseErrWhitelist configures Flag parse errors to be ignored
|
||||
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
||||
|
||||
// Command is just that, a command for your application.
|
||||
// E.g. 'go run ...' - 'run' is the command. Cobra requires
|
||||
// you to define the usage and description as part of your command
|
||||
|
@ -137,6 +140,9 @@ type Command struct {
|
|||
// TraverseChildren parses flags on all parents before executing child command.
|
||||
TraverseChildren bool
|
||||
|
||||
//FParseErrWhitelist flag parse errors to be ignored
|
||||
FParseErrWhitelist FParseErrWhitelist
|
||||
|
||||
// commands is the list of commands supported by this program.
|
||||
commands []*Command
|
||||
// parent is a parent command for this command.
|
||||
|
@ -1463,6 +1469,10 @@ func (c *Command) ParseFlags(args []string) error {
|
|||
}
|
||||
beforeErrorBufLen := c.flagErrorBuf.Len()
|
||||
c.mergePersistentFlags()
|
||||
|
||||
//do it here after merging all flags and just before parse
|
||||
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
|
||||
|
||||
err := c.Flags().Parse(args)
|
||||
// Print warnings if they occurred (e.g. deprecated flag messages).
|
||||
if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
|
||||
|
|
105
command_test.go
105
command_test.go
|
@ -1626,3 +1626,108 @@ func TestCalledAs(t *testing.T) {
|
|||
t.Run(name, tc.test)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFParseErrWhitelistBackwardCompatibility(t *testing.T) {
|
||||
c := &Command{Use: "c", Run: emptyRun}
|
||||
c.Flags().BoolP("boola", "a", false, "a boolean flag")
|
||||
|
||||
output, err := executeCommand(c, "c", "-a", "--unknown", "flag")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
}
|
||||
checkStringContains(t, output, "unknown flag: --unknown")
|
||||
}
|
||||
|
||||
func TestFParseErrWhitelistSameCommand(t *testing.T) {
|
||||
c := &Command{
|
||||
Use: "c",
|
||||
Run: emptyRun,
|
||||
FParseErrWhitelist: FParseErrWhitelist{
|
||||
UnknownFlags: true,
|
||||
},
|
||||
}
|
||||
c.Flags().BoolP("boola", "a", false, "a boolean flag")
|
||||
|
||||
_, err := executeCommand(c, "c", "-a", "--unknown", "flag")
|
||||
if err != nil {
|
||||
t.Error("unexpected error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFParseErrWhitelistParentCommand(t *testing.T) {
|
||||
root := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
FParseErrWhitelist: FParseErrWhitelist{
|
||||
UnknownFlags: true,
|
||||
},
|
||||
}
|
||||
|
||||
c := &Command{
|
||||
Use: "child",
|
||||
Run: emptyRun,
|
||||
}
|
||||
c.Flags().BoolP("boola", "a", false, "a boolean flag")
|
||||
|
||||
root.AddCommand(c)
|
||||
|
||||
output, err := executeCommand(root, "child", "-a", "--unknown", "flag")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
}
|
||||
checkStringContains(t, output, "unknown flag: --unknown")
|
||||
}
|
||||
|
||||
func TestFParseErrWhitelistChildCommand(t *testing.T) {
|
||||
root := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
}
|
||||
|
||||
c := &Command{
|
||||
Use: "child",
|
||||
Run: emptyRun,
|
||||
FParseErrWhitelist: FParseErrWhitelist{
|
||||
UnknownFlags: true,
|
||||
},
|
||||
}
|
||||
c.Flags().BoolP("boola", "a", false, "a boolean flag")
|
||||
|
||||
root.AddCommand(c)
|
||||
|
||||
_, err := executeCommand(root, "child", "-a", "--unknown", "flag")
|
||||
if err != nil {
|
||||
t.Error("unexpected error: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
|
||||
root := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
}
|
||||
|
||||
c := &Command{
|
||||
Use: "child",
|
||||
Run: emptyRun,
|
||||
FParseErrWhitelist: FParseErrWhitelist{
|
||||
UnknownFlags: true,
|
||||
},
|
||||
}
|
||||
c.Flags().BoolP("boola", "a", false, "a boolean flag")
|
||||
|
||||
s := &Command{
|
||||
Use: "sibling",
|
||||
Run: emptyRun,
|
||||
}
|
||||
s.Flags().BoolP("boolb", "b", false, "a boolean flag")
|
||||
|
||||
root.AddCommand(c)
|
||||
root.AddCommand(s)
|
||||
|
||||
output, err := executeCommand(root, "sibling", "-b", "--unknown", "flag")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
}
|
||||
checkStringContains(t, output, "unknown flag: --unknown")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue