Skip to content

flag.Func error not raised if trigged via config file #90

@youngpm

Description

@youngpm

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions