This commit is contained in:
Aiden McClelland
2021-04-12 15:02:02 -06:00
parent cd38fbd58d
commit fba286e7a8
4 changed files with 31 additions and 0 deletions

View File

@@ -98,12 +98,16 @@ fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> Token
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let required = LitBool::new(subcommand_required, Span::call_site()); let required = LitBool::new(subcommand_required, Span::call_site());
let alias = &opt.common().aliases;
quote! { quote! {
pub fn build_app() -> rpc_toolkit_prelude::App<'static, 'static> { pub fn build_app() -> rpc_toolkit_prelude::App<'static, 'static> {
let mut app = rpc_toolkit_prelude::App::new(#name); let mut app = rpc_toolkit_prelude::App::new(#name);
#( #(
app = app.about(#about); app = app.about(#about);
)* )*
#(
app = app.alias(#alias);
)*
#( #(
app = app.arg(#arg); app = app.arg(#arg);
)* )*

View File

@@ -19,6 +19,7 @@ impl Default for ExecutionContext {
pub struct LeafOptions { pub struct LeafOptions {
blocking: Option<Path>, blocking: Option<Path>,
is_async: bool, is_async: bool,
aliases: Vec<LitStr>,
about: Option<LitStr>, about: Option<LitStr>,
rename: Option<Ident>, rename: Option<Ident>,
exec_ctx: ExecutionContext, exec_ctx: ExecutionContext,

View File

@@ -126,6 +126,30 @@ pub fn parse_command_attr(args: AttributeArgs) -> Result<Options> {
"`display` cannot be assigned to", "`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") => { NestedMeta::Meta(Meta::Path(p)) if p.is_ident("cli_only") => {
match &opt.common().exec_ctx { match &opt.common().exec_ctx {
ExecutionContext::Standard => { ExecutionContext::Standard => {

View File

@@ -10,6 +10,8 @@ pub fn command(args: TokenStream, item: TokenStream) -> TokenStream {
/// `#[arg(...)]` -> Take this argument as a parameter /// `#[arg(...)]` -> Take this argument as a parameter
/// - `#[arg(help = "Help text")]` -> Set help text for the arg /// - `#[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(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(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 /// - `#[arg(long = "arg")]` -> Set the "long" representation of the arg to `--arg` on the CLI