package main import ( "testing" "os" ) func WithArgs(args []string, test func ()) { old_args := os.Args defer (func() { os.Args = old_args })() os.Args = args test() } func TestParseOpts(t *testing.T) { args := []string{"cat", "-n", "main.go"} WithArgs(args, func () { opts := ParseOpts() if opts.Number != true { t.Errorf("opts.Number should be true, not: %v", opts.Number) } }) /* // This fails because flag can't be run more than once args = []string{"cat.exe", "-s", "main.go"} WithArgs(args, func () { opts := ParseOpts() if opts.Number != true { t.Errorf("opts.Number should be true, not: %v", opts.Number) } }) */ }