enforce required flags (#502)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Di Xu · committed by Eric Paris · parent 5020481 · Browse files · 2 files·+70 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+70 added−0 removed
- other:+70 added−0 removed
Provenance
- Commit
4d6af28- Tree
c9b4b4e- Author
Di Xu <dixudx@users.noreply.github.com>- Committer
Eric Paris <eparis@redhat.com>- Parent
5020481
2 files changed+70 added−0 removed
modified:command_test.go+48 added
436 unmodified lines437 if c.Name() != sub.Name() {438 t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name())439 }440 }441+442+func TestRequiredFlags(t *testing.T) {443+ c := &Command{Use: "c", Run: func(*Command, []string) {}}444+ output := new(bytes.Buffer)445+ c.SetOutput(output)446+ c.Flags().String("foo1", "", "required foo1")447+ c.MarkFlagRequired("foo1")448+ c.Flags().String("foo2", "", "required foo2")449+ c.MarkFlagRequired("foo2")450+ c.Flags().String("bar", "", "optional bar")451+452+ expected := fmt.Sprintf("Required flag(s) %q, %q have/has not been set", "foo1", "foo2")453+454+ if err := c.Execute(); err != nil {455+ if err.Error() != expected {456+ t.Errorf("expected %v, got %v", expected, err.Error())457+ }458+ }459+}460+461+func TestPersistentRequiredFlags(t *testing.T) {462+ parent := &Command{Use: "parent", Run: func(*Command, []string) {}}463+ output := new(bytes.Buffer)464+ parent.SetOutput(output)465+ parent.PersistentFlags().String("foo1", "", "required foo1")466+ parent.MarkPersistentFlagRequired("foo1")467+ parent.PersistentFlags().String("foo2", "", "required foo2")468+ parent.MarkPersistentFlagRequired("foo2")469+ parent.Flags().String("foo3", "", "optional foo3")470+471+ child := &Command{Use: "child", Run: func(*Command, []string) {}}472+ child.Flags().String("bar1", "", "required bar1")473+ child.MarkFlagRequired("bar1")474+ child.Flags().String("bar2", "", "required bar2")475+ child.MarkFlagRequired("bar2")476+ child.Flags().String("bar3", "", "optional bar3")477+478+ parent.AddCommand(child)479+ parent.SetArgs([]string{"child"})480+481+ expected := fmt.Sprintf("Required flag(s) %q, %q, %q, %q have/has not been set", "bar1", "bar2", "foo1", "foo2")482+483+ if err := parent.Execute(); err != nil {484+ if err.Error() != expected {485+ t.Errorf("expected %v, got %v", expected, err.Error())486+ }487+ }488+}modified:command.go+22 added
691 unmodified lines692 } else if c.PreRun != nil {693 c.PreRun(c, argWoFlags)694 }695 696+ if err := c.validateRequiredFlags(); err != nil {697+ return err698+ }699 if c.RunE != nil {700 if err := c.RunE(c, argWoFlags); err != nil {701 return err702 }109 unmodified lines812 }813 return c.Args(c, args)814 }815 816+func (c *Command) validateRequiredFlags() error {817+ flags := c.Flags()818+ missingFlagNames := []string{}819+ flags.VisitAll(func(pflag *flag.Flag) {820+ requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]821+ if !found {822+ return823+ }824+ if (requiredAnnotation[0] == "true") && !pflag.Changed {825+ missingFlagNames = append(missingFlagNames, pflag.Name)826+ }827+ })828+829+ if len(missingFlagNames) > 0 {830+ return fmt.Errorf(`Required flag(s) "%s" have/has not been set`, strings.Join(missingFlagNames, `", "`))831+ }832+ return nil833+}834+835 // InitDefaultHelpFlag adds default help flag to c.836 // It is called automatically by executing the c or by calling help and usage.837 // If c already has help flag, it will do nothing.838 func (c *Command) InitDefaultHelpFlag() {573 unmodified lines