I'm not sure how you expect the *nfs=true* case to work. The "-data=x" flag 
will be passed to test binary, as described in the documentation.  But the 
test binary is not just your test code, it also contains the testing 
framework, which must run some variant of *flag.Parse()* in order to parse 
its own flags. That is where the error comes in, because the default parser 
does not contain your flag. 

If you add "*flag.StringVar(&data, "data", datainit, "data")*" to the nfs 
case, then it runs, though I suspect that defeats the purpose. 

Good Luck,
     Jake

On Tuesday, December 12, 2017 at 7:25:37 PM UTC-5, gocss wrote:
>
> when I run the minimal program below it errors about NOT knowing  -data 
> flag:
> go test -v -args -data=x
> flag provided but not defined: -data
> Usage of /tmp/go-build454762170/
> github.com/phcurtis/flagexp/_test/flagexp.test:
>   -test.bench regexp .... blah blah,
>
> if you change nfs=false ... and then it uses flag.Parse etc ... code runs 
> without issue
> but I want to handle error cases in other code I'm working and use  the 
> flag.NewFlagSet with go test ...
> note the below code in either case works with go run but not with go test.
>
>
> package main
>
> import (
>     "flag"
>     "fmt"
>     "os"
>     "testing"
> )
>
> var data string
> var test func(t *testing.T)
> var datainit = "defValue"
> var nfs = true
>
> func init() {
>     if nfs {
>         flag3 := flag.NewFlagSet("nfs", flag.ContinueOnError)
>         flag3.StringVar(&data, "data", datainit, "data")
>         test = func(t *testing.T) {
>             fmt.Println("TP1")
>             err := flag3.Parse(os.Args)
>             fmt.Printf("data:%v err:%v\n", data, err)
>         }
>         return
>     }
>     flag.StringVar(&data, "data", datainit, "data")
>     test = func(t *testing.T) {
>         fmt.Println("TP2")
>         flag.Parse()
>         fmt.Printf("data:%v\n", data)
>     }
> }
>
> func Test_code(t *testing.T) {
>     test(t)
> }
>
> ***********************************************************
> heres full output when nfs=true
> paul@t560:~/go/src/github.com/phcurtis/flagexp$ go test -v -args -data=x
> flag provided but not defined: -data
> Usage of /tmp/go-build454762170/
> github.com/phcurtis/flagexp/_test/flagexp.test:
>   -test.bench regexp
>         run only benchmarks matching regexp
>   -test.benchmem
>         print memory allocations for benchmarks
>   -test.benchtime d
>         run each benchmark for duration d (default 1s)
>   -test.blockprofile file
>         write a goroutine blocking profile to file
>   -test.blockprofilerate rate
>         set blocking profile rate (see runtime.SetBlockProfileRate) 
> (default 1)
>   -test.count n
>         run tests and benchmarks n times (default 1)
>   -test.coverprofile file
>         write a coverage profile to file
>   -test.cpu list
>         comma-separated list of cpu counts to run each test with
>   -test.cpuprofile file
>         write a cpu profile to file
>   -test.list regexp
>         list tests, examples, and benchmarch maching regexp then exit
>   -test.memprofile file
>         write a memory profile to file
>   -test.memprofilerate rate
>         set memory profiling rate (see runtime.MemProfileRate)
>   -test.mutexprofile string
>         write a mutex contention profile to the named file after execution
>   -test.mutexprofilefraction int
>         if >= 0, calls runtime.SetMutexProfileFraction() (default 1)
>   -test.outputdir dir
>         write profiles to dir
>   -test.parallel n
>         run at most n tests in parallel (default 4)
>   -test.run regexp
>         run only tests and examples matching regexp
>   -test.short
>         run smaller test suite to save time
>   -test.timeout d
>         panic test binary after duration d (0 means unlimited)
>   -test.trace file
>         write an execution trace to file
>   -test.v
>         verbose: print additional output
> exit status 2
> FAIL    github.com/phcurtis/flagexp    0.001s
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to