Skip to content

flag: improve clarity of "flag provided but not defined" error message #36364

@bradmiro

Description

@bradmiro

Version: go1.13.5

I have a code snippet with a series of flags:

var ( flag1 string flag2 string ) func init() { flag.StringVar(&flag1, "flag1", "", "value for flag 1") flag.StringVar(&flag2, "flag2", "", "value for flag 2") } func main () { ... } 

After building, if this is executed in a bash shell:
./script "-flag1 value1" "-flag2 value2"
the output I see is this:

flag provided but not defined: -flag1 value1 Usage of ./script: -flag1 string value for flag1 -flag2 string value for flag2 

Now, this code doesn't work because of the quotes in the bash command and removing the quotes makes this work exactly as expected. This took me a while to debug as I wasn't able to initially interpret from flag provided but not defined: -flag1 value1 that the program was interpreting -flag1 value1 as the literal flag, as opposed to the flag and the value.

I might suggest one of two fixes:

  1. Slightly modify the error message from
    flag provided but not defined: FLAG
    to
    flag "FLAG" provided but not defined

  2. Add logic to detect common potential mistakes such as:

flag provided but not defined: FLAG Did you mean to include <detected mistake such as space / equals sign etc>? 

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions