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

Derive feature: Add a prefix for env vars #3517

Closed
2 tasks done
remoun opened this issue Feb 28, 2022 · 1 comment
Closed
2 tasks done

Derive feature: Add a prefix for env vars #3517

remoun opened this issue Feb 28, 2022 · 1 comment
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@remoun
Copy link

remoun commented Feb 28, 2022

Please complete the following tasks

Clap Version

3.1.2

Describe your use case

I'm working on adding env var support to a fairly large number of flags, with the Derive API. It would be great if we could specify a common prefix for all the env vars, without having to spell it out for each flag.

Describe the solution you'd like

Something like all_env_prefix = "FOO_", e.g.

#[derive(Debug, Parser)]
#[clap(rename_all_env = "kebab", all_env_prefix="FOO_")]
struct BehaviorModel {
    #[clap(env)]
    be_nice: String,
}

Alternatives, if applicable

The current alternative is to spell out each of the names with the env = "..." syntax, but that's very repetitive.

A more general approach would be to allow specifying a callback for mapping flag names to env vars, e.g.

#[derive(Debug, Parser)]
#[clap(rename_all_env = flag_to_env)]
struct BehaviorModel {
    #[clap(env)]
    be_nice: String,
}

pub fn flag_to_env(name: &str): String {
    use heck::ToShoutySnakeCase;
    return format!("FOO_{}", name.to_shouty_snake_case());
}

... but I'm not sure that's worth the overhead of supporting callbacks, for which there is no precedent AFAIK.

Additional Context

This seems similar to #2463 but for env vars renaming.

@remoun
Copy link
Author

remoun commented Feb 28, 2022

Oops, I guess this is a dupe of #3221

@remoun remoun closed this as completed Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

1 participant