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

feat(derive): Add support for lower/upper in rename_all #2822

Merged
merged 1 commit into from
Oct 7, 2021
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
10 changes: 10 additions & 0 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub enum CasingStyle {
ScreamingSnake,
/// Keep all letters lowercase and indicate word boundaries with underscores.
Snake,
/// Keep all letters lowercase and remove word boundaries.
Lower,
/// Keep all letters uppercase and remove word boundaries.
Upper,
/// Use the original attribute name defined in the code.
Verbatim,
}
Expand Down Expand Up @@ -206,6 +210,8 @@ impl CasingStyle {
"pascal" | "pascalcase" => cs(Pascal),
"screamingsnake" | "screamingsnakecase" => cs(ScreamingSnake),
"snake" | "snakecase" => cs(Snake),
"lower" | "lowercase" => cs(Lower),
"upper" | "uppercase" => cs(Upper),
"verbatim" | "verbatimcase" => cs(Verbatim),
s => abort!(name, "unsupported casing: `{}`", s),
}
Expand All @@ -226,6 +232,8 @@ impl Name {
Camel => s.to_mixed_case(),
ScreamingSnake => s.to_shouty_snake_case(),
Snake => s.to_snake_case(),
Lower => s.to_snake_case().replace("_", ""),
Upper => s.to_shouty_snake_case().replace("_", ""),
Verbatim => s,
};
quote_spanned!(ident.span()=> #s)
Expand All @@ -246,6 +254,8 @@ impl Name {
Camel => s.to_mixed_case(),
ScreamingSnake => s.to_shouty_snake_case(),
Snake => s.to_snake_case(),
Lower => s.to_snake_case(),
Upper => s.to_shouty_snake_case(),
Verbatim => s,
};

Expand Down
28 changes: 28 additions & 0 deletions clap_derive/tests/argument_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,31 @@ fn test_rename_all_is_propagation_can_be_overridden() {
Opt::parse_from(&["test", "SECOND_VARIANT", "--foo-option"])
);
}

#[test]
fn test_lower_is_renamed() {
#[derive(Clap, Debug, PartialEq)]
struct Opt {
#[clap(rename_all = "lower", long)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::parse_from(&["test", "--foooption"])
);
}

#[test]
fn test_upper_is_renamed() {
#[derive(Clap, Debug, PartialEq)]
struct Opt {
#[clap(rename_all = "upper", long)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::parse_from(&["test", "--FOOOPTION"])
);
}