-
-
Notifications
You must be signed in to change notification settings - Fork 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 is_env_present to ArgMatches #1833
Conversation
5998f07
to
6da733c
Compare
So far looks good, but let's rename it to Also, I would go with this description
|
src/parse/arg_matcher.rs
Outdated
pub fn set_env_set(&mut self, arg: &Id) { | ||
let ma = self.entry(arg).or_insert(MatchedArg::new()); | ||
ma.env_set = true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this helper? It's a two line function and you're using it in only one place.
FYI: if a function is not supposed to be exported to user (it's not a part of public API), you should use pub(crate)
but not pub
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this helper?
I liked the encapsulation it provided. Otherwise, I had to bring MatchedArg
into parser.rs
. Happy to remove it if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name here is not readable. Once you rename the function and the field, you should probably rename this.
Signed-off-by: David McNeil <mcneil.david2@gmail.com>
This actually is different than how |
Then this function is pointless since it would be a synonym for std::env::var("ENV_VAR") == Err(ValError::NotPresent) No point to bring it in clap 🤷 |
What is your use case? I suspect the semantic I described is the one you really need. |
While this is mostly true, there could potentially be a race condition from when The real problem is that if all you have is an
I am trying to distinguish between when an argument's value was set with an environment variable vs when it was set with a default value. One way to do this would be to check if the environment variable was set.
Yes, this would work too and I agree would be more useful. But because arguments can have multiple values does this basically just ensure that the first argument in values was from the environment variable? How does that work with |
You are kinda the developer of your CLI, aren't you? 😄 How can you not know it? You define these arguments, you define the env name, therefore you know it.
The proper way to do this is adding I don't really think that introducing a full synonym of a well known function from std will get us anywhere, no offense.
If you ask me, use clap::{App, Arg};
fn main() {
std::env::set_var("ENV_VAR", "val_env");
let m = App::new("app")
.arg(
Arg::with_name("test")
.env("ENV_VAR")
.min_values(0)
)
.get_matches_from(&["test", "val1", "val2"]);
let values = m.values_of("test").unwrap().collect::<Vec<_>>();
assert_eq!(values, &["val1", "val2", "val_env"]);
} I'm going to raise an issue about that. |
If you are iterating over a list of argument names and getting matches from
This sounds like a better approach, thanks. I will close this PR and work on that solution.
Yes! Completely agree. This was very confusing to me and I would love to see it behave the way you describe. |
Closes #1345
Signed-off-by: David McNeil mcneil.david2@gmail.com