From 4f8de64b36e5729ffd84f827c2db148f06378c81 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 12 Nov 2023 06:04:34 -0800 Subject: [PATCH] fix: re-implement help trigger parsing for syn 2 --- argh/tests/lib.rs | 2 +- .../positional-and-greedy.stderr | 16 ++++++++----- argh_derive/src/parse_attrs.rs | 23 +++++++++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/argh/tests/lib.rs b/argh/tests/lib.rs index de2c63c..d6a2fef 100644 --- a/argh/tests/lib.rs +++ b/argh/tests/lib.rs @@ -345,7 +345,7 @@ A \description: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\ Options: --s a \description: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\ - --help display usage information + --help, help display usage information "###, ); } diff --git a/argh/tests/ui/conflicting-tails/positional-and-greedy.stderr b/argh/tests/ui/conflicting-tails/positional-and-greedy.stderr index f6a6399..31d311d 100644 --- a/argh/tests/ui/conflicting-tails/positional-and-greedy.stderr +++ b/argh/tests/ui/conflicting-tails/positional-and-greedy.stderr @@ -1,11 +1,15 @@ error: Only the last positional argument may be `Option`, `Vec`, or defaulted. --> tests/ui/conflicting-tails/positional-and-greedy.rs:4:5 | -4 | #[argh(positional)] - | ^ +4 | / #[argh(positional)] +5 | | /// positional +6 | | positional: Vec, + | |___________________________^ error: Later positional argument declared here. - --> tests/ui/conflicting-tails/positional-and-greedy.rs:8:5 - | -8 | #[argh(positional, greedy)] - | ^ + --> tests/ui/conflicting-tails/positional-and-greedy.rs:8:5 + | +8 | / #[argh(positional, greedy)] +9 | | /// remainder +10 | | remainder: Vec, + | |__________________________^ diff --git a/argh_derive/src/parse_attrs.rs b/argh_derive/src/parse_attrs.rs index 460e3e0..39d2fba 100644 --- a/argh_derive/src/parse_attrs.rs +++ b/argh_derive/src/parse_attrs.rs @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +use syn::{parse::Parser, punctuated::Punctuated}; + use { crate::errors::Errors, proc_macro2::Span, @@ -320,7 +322,7 @@ impl TypeAttrs { this.parse_attr_subcommand(errors, ident); } } else if name.is_ident("help_triggers") { - if let Some(m) = errors.expect_meta_list(meta) { + if let Some(m) = errors.expect_meta_list(&meta) { Self::parse_help_triggers(m, errors, &mut this); } } else { @@ -412,14 +414,21 @@ impl TypeAttrs { } } - // get the list of arguments that trigger printing of the help message as a vector of strings (help_arguments = ["-h", "--help", "help"]) + // get the list of arguments that trigger printing of the help message as a vector of strings (help_arguments("-h", "--help", "help")) fn parse_help_triggers(m: &syn::MetaList, errors: &Errors, this: &mut TypeAttrs) { - for nested in &m.nested { - if let Some(lit_str) = - errors.expect_nested_lit(nested).and_then(|l| errors.expect_lit_str(l)) - { - this.help_triggers.get_or_insert_with(Vec::new).push(lit_str.clone()); + let parser = Punctuated::::parse_terminated; + match parser.parse(m.tokens.clone().into()) { + Ok(args) => { + let mut triggers = Vec::new(); + for arg in args { + if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit_str), .. }) = arg { + triggers.push(lit_str); + } + } + + this.help_triggers = Some(triggers); } + Err(err) => errors.push(err), } } }