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

env: does not work for positional arguments #408

Open
nf-brentsaner opened this issue Jul 12, 2024 · 0 comments
Open

env: does not work for positional arguments #408

nf-brentsaner opened this issue Jul 12, 2024 · 0 comments

Comments

@nf-brentsaner
Copy link

Given:

type Args struct {
	Foo string `env:"FOO" short:"f" long:"foo"`
	Bar PosArg `positional-args:"1"`
}

type PosArg struct {
	Pos string `env:"BAR" positional-arg-name:"POSITIONAL_ARG"`
}

the execution of BAR='a non-empty string' <program> will currently never populate Args.Bar.Pos.

This behavior (or... lack thereof, technically) occurs whether the env: tag is defined on Args.Bar, PosArg.Pos, or both.

I recommend changing the approach of how environment variables are processed:

1.) First, before go-flags' tags are processed, run it through e.g. https://pkg.go.dev/github.com/caarlos0/env/v11. While this adds a dependency, it has the benefit of offloading env var processing logic from this project (and ensures env vars are handed per *NIX conventions, see below) (the particular library I linked to is, I think, the most popular and well-maintained env-to-struct library for Golang). This should then also handle nested structs (such as positional args).

2.) Then perform normal go-flags' tag processing/reflection.

This not only would resolve this bug, but it is also backwards-compatible (the library I linked uses the same tag name) and would respect *NIX conventions for priority; first CLI flags/args/switches, then env vars, then configuration files. (This priority ordering arose as a convention because it allows for runtime-contextual options (CLI invocation) to apply over session-contextual options (env vars), both of which can override system-contextual/default options (config files).)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant