I've noticed differing behavior if I specify a flag via the command line vs if that flag comes from a config file.
Here is an example:
package main
import (
"errors"
"flag"
"os"
"github.com/peterbourgon/ff"
)
func main() {
fs := flag.NewFlagSet("my-program", flag.ExitOnError)
var (
_ = fs.String("config", "", "config file (optional)")
)
fs.Func("aflag", "a flag that raises a error",
func(flagValue string) error {
return errors.New("bad flag")
},
)
ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("MY_PROGRAM"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
}
If I run it by passing the flag explicitly, I get
./my-program -aflag bad
invalid value "bad" for flag -aflag: bad flag
Usage of my-program:
-aflag value
a flag that raises a error
-config string
config file (optional)
but running ./my-program -config config.txt where config.txt is aflag bad executes without error. I know fs.Func runs and the error is returned in both cases, but I am not sure why it doesn't trigger an an exit in the latter case.
Thanks!
I've noticed differing behavior if I specify a flag via the command line vs if that flag comes from a config file.
Here is an example:
If I run it by passing the flag explicitly, I get
but running
./my-program -config config.txtwhereconfig.txtisaflag badexecutes without error. I knowfs.Funcruns and the error is returned in both cases, but I am not sure why it doesn't trigger an an exit in the latter case.Thanks!