Skip to content
Myr

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

2 files+115 added−0 removed

modified:command_test.go+105 added
1624 unmodified lines
1625 for name, tc := range tests {1626 t.Run(name, tc.test)1627 }1628}16291630func TestFParseErrWhitelistBackwardCompatibility(t *testing.T) {1631 c := &Command{Use: "c", Run: emptyRun}1632 c.Flags().BoolP("boola", "a", false, "a boolean flag")16331634 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}16401641func 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")16501651 _, err := executeCommand(c, "c", "-a", "--unknown", "flag")1652 if err != nil {1653 t.Error("unexpected error: ", err)1654 }1655}16561657func TestFParseErrWhitelistParentCommand(t *testing.T) {1658 root := &Command{1659 Use: "root",1660 Run: emptyRun,1661 FParseErrWhitelist: FParseErrWhitelist{1662 UnknownFlags: true,1663 },1664 }16651666 c := &Command{1667 Use: "child",1668 Run: emptyRun,1669 }1670 c.Flags().BoolP("boola", "a", false, "a boolean flag")16711672 root.AddCommand(c)16731674 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}16801681func TestFParseErrWhitelistChildCommand(t *testing.T) {1682 root := &Command{1683 Use: "root",1684 Run: emptyRun,1685 }16861687 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")16951696 root.AddCommand(c)16971698 _, err := executeCommand(root, "child", "-a", "--unknown", "flag")1699 if err != nil {1700 t.Error("unexpected error: ", err.Error())1701 }1702}17031704func TestFParseErrWhitelistSiblingCommand(t *testing.T) {1705 root := &Command{1706 Use: "root",1707 Run: emptyRun,1708 }17091710 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")17181719 s := &Command{1720 Use: "sibling",1721 Run: emptyRun,1722 }1723 s.Flags().BoolP("boolb", "b", false, "a boolean flag")17241725 root.AddCommand(c)1726 root.AddCommand(s)17271728 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 lines
2627 flag "github.com/spf13/pflag"28)2930// FParseErrWhitelist configures Flag parse errors to be ignored31type FParseErrWhitelist flag.ParseErrorsWhitelist3233// 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 lines
139140 // TraverseChildren parses flags on all parents before executing child command.141 TraverseChildren bool142143 //FParseErrWhitelist flag parse errors to be ignored144 FParseErrWhitelist FParseErrWhitelist145146 // commands is the list of commands supported by this program.147 commands []*Command148 // parent is a parent command for this command.149 parent *Command
1318 unmodified lines
1468 c.flagErrorBuf = new(bytes.Buffer)1469 }1470 beforeErrorBufLen := c.flagErrorBuf.Len()1471 c.mergePersistentFlags()14721473 //do it here after merging all flags and just before parse1474 c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)14751476 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