Skip to content
Myr

added a flag to disable flags

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Mark Bates · committed by Mark Bates · parent f368244 · Browse files · 2 files+31 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+31 added−0 removed

  • other:+31 added−0 removed

2 files+31 added−0 removed

modified:command_test.go+21 added
1package cobra23import (4 "os"5 "reflect"6 "testing"7)8
103 unmodified lines
112 t.Errorf("expected: %v, got: %v", test.output, output)113 }114 }115}116117func Test_DisableFlagParsing(t *testing.T) {118 as := []string{"-v", "-race", "-file", "foo.go"}119 targs := []string{}120 cmdPrint := &Command{121 DisableFlagParsing: true,122 Run: func(cmd *Command, args []string) {123 targs = args124 },125 }126 osargs := []string{"cmd"}127 os.Args = append(osargs, as...)128 err := cmdPrint.Execute()129 if err != nil {130 t.Error(err)131 }132 if !reflect.DeepEqual(as, targs) {133 t.Errorf("expected: %v, got: %v", as, targs)134 }135}
modified:command.go+10 added
118 unmodified lines
119 // Disable the suggestions based on Levenshtein distance that go along with 'unknown command' messages120 DisableSuggestions bool121 // If displaying suggestions, allows to set the minimum levenshtein distance to display, must be > 0122 SuggestionsMinimumDistance int123124 // Disable the flag parsing. If this is true all flags will be passed to the command as arguments.125 DisableFlagParsing bool126}127128// os.Args[1:] by default, if desired, can be overridden129// particularly useful when testing.
408 unmodified lines
538 return flag.ErrHelp539 }540541 c.preRun()542543 argWoFlags := c.Flags().Args()544 if c.DisableFlagParsing {545 argWoFlags = a546 }547548 for p := c; p != nil; p = p.Parent() {549 if p.PersistentPreRunE != nil {550 if err := p.PersistentPreRunE(c, argWoFlags); err != nil {
622 unmodified lines
1173}11741175// ParseFlags parses persistent flag tree & local flags1176func (c *Command) ParseFlags(args []string) (err error) {1177 if c.DisableFlagParsing {1178 return nil1179 }1180 c.mergePersistentFlags()1181 err = c.Flags().Parse(args)1182 return1183}
44 unmodified lines