Skip to content

Commit

Permalink
fix(derive): Switch default actions/parsers for unstable-v4
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 13, 2022
1 parent 8b4141c commit e8358e5
Show file tree
Hide file tree
Showing 27 changed files with 181 additions and 231 deletions.
31 changes: 27 additions & 4 deletions clap_derive/src/attrs.rs
Expand Up @@ -784,9 +784,18 @@ impl Attrs {
.unwrap_or_else(|| {
if let Some(action) = self.action.as_ref() {
let inner_type = inner_type(field_type);
default_value_parser(inner_type, action.span())
} else {
let span = action.span();
default_value_parser(inner_type, span)
} else if !self.ignore_parser() || cfg!(not(feature = "unstable-v4")) {
self.parser(field_type).value_parser()
} else {
let inner_type = inner_type(field_type);
let span = self
.action
.as_ref()
.map(|a| a.span())
.unwrap_or_else(|| self.kind.span());
default_value_parser(inner_type, span)
}
})
}
Expand All @@ -797,13 +806,27 @@ impl Attrs {
.map(|p| p.resolve(field_type))
.unwrap_or_else(|| {
if let Some(value_parser) = self.value_parser.as_ref() {
default_action(field_type, value_parser.span())
} else {
let span = value_parser.span();
default_action(field_type, span)
} else if !self.ignore_parser() || cfg!(not(feature = "unstable-v4")) {
self.parser(field_type).action()
} else {
let span = self
.value_parser
.as_ref()
.map(|a| a.span())
.unwrap_or_else(|| self.kind.span());
default_action(field_type, span)
}
})
}

#[cfg(feature = "unstable-v4")]
pub fn ignore_parser(&self) -> bool {
self.parser.is_none()
}

#[cfg(not(feature = "unstable-v4"))]
pub fn ignore_parser(&self) -> bool {
self.value_parser.is_some() || self.action.is_some()
}
Expand Down
8 changes: 2 additions & 6 deletions tests/derive/next/arguments.rs
Expand Up @@ -19,7 +19,6 @@ use clap::Parser;
fn required_argument() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser)]
arg: i32,
}
assert_eq!(
Expand All @@ -34,7 +33,7 @@ fn required_argument() {
fn argument_with_default() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, default_value = "42")]
#[clap(default_value = "42")]
arg: i32,
}
assert_eq!(
Expand All @@ -49,7 +48,6 @@ fn argument_with_default() {
fn auto_value_name() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser)]
my_special_arg: i32,
}

Expand All @@ -69,7 +67,7 @@ fn auto_value_name() {
fn explicit_value_name() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, value_name = "BROWNIE_POINTS")]
#[clap(value_name = "BROWNIE_POINTS")]
my_special_arg: i32,
}

Expand All @@ -90,7 +88,6 @@ fn explicit_value_name() {
fn option_type_is_optional() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser)]
arg: Option<i32>,
}
assert_eq!(
Expand All @@ -105,7 +102,6 @@ fn option_type_is_optional() {
fn vec_type_is_multiple_values() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser)]
arg: Vec<i32>,
}
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions tests/derive/next/basic.rs
Expand Up @@ -18,7 +18,7 @@ use clap::Parser;
fn basic() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short = 'a', long = "arg", value_parser)]
#[clap(short = 'a', long = "arg")]
arg: i32,
}
assert_eq!(
Expand All @@ -31,7 +31,7 @@ fn basic() {
fn update_basic() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short = 'a', long = "arg", value_parser)]
#[clap(short = 'a', long = "arg")]
single_value: i32,
}

