Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bradmiro opened this issue Jan 2, 2020 · 3 comments
Open

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

bradmiro opened this issue Jan 2, 2020 · 3 comments
Milestone

Comments

@bradmiro
Copy link

@bradmiro bradmiro commented Jan 2, 2020

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>?
@gopherbot gopherbot added this to the Proposal milestone Jan 2, 2020
@gopherbot gopherbot added the Proposal label Jan 2, 2020
@bradmiro

This comment has been minimized.

Copy link
Author

@bradmiro bradmiro commented Jan 2, 2020

/cc @dmitshur

@dmitshur dmitshur changed the title proposal: go/flag, add clarity for debugging proposal: flag: add clarity for debugging Jan 2, 2020
@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Jan 2, 2020

I think the first suggestion would be a good improvement. It improves clarity of the error message without adding complexity and special cases to the code.

There isn't much precedent in the flag package or elsewhere in Go to try to guess the users' intention and try to make suggestions, so I don't believe the second suggested fix is as good as the first.

/cc @robpike per owners.

@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Jan 2, 2020

I don't think this change is large enough to need to go through the proposal process (https://github.com/golang/proposal#scope), so I'll make it a regular issue for now.

@dmitshur dmitshur added NeedsInvestigation and removed Proposal labels Jan 2, 2020
@gopherbot gopherbot added the Proposal label Jan 2, 2020
@dmitshur dmitshur modified the milestones: Proposal, Backlog Jan 2, 2020
@dmitshur dmitshur changed the title proposal: flag: add clarity for debugging flag: improve clarity of "flag provided but not defined" error message Jan 2, 2020
@dmitshur dmitshur removed the Proposal label Jan 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.