ardanlabs/conf

Parsing a bool flag followed by an arg returns an error

sedalu opened this issue · 4 comments

Parse unexpectedly returns an error when parsing a bool flag followed by an arg. This behavior differs from the standard library flag package.

I've put together a playground demonstrating the issue.

https://go.dev/play/p/VhuQrHnAKqD

I feel like this parse(cmd, "--bool", "arg") is behaving properly since this suggested "arg" is a value to set for the "--bool" parameter.

The standard library treats parse(cmd, "--bool", "arg") and parse(cmd, "--bool=arg") differently. Only the latter attempts to do assignment to the bool flag. Whereas conf treats them identically and attempts to do assignment in both cases.

I am working on a CLI that is using conf to parse flags, so that environment variables can be captured as well. The CLI takes an optional boolean flag and a required argument. Eg. cmd some/path or cmd --bool some/path. The current behavior of conf requires invocation of the CLI as cmd --bool=true some/path, which is not as intuitive as cmd --bool some/path.

If conf is working as intended, then it would be helpful to document the behavior.

You need the some/path option at the end. Have you looked at the code I'm using for parsing the command line? Can you see where to fix this behavior?

I've opened #23 with what should be the fix. It required updating how parse flags are tracked, the order that fields are processed, and how fields are sourced for flags.