Created
August 9, 2018 19:09
-
-
Save FrenchBen/a8ed180ef7e4b844e898e340cfca9152 to your computer and use it in GitHub Desktop.
Simple pFlag go test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/spf13/pflag" | |
) | |
var ( | |
argPort = pflag.Int("port", 8443, "The secure port to listen to for incoming HTTPS requests.") | |
) | |
func main() { | |
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | |
pflag.Parse() | |
fmt.Printf("We got port: %d", *argPort) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"github.com/spf13/pflag" | |
"gotest.tools/assert" | |
is "gotest.tools/assert/cmp" | |
) | |
func TestCliFlags(t *testing.T) { | |
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError) | |
err := flags.Parse([]string{ | |
"--port=8080", | |
}) | |
assert.Check(t, err) | |
assert.Check(t, is.Equal(8080, *argPort)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output of test: