-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(arg_value): ArgValue can be used for possible_values Through the ArgValue it is possible: * `hide` possible_values from showing in completion, help and validation * add `about` to possible_values in completion * Resolved a few change-requests by epage * make clippy happy * add ArgValue::get_visible_value * remove verbose destructering * rename ArgValue::get_hidden to ArgValue::is_hidden * add test for help output of hidden ArgValues * Documentation for ArgValue There is an issue that required to implement From<&ArgValue> for ArgValue. We should probably find a solution without that. * fix requested changes by epage * fix formatting * add deref in possible_values call to remove From<&&str> * make clippy happy * use copied() instad of map(|v|*v) * Finishing up for merge, hopefully * changes requested by pksunkara
- Loading branch information
Showing
22 changed files
with
333 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/// The representation of a possible value of an argument. | ||
/// | ||
/// This is used for specifying [possible values] of [Args]. | ||
/// | ||
/// **NOTE:** This struct is likely not needed for most usecases as it is only required to | ||
/// [hide] single values from help messages and shell completions or to attach [about] to possible values. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use clap::{Arg, ArgValue}; | ||
/// let cfg = Arg::new("config") | ||
/// .takes_value(true) | ||
/// .value_name("FILE") | ||
/// .possible_value(ArgValue::new("fast")) | ||
/// .possible_value(ArgValue::new("slow").about("slower than fast")) | ||
/// .possible_value(ArgValue::new("secret speed").hidden(true)); | ||
/// ``` | ||
/// [Args]: crate::Arg | ||
/// [possible values]: crate::Arg::possible_value() | ||
/// [hide]: ArgValue::hidden() | ||
/// [about]: ArgValue::about() | ||
#[derive(Debug, Default, Clone)] | ||
pub struct ArgValue<'help> { | ||
pub(crate) name: &'help str, | ||
pub(crate) about: Option<&'help str>, | ||
pub(crate) hidden: bool, | ||
} | ||
|
||
impl<'help> From<&'help str> for ArgValue<'help> { | ||
fn from(s: &'help str) -> Self { | ||
Self::new(s) | ||
} | ||
} | ||
|
||
/// Getters | ||
impl<'help> ArgValue<'help> { | ||
/// Get the name of the argument value | ||
#[inline] | ||
pub fn get_name(&self) -> &str { | ||
self.name | ||
} | ||
|
||
/// Get the help specified for this argument, if any | ||
#[inline] | ||
pub fn get_about(&self) -> Option<&str> { | ||
self.about | ||
} | ||
|
||
/// Should the value be hidden from help messages and completion | ||
#[inline] | ||
pub fn is_hidden(&self) -> bool { | ||
self.hidden | ||
} | ||
|
||
/// Get the name if argument value is not hidden, `None` otherwise | ||
pub fn get_visible_name(&self) -> Option<&str> { | ||
if self.hidden { | ||
None | ||
} else { | ||
Some(self.name) | ||
} | ||
} | ||
} | ||
|
||
impl<'help> ArgValue<'help> { | ||
/// Creates a new instance of [`ArgValue`] using a string name. The name will be used to | ||
/// decide wether this value was provided by the user to an argument. | ||
/// | ||
/// **NOTE:** In case it is not [hidden] it will also be shown in help messages for arguments | ||
/// that use it as a [possible value] and have not hidden them through [`Arg::hide_possible_values(true)`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use clap::ArgValue; | ||
/// ArgValue::new("fast") | ||
/// # ; | ||
/// ``` | ||
/// [hidden]: ArgValue::hide | ||
/// [possible value]: crate::Arg::possible_values | ||
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values() | ||
pub fn new(name: &'help str) -> Self { | ||
ArgValue { | ||
name, | ||
..Default::default() | ||
} | ||
} | ||
|
||
/// Sets the help text of the value that will be displayed to the user when completing the | ||
/// value in a compatible shell. Typically, this is a short description of the value. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use clap::ArgValue; | ||
/// ArgValue::new("slow") | ||
/// .about("not fast") | ||
/// # ; | ||
/// ``` | ||
#[inline] | ||
pub fn about(mut self, about: &'help str) -> Self { | ||
self.about = Some(about); | ||
self | ||
} | ||
|
||
/// Hides this value from help text and shell completions. | ||
/// | ||
/// This is an alternative to hiding through [`Arg::hide_possible_values(true)`], if you only | ||
/// want to hide some values. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use clap::ArgValue; | ||
/// ArgValue::new("secret") | ||
/// .hidden(true) | ||
/// # ; | ||
/// ``` | ||
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values() | ||
#[inline] | ||
pub fn hidden(mut self, yes: bool) -> Self { | ||
self.hidden = yes; | ||
self | ||
} | ||
} |
Oops, something went wrong.
5580e8c
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.
Hello,
It would have been nice if this commit had a small note in
CHANGELOG
as it seems to be a breaking change inArg::possible_values
API5580e8c
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.
How so? I added some backward compatibility in e63760e.
5580e8c
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.
Weird, this morning we got the error on
3.0.0-beta.5
with this kind of code:And it went away with
5580e8c
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.
Do you have a fully reproducible minimal example code we can look into?
5580e8c
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.
Maybe there's just something I don't understand properly with Rust, trying to repro I noticed that it's the function call that triggers the issue. The example :
5580e8c
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.
@epage Wdyt?
5580e8c
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.
Explicitly declaring the Apps lifetime(
App<'static>
) gives us:I think whats happening is that
possible_values
is requiring theIntoIterator
live for'help
and not justhome_path()
.This is a breakage only in some cases and there is a workaround (remove
&
), so while its helpful to understand how we unintentionally broke something inbeta5
, I don't think there is anything we should change in the current code base because of this.