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

Add support for setting a global env var prefix #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hhromic
Copy link
Contributor

@hhromic hhromic commented Jul 1, 2024

This PR implements support for setting a global env var prefix (see linked issue).
While writing common reusable args structs for our Go programs today, I realized on the value and usefulness of this feature in real-world applications and decided to implement it for go-arg.

The implementation adds a new EnvPrefix field in Config and therefore is only accessible when using NewParser().
Because the implementation adds the prefix to spec.env, the prefix is reflected everywhere, including usage help text.

To write the unit test, I had to extend parseWithEnv so a Config can be passed instead of hard-coding Config{}.

And last but not least, an example in the README was using os.Args instead of os.Args[1:] in p.Parse(). This probably came from a very outdated version, but I fixed it as well now.

Resolves #204

Copy link
Owner

@alexflint alexflint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One request though: don't add the joining underscore, instead let the user put it in themselves if they need it.

parse.go Outdated Show resolved Hide resolved
@hhromic
Copy link
Contributor Author

hhromic commented Jul 6, 2024

Thanks! One request though: don't add the joining underscore, instead let the user put it in themselves if they need it.

Done! The underscore is not hard-coded anymore :)

* Also made newline separations around sections consistent
* Also fixed usage of `p.Parse()` in env variable ignore example
@hhromic
Copy link
Contributor Author

hhromic commented Sep 7, 2024

Rebased this on top of latest master :)

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

Successfully merging this pull request may close these issues.

Global env var prefix
2 participants