doc(parser): Specify what unwraps are for

This commit is contained in:
Ed Page 2022-05-17 16:00:03 -05:00
parent 91fe9eedc9
commit c9e3b81e3c
2 changed files with 60 additions and 25 deletions

View file

@ -1041,13 +1041,16 @@ impl<'help> Arg<'help> {
/// ["cmd", "--hostname", "rust-lang.org", "--port", "3001"]
/// ).unwrap();
///
/// let color: &String = m.get_one("color").unwrap().unwrap();
/// let color: &String = m.get_one("color")
/// .expect("matches definition").expect("default");
/// assert_eq!(color, "auto");
///
/// let hostname: &String = m.get_one("hostname").unwrap().unwrap();
/// let hostname: &String = m.get_one("hostname")
/// .expect("matches definition").expect("required");
/// assert_eq!(hostname, "rust-lang.org");
///
/// let port: u16 = *m.get_one("port").unwrap().unwrap();
/// let port: u16 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
pub fn value_parser(mut self, parser: impl Into<super::ValueParser>) -> Self {

View file

@ -45,13 +45,16 @@ use crate::parser::AnyValueId;
/// ["cmd", "--hostname", "rust-lang.org", "--port", "3001"]
/// ).unwrap();
///
/// let color: &String = m.get_one("color").unwrap().unwrap();
/// let color: &String = m.get_one("color")
/// .expect("matches definition").expect("default");
/// assert_eq!(color, "auto");
///
/// let hostname: &String = m.get_one("hostname").unwrap().unwrap();
/// let hostname: &String = m.get_one("hostname")
/// .expect("matches definition").expect("required");
/// assert_eq!(hostname, "rust-lang.org");
///
/// let port: u16 = *m.get_one("port").unwrap().unwrap();
/// let port: u16 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
#[derive(Clone)]
@ -97,10 +100,12 @@ impl ValueParser {
/// .arg(
/// clap::Arg::new("env")
/// .value_parser(clap::builder::ValueParser::new(parse_env_var))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "key=value"]).unwrap();
/// let port: &EnvVar = m.get_one("env").unwrap().unwrap();
/// let port: &EnvVar = m.get_one("env")
/// .expect("matches definition").expect("required");
/// assert_eq!(*port, ("key".into(), Some("value".into())));
/// ```
pub fn new(other: impl AnyValueParser + Send + Sync + 'static) -> Self {
@ -120,10 +125,12 @@ impl ValueParser {
/// .arg(
/// clap::Arg::new("download")
/// .value_parser(clap::value_parser!(bool))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "true"]).unwrap();
/// let port: bool = *m.get_one("download").unwrap().unwrap();
/// let port: bool = *m.get_one("download")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, true);
///
/// assert!(cmd.try_get_matches_from_mut(["cmd", "forever"]).is_err());
@ -144,10 +151,12 @@ impl ValueParser {
/// .arg(
/// clap::Arg::new("port")
/// .value_parser(clap::value_parser!(String))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "80"]).unwrap();
/// let port: &String = m.get_one("port").unwrap().unwrap();
/// let port: &String = m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, "80");
/// ```
pub const fn string() -> Self {
@ -170,10 +179,12 @@ impl ValueParser {
/// .arg(
/// clap::Arg::new("output")
/// .value_parser(clap::value_parser!(PathBuf))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "hello.txt"]).unwrap();
/// let port: &PathBuf = m.get_one("output").unwrap().unwrap();
/// let port: &PathBuf = m.get_one("output")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, Path::new("hello.txt"));
///
/// assert!(cmd.try_get_matches_from_mut(["cmd", ""]).is_err());
@ -252,7 +263,8 @@ impl ValueParser {
/// ["cmd", "--hostname", "rust-lang.org"]
/// ).unwrap();
///
/// let hostname: &String = m.get_one("hostname").unwrap().unwrap();
/// let hostname: &String = m.get_one("hostname")
/// .expect("matches definition").expect("required");
/// assert_eq!(hostname, "rust-lang.org");
/// ```
impl<P: AnyValueParser + Send + Sync + 'static> From<P> for ValueParser {
@ -278,7 +290,8 @@ impl<P: AnyValueParser + Send + Sync + 'static> From<P> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
impl From<std::ops::Range<i64>> for ValueParser {
@ -305,7 +318,8 @@ impl From<std::ops::Range<i64>> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
impl From<std::ops::RangeInclusive<i64>> for ValueParser {
@ -332,7 +346,8 @@ impl From<std::ops::RangeInclusive<i64>> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
impl From<std::ops::RangeFrom<i64>> for ValueParser {
@ -359,7 +374,8 @@ impl From<std::ops::RangeFrom<i64>> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "80"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 80);
/// ```
impl From<std::ops::RangeTo<i64>> for ValueParser {
@ -386,7 +402,8 @@ impl From<std::ops::RangeTo<i64>> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "80"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 80);
/// ```
impl From<std::ops::RangeToInclusive<i64>> for ValueParser {
@ -413,7 +430,8 @@ impl From<std::ops::RangeToInclusive<i64>> for ValueParser {
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: i64 = *m.get_one("port").unwrap().unwrap();
/// let port: i64 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
impl From<std::ops::RangeFull> for ValueParser {
@ -443,7 +461,8 @@ impl From<std::ops::RangeFull> for ValueParser {
/// ["cmd", "--color", "never"]
/// ).unwrap();
///
/// let color: &String = m.get_one("color").unwrap().unwrap();
/// let color: &String = m.get_one("color")
/// .expect("matches definition").expect("default");
/// assert_eq!(color, "never");
/// ```
impl<P, const C: usize> From<[P; C]> for ValueParser
@ -775,10 +794,12 @@ impl TypedValueParser for PathBufValueParser {
/// .arg(
/// clap::Arg::new("color")
/// .value_parser(clap::builder::ArgEnumValueParser::<ColorChoice>::new())
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "always"]).unwrap();
/// let port: ColorChoice = *m.get_one("color").unwrap().unwrap();
/// let port: ColorChoice = *m.get_one("color")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, ColorChoice::Always);
///
/// // Semantics
@ -884,10 +905,12 @@ impl<E: crate::ArgEnum + Clone + Send + Sync + 'static> Default for ArgEnumValue
/// .arg(
/// clap::Arg::new("color")
/// .value_parser(clap::builder::PossibleValuesParser::new(["always", "auto", "never"]))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "always"]).unwrap();
/// let port: &String = m.get_one("color").unwrap().unwrap();
/// let port: &String = m.get_one("color")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, "always");
/// ```
///
@ -994,7 +1017,8 @@ where
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: u16 = *m.get_one("port").unwrap().unwrap();
/// let port: u16 = *m.get_one("port")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, 3001);
/// ```
///
@ -1245,10 +1269,12 @@ impl TypedValueParser for BoolValueParser {
/// .arg(
/// clap::Arg::new("append")
/// .value_parser(clap::builder::FalseyValueParser::new())
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "true"]).unwrap();
/// let port: bool = *m.get_one("append").unwrap().unwrap();
/// let port: bool = *m.get_one("append")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, true);
/// ```
///
@ -1330,10 +1356,12 @@ impl TypedValueParser for FalseyValueParser {
/// .arg(
/// clap::Arg::new("append")
/// .value_parser(clap::builder::BoolishValueParser::new())
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "true"]).unwrap();
/// let port: bool = *m.get_one("append").unwrap().unwrap();
/// let port: bool = *m.get_one("append")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, true);
/// ```
///
@ -1420,10 +1448,12 @@ impl TypedValueParser for BoolishValueParser {
/// .arg(
/// clap::Arg::new("append")
/// .value_parser(clap::builder::NonEmptyStringValueParser::new())
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "true"]).unwrap();
/// let port: &String = m.get_one("append").unwrap().unwrap();
/// let port: &String = m.get_one("append")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, "true");
/// ```
///
@ -1616,10 +1646,12 @@ pub mod via_prelude {
/// .arg(
/// clap::Arg::new("output")
/// .value_parser(clap::value_parser!(PathBuf))
/// .required(true)
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "file.txt"]).unwrap();
/// let port: &PathBuf = m.get_one("output").unwrap().unwrap();
/// let port: &PathBuf = m.get_one("output")
/// .expect("matches definition").expect("required");
/// assert_eq!(port, Path::new("file.txt"));
/// ```
///