Expand Down
1 change: 0 additions & 1 deletion tests/derive/next/boxed.rs
Expand Up @@ -16,7 +16,6 @@ enum Sub {

#[derive(Args, PartialEq, Debug)]
struct Ext {
#[clap(value_parser)]
arg: u32,
}

Expand Down
14 changes: 7 additions & 7 deletions tests/derive/next/custom_string_parsers.rs
Expand Up @@ -22,19 +22,19 @@ use std::path::PathBuf;

#[derive(Parser, PartialEq, Debug)]
struct PathOpt {
#[clap(short, long, value_parser)]
#[clap(short, long)]
path: PathBuf,

#[clap(short, default_value = "../", value_parser)]
#[clap(short, default_value = "../")]
default_path: PathBuf,

#[clap(short, value_parser, multiple_occurrences(true))]
#[clap(short, multiple_occurrences(true))]
vector_path: Vec<PathBuf>,

#[clap(short, value_parser)]
#[clap(short)]
option_path_1: Option<PathBuf>,

#[clap(short = 'q', value_parser)]
#[clap(short = 'q')]
option_path_2: Option<PathBuf>,
}

Expand Down Expand Up @@ -135,10 +135,10 @@ struct DefaultedOpt {
#[clap(short, parse(from_str))]
bytes: Bytes,

#[clap(short, value_parser)]
#[clap(short)]
integer: u64,

#[clap(short, value_parser)]
#[clap(short)]
path: PathBuf,
}

Expand Down
8 changes: 4 additions & 4 deletions tests/derive/next/default_value.rs
Expand Up @@ -6,7 +6,7 @@ use crate::utils;
fn default_value() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, default_value = "3")]
#[clap(default_value = "3")]
arg: i32,
}
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
Expand All @@ -20,7 +20,7 @@ fn default_value() {
fn default_value_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, default_value_t = 3)]
#[clap(default_value_t = 3)]
arg: i32,
}
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
Expand All @@ -34,7 +34,7 @@ fn default_value_t() {
fn auto_default_value_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, default_value_t)]
#[clap(default_value_t)]
arg: i32,
}
assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(&["test"]).unwrap());
Expand All @@ -50,7 +50,7 @@ fn detect_os_variant() {

#[derive(clap::Parser)]
pub struct Options {
#[clap(value_parser, default_value_os = ("123".as_ref()))]
#[clap(default_value_os = ("123".as_ref()))]
x: String,
}
Options::command().debug_assert();
Expand Down
28 changes: 10 additions & 18 deletions tests/derive/next/doc_comments_help.rs
Expand Up @@ -23,7 +23,7 @@ fn doc_comments() {
struct LoremIpsum {
/// Fooify a bar
/// and a baz
#[clap(short, long, action)]
#[clap(short, long)]
foo: bool,
}

Expand All @@ -39,12 +39,7 @@ fn help_is_better_than_comments() {
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
struct LoremIpsum {
/// Fooify a bar
#[clap(
short,
long,
help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES",
action
)]
#[clap(short, long, help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES")]
foo: bool,
}

Expand Down Expand Up @@ -76,11 +71,11 @@ fn field_long_doc_comment_both_help_long_help() {
/// Dot is removed from multiline comments.
///
/// Long help
#[clap(long, action)]
#[clap(long)]
foo: bool,

/// Dot is removed from one short comment.
#[clap(long, action)]
#[clap(long)]
bar: bool,
}

Expand Down Expand Up @@ -111,7 +106,7 @@ fn top_long_doc_comment_both_help_long_help() {
///
/// Or something else
Foo {
#[clap(value_parser, help = "foo")]
#[clap(help = "foo")]
bars: String,
},
}
Expand Down Expand Up @@ -146,7 +141,7 @@ fn verbatim_doc_comment() {
#[derive(Parser, Debug)]
#[clap(verbatim_doc_comment)]
struct SeeFigure1 {
#[clap(long, action)]
#[clap(long)]
foo: bool,
}

Expand Down Expand Up @@ -176,10 +171,10 @@ fn verbatim_doc_comment_field() {
#[derive(Parser, Debug)]
struct Command {
/// This help ends in a period.
#[clap(long, verbatim_doc_comment, action)]
#[clap(long, verbatim_doc_comment)]
foo: bool,
/// This help does not end in a period.
#[clap(long, action)]
#[clap(long)]
bar: bool,
}

Expand All @@ -196,7 +191,7 @@ fn multiline_separates_default() {
/// Multiline
///
/// Doc comment
#[clap(long, default_value = "x", value_parser)]
#[clap(long, default_value = "x")]
x: String,
}

Expand Down Expand Up @@ -234,10 +229,7 @@ fn doc_comment_about_handles_both_abouts() {
/// Sub doc comment body
#[derive(Parser, PartialEq, Eq, Debug)]
pub enum Sub {
Compress {
#[clap(value_parser)]
output: String,
},
Compress { output: String },
}

let cmd = Opts::command();
Expand Down
4 changes: 2 additions & 2 deletions tests/derive/next/explicit_name_no_renaming.rs
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
fn explicit_short_long_no_rename() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short = '.', long = ".foo", value_parser)]
#[clap(short = '.', long = ".foo")]
foo: String,
}

Expand All @@ -27,7 +27,7 @@ fn explicit_short_long_no_rename() {
fn explicit_name_no_rename() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(name = ".options", value_parser)]
#[clap(name = ".options")]
foo: String,
}

Expand Down
5 changes: 2 additions & 3 deletions tests/derive/next/flags.rs
Expand Up @@ -20,7 +20,7 @@ use clap::Parser;
fn bool_type_is_flag() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short, long, action)]
#[clap(short, long)]
alice: bool,
}

Expand Down Expand Up @@ -113,7 +113,7 @@ fn non_bool_type_flag() {
fn mixed_type_flags() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short, long, action)]
#[clap(short, long)]
alice: bool,
#[clap(short, long, action = clap::ArgAction::Count)]
bob: u8,
Expand Down Expand Up @@ -181,7 +181,6 @@ fn ignore_qualified_bool_type() {

#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(action)]
arg: inner::bool,
}

Expand Down

0 comments on commit e8358e5

Please sign in to comment.