mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-12 13:44:32 +00:00
implement cli command alias feature
This commit is contained in:
parent
474fae5b3d
commit
e7cb1f20d0
@ -156,6 +156,8 @@ fn parse_nested_command<'a>(
|
|||||||
|
|
||||||
// Note: Avoid async recursive function, because current rust compiler cant handle that
|
// Note: Avoid async recursive function, because current rust compiler cant handle that
|
||||||
loop {
|
loop {
|
||||||
|
replace_aliases(args, &map.aliases);
|
||||||
|
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
let mut cmds: Vec<&String> = map.commands.keys().collect();
|
let mut cmds: Vec<&String> = map.commands.keys().collect();
|
||||||
cmds.sort();
|
cmds.sort();
|
||||||
@ -173,6 +175,7 @@ fn parse_nested_command<'a>(
|
|||||||
return Err(format_err!("{}", err_msg));
|
return Err(format_err!("{}", err_msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let command = args.remove(0);
|
let command = args.remove(0);
|
||||||
|
|
||||||
let (_, sub_cmd) = match map.find_command(&command) {
|
let (_, sub_cmd) = match map.find_command(&command) {
|
||||||
@ -228,7 +231,7 @@ fn help_command(
|
|||||||
_info: &ApiMethod,
|
_info: &ApiMethod,
|
||||||
_rpcenv: &mut dyn RpcEnvironment,
|
_rpcenv: &mut dyn RpcEnvironment,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
let command: Vec<String> = param["command"]
|
let mut command: Vec<String> = param["command"]
|
||||||
.as_array()
|
.as_array()
|
||||||
.unwrap_or(&Vec::new())
|
.unwrap_or(&Vec::new())
|
||||||
.iter()
|
.iter()
|
||||||
@ -239,6 +242,9 @@ fn help_command(
|
|||||||
|
|
||||||
HELP_CONTEXT.with(|ctx| match &*ctx.borrow() {
|
HELP_CONTEXT.with(|ctx| match &*ctx.borrow() {
|
||||||
Some(def) => {
|
Some(def) => {
|
||||||
|
if let CommandLineInterface::Nested(map) = def.as_ref() {
|
||||||
|
replace_aliases(&mut command, &map.aliases);
|
||||||
|
}
|
||||||
print_help(def, String::from(""), &command, verbose);
|
print_help(def, String::from(""), &command, verbose);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -259,6 +265,26 @@ pub(crate) fn help_command_def() -> CliCommand {
|
|||||||
CliCommand::new(&API_METHOD_COMMAND_HELP).arg_param(&["command"])
|
CliCommand::new(&API_METHOD_COMMAND_HELP).arg_param(&["command"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn replace_aliases(
|
||||||
|
args: &mut Vec<String>,
|
||||||
|
aliases: &Vec<(Vec<&'static str>, Vec<&'static str>)>,
|
||||||
|
) {
|
||||||
|
for (old, new) in aliases {
|
||||||
|
if args.len() < old.len() { continue; }
|
||||||
|
if old[..] == args[..old.len()] {
|
||||||
|
let new_args: Vec<String> = new.iter()
|
||||||
|
.map(|s| String::from(*s)).collect();
|
||||||
|
let rest = args.split_off(old.len());
|
||||||
|
args.truncate(0);
|
||||||
|
args.extend(new_args);
|
||||||
|
for arg in rest.iter() {
|
||||||
|
args.push(arg.clone());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Handle command invocation.
|
/// Handle command invocation.
|
||||||
///
|
///
|
||||||
/// This command gets the command line ``args`` and tries to invoke
|
/// This command gets the command line ``args`` and tries to invoke
|
||||||
|
@ -101,6 +101,7 @@ pub struct CliCommandMap {
|
|||||||
/// Each command has an unique name. The map associates names with
|
/// Each command has an unique name. The map associates names with
|
||||||
/// command definitions.
|
/// command definitions.
|
||||||
pub commands: HashMap<String, CommandLineInterface>,
|
pub commands: HashMap<String, CommandLineInterface>,
|
||||||
|
pub aliases: Vec<(Vec<&'static str>, Vec<&'static str>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CliCommandMap {
|
impl CliCommandMap {
|
||||||
@ -115,6 +116,11 @@ impl CliCommandMap {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn alias(mut self, old: &'static[&'static str], new: &'static[&'static str]) -> Self {
|
||||||
|
self.aliases.push((Vec::from(old), Vec::from(new)));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Insert the help command.
|
/// Insert the help command.
|
||||||
pub fn insert_help(mut self) -> Self {
|
pub fn insert_help(mut self) -> Self {
|
||||||
self.commands
|
self.commands
|
||||||
|
Loading…
Reference in New Issue
Block a user