-
-
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
Open
veeceey
wants to merge
2
commits into
clap-rs:master
Choose a base branch
from
veeceey:feat/issue-3221-env-prefix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+371
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Show resolved
Hide 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 |
||
| 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(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.