Skip to content

Commit 4dab30c

Browse files
rajatjindaleparis
authored andcommitted
Add support for ignoring parse errors (#662)
1 parent a1f051b commit 4dab30c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

Diff for: command.go

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727
flag "github.com/spf13/pflag"
2828
)
2929

30+
// FParseErrWhitelist configures Flag parse errors to be ignored
31+
type FParseErrWhitelist flag.ParseErrorsWhitelist
32+
3033
// Command is just that, a command for your application.
3134
// E.g. 'go run ...' - 'run' is the command. Cobra requires
3235
// you to define the usage and description as part of your command
@@ -137,6 +140,9 @@ type Command struct {
137140
// TraverseChildren parses flags on all parents before executing child command.
138141
TraverseChildren bool
139142

143+
//FParseErrWhitelist flag parse errors to be ignored
144+
FParseErrWhitelist FParseErrWhitelist
145+
140146
// commands is the list of commands supported by this program.
141147
commands []*Command
142148
// parent is a parent command for this command.
@@ -1463,6 +1469,10 @@ func (c *Command) ParseFlags(args []string) error {
14631469
}
14641470
beforeErrorBufLen := c.flagErrorBuf.Len()
14651471
c.mergePersistentFlags()
1472+
1473+
//do it here after merging all flags and just before parse
1474+
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
1475+
14661476
err := c.Flags().Parse(args)
14671477
// Print warnings if they occurred (e.g. deprecated flag messages).
14681478
if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {

Diff for: command_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -1626,3 +1626,108 @@ func TestCalledAs(t *testing.T) {
16261626
t.Run(name, tc.test)
16271627
}
16281628
}
1629+
1630+
func TestFParseErrWhitelistBackwardCompatibility(t *testing.T) {
1631+
c := &Command{Use: "c", Run: emptyRun}
1632+
c.Flags().BoolP("boola", "a", false, "a boolean flag")
1633+
1634+
output, err := executeCommand(c, "c", "-a", "--unknown", "flag")
1635+
if err == nil {
1636+
t.Error("expected unknown flag error")
1637+
}
1638+
checkStringContains(t, output, "unknown flag: --unknown")
1639+
}
1640+
1641+
func TestFParseErrWhitelistSameCommand(t *testing.T) {
1642+
c := &Command{
1643+
Use: "c",
1644+
Run: emptyRun,
1645+
FParseErrWhitelist: FParseErrWhitelist{
1646+
UnknownFlags: true,
1647+
},
1648+
}
1649+
c.Flags().BoolP("boola", "a", false, "a boolean flag")
1650+
1651+
_, err := executeCommand(c, "c", "-a", "--unknown", "flag")
1652+
if err != nil {
1653+
t.Error("unexpected error: ", err)
1654+
}
1655+
}
1656+
1657+
func TestFParseErrWhitelistParentCommand(t *testing.T) {
1658+
root := &Command{
1659+
Use: "root",
1660+
Run: emptyRun,
1661+
FParseErrWhitelist: FParseErrWhitelist{
1662+
UnknownFlags: true,
1663+
},
1664+
}
1665+
1666+
c := &Command{
1667+
Use: "child",
1668+
Run: emptyRun,
1669+
}
1670+
c.Flags().BoolP("boola", "a", false, "a boolean flag")
1671+
1672+
root.AddCommand(c)
1673+
1674+
output, err := executeCommand(root, "child", "-a", "--unknown", "flag")
1675+
if err == nil {
1676+
t.Error("expected unknown flag error")
1677+
}
1678+
checkStringContains(t, output, "unknown flag: --unknown")
1679+
}
1680+
1681+
func TestFParseErrWhitelistChildCommand(t *testing.T) {
1682+
root := &Command{
1683+
Use: "root",
1684+
Run: emptyRun,
1685+
}
1686+
1687+
c := &Command{
1688+
Use: "child",
1689+
Run: emptyRun,
1690+
FParseErrWhitelist: FParseErrWhitelist{
1691+
UnknownFlags: true,
1692+
},
1693+
}
1694+
c.Flags().BoolP("boola", "a", false, "a boolean flag")
1695+
1696+
root.AddCommand(c)
1697+
1698+
_, err := executeCommand(root, "child", "-a", "--unknown", "flag")
1699+
if err != nil {
1700+
t.Error("unexpected error: ", err.Error())
1701+
}
1702+
}
1703+
1704+
func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
1705+
root := &Command{
1706+
Use: "root",
1707+
Run: emptyRun,
1708+
}
1709+
1710+
c := &Command{
1711+
Use: "child",
1712+
Run: emptyRun,
1713+
FParseErrWhitelist: FParseErrWhitelist{
1714+
UnknownFlags: true,
1715+
},
1716+
}
1717+
c.Flags().BoolP("boola", "a", false, "a boolean flag")
1718+
1719+
s := &Command{
1720+
Use: "sibling",
1721+
Run: emptyRun,
1722+
}
1723+
s.Flags().BoolP("boolb", "b", false, "a boolean flag")
1724+
1725+
root.AddCommand(c)
1726+
root.AddCommand(s)
1727+
1728+
output, err := executeCommand(root, "sibling", "-b", "--unknown", "flag")
1729+
if err == nil {
1730+
t.Error("expected unknown flag error")
1731+
}
1732+
checkStringContains(t, output, "unknown flag: --unknown")
1733+
}

0 commit comments

Comments
 (0)