mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
docs: Provide custom version flag example
This commit is contained in:
parent
39f5e807af
commit
e74864f88a
1 changed files with 29 additions and 0 deletions
|
@ -1244,12 +1244,41 @@ impl Command {
|
|||
/// # use clap_builder as clap;
|
||||
/// # use clap::{Command, error::ErrorKind};
|
||||
/// let res = Command::new("myprog")
|
||||
/// .version("1.0.0")
|
||||
/// .disable_version_flag(true)
|
||||
/// .try_get_matches_from(vec![
|
||||
/// "myprog", "--version"
|
||||
/// ]);
|
||||
/// assert!(res.is_err());
|
||||
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
||||
/// ```
|
||||
///
|
||||
/// You can create a custom version flag with [`ArgAction::Version`]
|
||||
/// ```rust
|
||||
/// # use clap_builder as clap;
|
||||
/// # use clap::{Command, Arg, ArgAction, error::ErrorKind};
|
||||
/// let mut cmd = Command::new("myprog")
|
||||
/// .version("1.0.0")
|
||||
/// // Remove the `-V` short flag
|
||||
/// .disable_version_flag(true)
|
||||
/// .arg(
|
||||
/// Arg::new("version")
|
||||
/// .long("version")
|
||||
/// .action(ArgAction::Version)
|
||||
/// .help("Print version")
|
||||
/// );
|
||||
///
|
||||
/// let res = cmd.try_get_matches_from_mut(vec![
|
||||
/// "myprog", "-V"
|
||||
/// ]);
|
||||
/// assert!(res.is_err());
|
||||
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
||||
///
|
||||
/// let res = cmd.try_get_matches_from_mut(vec![
|
||||
/// "myprog", "--version"
|
||||
/// ]);
|
||||
/// assert!(res.is_err());
|
||||
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::DisplayVersion);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn disable_version_flag(self, yes: bool) -> Self {
|
||||
|
|
Loading…
Reference in a new issue