MCPcopy
hub / github.com/spf13/pflag / TestHelp

Function TestHelp

flag_test.go:1007–1047  ·  view source on GitHub ↗

Test that -help invokes the usage message and returns ErrHelp.

(t *testing.T)

Source from the content-addressed store, hash-verified

1005
1006// Test that -help invokes the usage message and returns ErrHelp.
1007func TestHelp(t *testing.T) {
1008 helpCalled := false
1009 fs := NewFlagSet("help test", ContinueOnError)
1010 fs.Usage = func() { helpCalled = true }
1011 var flag bool
1012 fs.BoolVar(&flag, "flag", false, "regular flag")
1013 // Regular flag invocation should work
1014 err := fs.Parse([]string{"--flag=true"})
1015 if err != nil {
1016 t.Fatal("expected no error; got ", err)
1017 }
1018 if !flag {
1019 t.Error("flag was not set by --flag")
1020 }
1021 if helpCalled {
1022 t.Error("help called for regular flag")
1023 helpCalled = false // reset for next test
1024 }
1025 // Help flag should work as expected.
1026 err = fs.Parse([]string{"--help"})
1027 if err == nil {
1028 t.Fatal("error expected")
1029 }
1030 if err != ErrHelp {
1031 t.Fatal("expected ErrHelp; got ", err)
1032 }
1033 if !helpCalled {
1034 t.Fatal("help was not called")
1035 }
1036 // If we define a help flag, that should override.
1037 var help bool
1038 fs.BoolVar(&help, "help", false, "help flag")
1039 helpCalled = false
1040 err = fs.Parse([]string{"--help"})
1041 if err != nil {
1042 t.Fatal("expected no error for defined --help; got ", err)
1043 }
1044 if helpCalled {
1045 t.Fatal("help was called; should not have been for defined help flag")
1046 }
1047}
1048
1049func TestNoInterspersed(t *testing.T) {
1050 f := NewFlagSet("test", ContinueOnError)

Callers

nothing calls this directly

Calls 4

BoolVarMethod · 0.95
ParseMethod · 0.95
NewFlagSetFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected