Skip to content
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

v3 fix(derive): Add "id" attribute #4068

Merged
merged 1 commit into from Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion clap_derive/src/attrs.rs
Expand Up @@ -450,7 +450,7 @@ impl Attrs {
}

fn push_method(&mut self, name: Ident, arg: impl ToTokens) {
if name == "name" {
if name == "name" || name == "id" {
self.name = Name::Assigned(quote!(#arg));
} else if name == "value_parser" {
self.value_parser = Some(ValueParser::Explicit(Method::new(name, quote!(#arg))));
Expand Down
4 changes: 3 additions & 1 deletion src/_derive/mod.rs
Expand Up @@ -181,8 +181,10 @@
//! - e.g. `#[clap(max_values(3))]` would translate to `arg.max_values(3)`
//!
//! **Magic attributes**:
//! - `name = <expr>`: [`Arg::id`][crate::Arg::id]
//! - `id = <expr>`: [`Arg::id`][crate::Arg::id]
//! - When not present: case-converted field name is used
//! - `name = <expr>`: [`Arg::id`][crate::Arg::id]
//! - **Deprecated:** use `id`
//! - `value_parser [= <expr>]`: [`Arg::value_parser`][crate::Arg::value_parser]
//! - When not present: will auto-select an implementation based on the field type using
//! [`value_parser!][crate::value_parser!]
Expand Down
56 changes: 56 additions & 0 deletions tests/derive/naming.rs
Expand Up @@ -57,6 +57,34 @@ fn test_standalone_long_ignores_afterwards_defined_custom_name() {
);
}

#[test]
fn test_standalone_long_uses_previous_defined_custom_id() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[clap(id = "foo", long)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "--foo"]).unwrap()
);
}

#[test]
fn test_standalone_long_ignores_afterwards_defined_custom_id() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[clap(long, id = "foo")]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "--foo-option"]).unwrap()
);
}

#[test]
fn test_standalone_short_generates_kebab_case() {
#[derive(Parser, Debug, PartialEq)]
Expand Down Expand Up @@ -114,6 +142,34 @@ fn test_standalone_short_ignores_afterwards_defined_custom_name() {
);
}

#[test]
fn test_standalone_short_uses_previous_defined_custom_id() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[clap(id = "option", short)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-o"]).unwrap()
);
}

#[test]
fn test_standalone_short_ignores_afterwards_defined_custom_id() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[clap(short, id = "option")]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-f"]).unwrap()
);
}

#[test]
fn test_standalone_long_uses_previous_defined_casing() {
#[derive(Parser, Debug, PartialEq)]
Expand Down