Skip to content

Commit

Permalink
validate implicit flag names
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad authored and sadmac7000 committed Nov 10, 2022
1 parent 5b6148d commit 728d143
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
15 changes: 15 additions & 0 deletions argh/tests/ui/bad-long-names/bad-long-names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Command
#[derive(argh::FromArgs)]
struct Cmd {
#[argh(switch)]
/// non-ascii
привет: bool,
#[argh(switch)]
/// uppercase
XMLHTTPRequest: bool,
#[argh(switch, long = "not really")]
/// bad attr
ok: bool,
}

fn main() {}
17 changes: 17 additions & 0 deletions argh/tests/ui/bad-long-names/bad-long-names.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: Long names must be ASCII
--> tests/ui/bad-long-names/bad-long-names.rs:6:5
|
6 | привет: bool,
| ^^^^^^

error: Long names must be lowercase
--> tests/ui/bad-long-names/bad-long-names.rs:9:5
|
9 | XMLHTTPRequest: bool,
| ^^^^^^^^^^^^^^

error: Long names must be lowercase
--> tests/ui/bad-long-names/bad-long-names.rs:10:27
|
10 | #[argh(switch, long = "not really")]
| ^^^^^^^^^^^^
12 changes: 6 additions & 6 deletions argh_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate proc_macro;
use {
crate::{
errors::Errors,
parse_attrs::{FieldAttrs, FieldKind, TypeAttrs},
parse_attrs::{check_long_name, FieldAttrs, FieldKind, TypeAttrs},
},
proc_macro2::{Span, TokenStream},
quote::{quote, quote_spanned, ToTokens},
Expand Down Expand Up @@ -178,11 +178,11 @@ impl<'a> StructField<'a> {
// Defaults to the kebab-case'd field name if `#[argh(long = "...")]` is omitted.
let long_name = match kind {
FieldKind::Switch | FieldKind::Option => {
let long_name = attrs
.long
.as_ref()
.map(syn::LitStr::value)
.unwrap_or_else(|| to_kebab_case(&name.to_string()));
let long_name = attrs.long.as_ref().map(syn::LitStr::value).unwrap_or_else(|| {
let kebab_name = to_kebab_case(&name.to_string());
check_long_name(errors, name, &kebab_name);
kebab_name
});
if long_name == "help" {
errors.err(field, "Custom `--help` flags are not supported.");
}
Expand Down
16 changes: 10 additions & 6 deletions argh_derive/src/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ impl FieldAttrs {
parse_attr_single_string(errors, m, "long", &mut self.long);
let long = self.long.as_ref().unwrap();
let value = long.value();
if !value.is_ascii() {
errors.err(long, "Long names must be ASCII");
}
if !value.chars().all(|c| c.is_lowercase() || c == '-' || c.is_ascii_digit()) {
errors.err(long, "Long names must be lowercase");
}
check_long_name(errors, long, &value);
}

fn parse_attr_short(&mut self, errors: &Errors, m: &syn::MetaNameValue) {
Expand All @@ -203,6 +198,15 @@ impl FieldAttrs {
}
}

pub(crate) fn check_long_name(errors: &Errors, spanned: &impl syn::spanned::Spanned, value: &str) {
if !value.is_ascii() {
errors.err(spanned, "Long names must be ASCII");
}
if !value.chars().all(|c| c.is_lowercase() || c == '-' || c.is_ascii_digit()) {
errors.err(spanned, "Long names must be lowercase");
}
}

fn parse_attr_fn_name(
errors: &Errors,
m: &syn::MetaList,
Expand Down

0 comments on commit 728d143

Please sign in to comment.