-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 an env_prefix derive option for a consistent prefix for environment variables #3221
Comments
Wanted to double check. Did you mean for your "after" to be: #[derive(Parser)]
#[clap(env_prefix)]
pub struct Opts {
#[clap(long, env)]
pub config_dir: String,
#[clap(long, env)]
pub temp_dir: String,
#[clap(long, env)]
pub cache_dir: String,
#[clap(long)]
pub isolated: bool
}
|
If we do this, we'd need to make clear that |
Whoops, yes my bad, you'd still specify
Why wouldn't we want it to propagate? I can see that making the user be explicit is probably a better idea, but I could also see
Definitely not an issue to specify |
Sorry I didn't go into more details on this. We can't make derives for different structs interact at build-time. We have to do all of this at runtime. Our options are
In thinking about this, maybe it could be worthwhile to add this to the builder API. |
Adding this to the builder API would basically mean that If so that seems reasonable to me.
For what it's worth I think it would be fine to just have |
We want to avoid this. Someone refactoring their app from a single |
Alright, so the proposal is we add an Questions
So in writing up these questions, I think we've got our answers. |
It's would be amazing that flatten to do it automatically for example I would like: #[derive(Debug, Args, Deserialize)]
pub struct PostgreSQL {
#[clap(long, env)]
pub host: Option<String>,
}
#[derive(Parser, Debug)]
#[clap(author, version, about)]
#[clap(env_prefix)]
pub struct ConfigArgs {
#[clap(flatten)]
pub postgresql: PostgreSQL,
} This mean host would have the env #[derive(Debug, Args, Deserialize)]
#[clap(env_prefix(name, struct_name))]
pub struct PostgreSQL {
#[clap(long, env)]
pub host: Option<String>,
} |
Note that my later post shifted this from being a derive feature to a builder feature which means it could work across |
Add an `env_prefix` option to our CLI parsing module. This option will eventually come to clap itself (clap-rs/clap#3221), but for now we can roll it ourselves using clap's reflection API. This option will allow us to factor out common CLI options into their own structs while still having the environment variable prefixes determined by the root command.
Add an `env_prefix` option to our CLI parsing module. This option will eventually come to clap itself (clap-rs/clap#3221), but for now we can roll it ourselves using clap's reflection API. This option will allow us to factor out common CLI options into their own structs while still having the environment variable prefixes determined by the root command.
Add an `env_prefix` option to our CLI parsing module. This option will eventually come to clap itself (clap-rs/clap#3221), but for now we can roll it ourselves using clap's reflection API. This option will allow us to factor out common CLI options into their own structs while still having the environment variable prefixes determined by the root command.
@epage Is this planned to be implemented soon? |
It has the |
Clap doesn't yet support an easy derive mechanism for adding a prefix to the environment variable names: clap-rs/clap#3221. If we run into any conflicts / issues we can prefix these manually.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Alternatively, #5050 would allow doing this without any other new feature |
Maintainer's notes
App::env_prefix
andArg::env_prefix
and is modeled afterhelp_heading
App::env_prefix
applies to all futureArg
s and is set during.arg()
Arg::env_prefix
takes precedence overApp::env_prefix
App::env_prefix
part-way through struct definition_build
(env
will internally need to be aCow
), leaving help generation and env variable look up unchangedPlease complete the following tasks
Clap Version
v3.0.0-rc.8
Describe your use case
This was previously discussed in TeXitoi/structopt#370.
Basically the request is to be able to allow a specific prefix to be appended to all
#[clap(env)]
derived environment variable names.Describe the solution you'd like
Before:
After:
(where a custom prefix could be set by
#[clap(env_prefix="foo")]
, but the default was the#[clap(name)]
)Alternatives, if applicable
I don't think there's an alternative solution to manually setting the environment variable for every option that needs it.
There was a suggestion in the above issue about allowing this to be calculated via a function vs a string literal, but I can't think of a good use-case for it, so it seems unnecessary for this feature.
Additional Context
Examples from the above issue:
rust-analyzer
looks forRA_LOG
var,rustc
looks forRUSC_LOG
var,sccache
looks forSCCACHE_ENDPOINT
,SCCACHE_CACHE_SIZE
,SCCACHE_REDIS
and other....The text was updated successfully, but these errors were encountered: