diff --git a/rpc-toolkit-macro-internals/src/command/build.rs b/rpc-toolkit-macro-internals/src/command/build.rs index c7a4933..29edfc7 100644 --- a/rpc-toolkit-macro-internals/src/command/build.rs +++ b/rpc-toolkit-macro-internals/src/command/build.rs @@ -98,12 +98,16 @@ fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> Token }) .collect::>(); let required = LitBool::new(subcommand_required, Span::call_site()); + let alias = &opt.common().aliases; quote! { pub fn build_app() -> rpc_toolkit_prelude::App<'static, 'static> { let mut app = rpc_toolkit_prelude::App::new(#name); #( app = app.about(#about); )* + #( + app = app.alias(#alias); + )* #( app = app.arg(#arg); )* diff --git a/rpc-toolkit-macro-internals/src/command/mod.rs b/rpc-toolkit-macro-internals/src/command/mod.rs index 19348b8..84d5cfa 100644 --- a/rpc-toolkit-macro-internals/src/command/mod.rs +++ b/rpc-toolkit-macro-internals/src/command/mod.rs @@ -19,6 +19,7 @@ impl Default for ExecutionContext { pub struct LeafOptions { blocking: Option, is_async: bool, + aliases: Vec, about: Option, rename: Option, exec_ctx: ExecutionContext, diff --git a/rpc-toolkit-macro-internals/src/command/parse.rs b/rpc-toolkit-macro-internals/src/command/parse.rs index 17f2281..9f2083a 100644 --- a/rpc-toolkit-macro-internals/src/command/parse.rs +++ b/rpc-toolkit-macro-internals/src/command/parse.rs @@ -126,6 +126,30 @@ pub fn parse_command_attr(args: AttributeArgs) -> Result { "`display` cannot be assigned to", )); } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("aliases") => { + if !opt.common().aliases.is_empty() { + return Err(Error::new(list.span(), "duplicate argument `alias`")); + } + for nested in list.nested { + match nested { + NestedMeta::Lit(Lit::Str(alias)) => opt.common().aliases.push(alias), + a => return Err(Error::new(a.span(), "`alias` must be a string")), + } + } + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("alias") => { + return Err(Error::new(p.span(), "`alias` requires an argument")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("alias") => { + if !opt.common().aliases.is_empty() { + return Err(Error::new(nv.path.span(), "duplicate argument `alias`")); + } + if let Lit::Str(alias) = nv.lit { + opt.common().aliases.push(alias); + } else { + return Err(Error::new(nv.lit.span(), "`alias` must be a string")); + } + } NestedMeta::Meta(Meta::Path(p)) if p.is_ident("cli_only") => { match &opt.common().exec_ctx { ExecutionContext::Standard => { diff --git a/rpc-toolkit-macro/src/lib.rs b/rpc-toolkit-macro/src/lib.rs index 42b3b0c..6d40b06 100644 --- a/rpc-toolkit-macro/src/lib.rs +++ b/rpc-toolkit-macro/src/lib.rs @@ -10,6 +10,8 @@ pub fn command(args: TokenStream, item: TokenStream) -> TokenStream { /// `#[arg(...)]` -> Take this argument as a parameter /// - `#[arg(help = "Help text")]` -> Set help text for the arg +/// - `#[arg(alias = "ls")]` -> Set the alias `ls` in the CLI +/// - `#[arg(aliases("show", "ls"))]` -> Set the aliases `ls` and `show` in the CLI /// - `#[arg(rename = "new_name")]` -> Set the name of the arg to `new_name` in the RPC and CLI /// - `#[arg(short = "a")]` -> Set the "short" representation of the arg to `-a` on the CLI /// - `#[arg(long = "arg")]` -> Set the "long" representation of the arg to `--arg` on the CLI