-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add Command::next_env_prefix for env variable prefixing #6281
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ use std::path::Path; | |
| // Internal | ||
| use crate::builder::ArgAction; | ||
| use crate::builder::IntoResettable; | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| use crate::builder::OsStr; | ||
| use crate::builder::PossibleValue; | ||
| use crate::builder::Str; | ||
| use crate::builder::StyledStr; | ||
|
|
@@ -101,6 +103,8 @@ pub struct Command { | |
| subcommands: Vec<Command>, | ||
| groups: Vec<ArgGroup>, | ||
| current_help_heading: Option<Str>, | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| current_env_prefix: Option<OsStr>, | ||
| current_disp_ord: Option<usize>, | ||
| subcommand_value_name: Option<Str>, | ||
| subcommand_heading: Option<Str>, | ||
|
|
@@ -185,6 +189,11 @@ impl Command { | |
|
|
||
| arg.help_heading | ||
| .get_or_insert_with(|| self.current_help_heading.clone()); | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| { | ||
| arg.env_prefix | ||
| .get_or_insert_with(|| self.current_env_prefix.clone()); | ||
| } | ||
| self.args.push(arg); | ||
| } | ||
|
|
||
|
|
@@ -2378,6 +2387,41 @@ impl Command { | |
| self | ||
| } | ||
|
|
||
| /// Sets a prefix to be prepended to the environment variable names of all | ||
| /// subsequent arguments added to this command. | ||
| /// | ||
| /// This is a stateful method that affects all future [`Arg`]s added via | ||
| /// [`Command::arg`]. An explicit [`Arg::env_prefix`] on an argument takes | ||
| /// precedence over this. | ||
| /// | ||
| /// The prefix and the argument's env name will be joined with `_`. | ||
| /// | ||
| /// This is modeled after [`Command::next_help_heading`]. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # #[cfg(all(feature = "env", feature = "string"))] { | ||
| /// # use clap_builder as clap; | ||
| /// # use clap::{Command, Arg}; | ||
| /// let cmd = Command::new("myapp") | ||
| /// .next_env_prefix("MYAPP") | ||
| /// .arg(Arg::new("config").long("config").env("CONFIG")) | ||
| /// .arg(Arg::new("verbose").long("verbose")); | ||
| /// // config's env var will be MYAPP_CONFIG | ||
| /// # } | ||
| /// ``` | ||
| /// | ||
| /// [`Command::arg`]: Command::arg() | ||
| /// [`Arg::env_prefix`]: crate::Arg::env_prefix() | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| #[inline] | ||
| #[must_use] | ||
| pub fn next_env_prefix(mut self, prefix: impl IntoResettable<OsStr>) -> Self { | ||
| self.current_env_prefix = prefix.into_resettable().into_option(); | ||
| self | ||
| } | ||
|
|
||
| /// Change the starting value for assigning future display orders for args. | ||
| /// | ||
| /// This will be used for any arg that hasn't had [`Arg::display_order`] called. | ||
|
|
@@ -3834,6 +3878,13 @@ impl Command { | |
| self.current_help_heading.as_deref() | ||
| } | ||
|
|
||
| /// Get the env prefix specified via [`Command::next_env_prefix`]. | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| #[inline] | ||
| pub fn get_next_env_prefix(&self) -> Option<&std::ffi::OsStr> { | ||
| self.current_env_prefix.as_ref().map(|s| s.as_os_str()) | ||
| } | ||
|
|
||
| /// Iterate through the *visible* aliases for this subcommand. | ||
| #[inline] | ||
| pub fn get_visible_aliases(&self) -> impl Iterator<Item = &str> + '_ { | ||
|
|
@@ -4440,6 +4491,18 @@ impl Command { | |
| } | ||
| } | ||
|
|
||
| // Apply env prefix to env variable names | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
|
epage marked this conversation as resolved.
|
||
| if let Some(Some(ref prefix)) = a.env_prefix { | ||
| if let Some((ref env_name, _)) = a.env { | ||
| let mut prefixed = prefix.to_os_string(); | ||
| prefixed.push("_"); | ||
| prefixed.push(env_name.as_os_str()); | ||
| let value = env::var_os(&prefixed); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not thrilled with us having looked up the env and now we look it up again
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is a tradeoff with the current design. Moving the prefix application earlier (e.g. into |
||
| a.env = Some((OsStr::from_string(prefixed), value)); | ||
| } | ||
| } | ||
|
|
||
| // Figure out implied settings | ||
| a._build(); | ||
| if hide_pv && a.is_takes_value_set() { | ||
|
|
@@ -5221,6 +5284,8 @@ impl Default for Command { | |
| subcommands: Default::default(), | ||
| groups: Default::default(), | ||
| current_help_heading: Default::default(), | ||
| #[cfg(all(feature = "env", feature = "string"))] | ||
| current_env_prefix: Default::default(), | ||
| current_disp_ord: Some(0), | ||
| subcommand_value_name: Default::default(), | ||
| subcommand_heading: Default::default(), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.