Add support for ignoring parse errors (#662)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rajat Jindal · committed by Eric Paris · parent a1f051b · Browse files · 2 files·+115 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+115 added−0 removed
- other:+115 added−0 removed
Provenance
- Commit
4dab30c- Tree
c9aef76- Author
Rajat Jindal <rajatjindal83@gmail.com>- Committer
Eric Paris <eparis@redhat.com>- Parent
a1f051b
2 files changed+115 added−0 removed
modified:command_test.go+105 added
1624 unmodified lines1625 for name, tc := range tests {1626 t.Run(name, tc.test)1627 }1628 }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+}modified:command.go+10 added
25 unmodified lines26 27 flag "github.com/spf13/pflag"28 )29 30+// FParseErrWhitelist configures Flag parse errors to be ignored31+type FParseErrWhitelist flag.ParseErrorsWhitelist32+33 // Command is just that, a command for your application.34 // E.g. 'go run ...' - 'run' is the command. Cobra requires35 // you to define the usage and description as part of your command36 // definition to ensure usability.102 unmodified lines139 140 // TraverseChildren parses flags on all parents before executing child command.141 TraverseChildren bool142 143+ //FParseErrWhitelist flag parse errors to be ignored144+ FParseErrWhitelist FParseErrWhitelist145+146 // commands is the list of commands supported by this program.147 commands []*Command148 // parent is a parent command for this command.149 parent *Command1318 unmodified lines1468 c.flagErrorBuf = new(bytes.Buffer)1469 }1470 beforeErrorBufLen := c.flagErrorBuf.Len()1471 c.mergePersistentFlags()1472+1473+ //do it here after merging all flags and just before parse1474+ c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)1475+1476 err := c.Flags().Parse(args)1477 // Print warnings if they occurred (e.g. deprecated flag messages).1478 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {1479 c.Print(c.flagErrorBuf.String())38 unmodified lines