cli/command.rs: implement prefix match

This commit is contained in:
Dietmar Maurer 2018-12-10 18:13:55 +01:00
parent 8f62336b0f
commit baed30b702
3 changed files with 25 additions and 4 deletions

1
debian/install vendored
View File

@ -1,4 +1,5 @@
target/release/api-test-server /usr/sbin
target/release/pbs /usr/sbin
www/images/logo-128.png /usr/share/javascript/proxmox-backup-server/images/
www/images/proxmox_logo.png /usr/share/javascript/proxmox-backup-server/images/
www/proxmox-backup-gui.js /usr/share/javascript/proxmox-backup-server/js/

View File

@ -27,6 +27,28 @@ fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<(),
Ok(())
}
fn find_command<'a>(def: &'a HashMap<String, CommandLineInterface>, name: &str) -> Option<&'a CommandLineInterface> {
if let Some(sub_cmd) = def.get(name) {
return Some(sub_cmd);
};
let mut matches: Vec<&str> = vec![];
for cmd in def.keys() {
if cmd.starts_with(name) {
matches.push(cmd); }
}
if matches.len() != 1 { return None; }
if let Some(sub_cmd) = def.get(matches[0]) {
return Some(sub_cmd);
};
None
}
fn handle_nested_command(def: &HashMap<String, CommandLineInterface>, mut args: Vec<String>) -> Result<(), Error> {
if args.len() < 1 {
@ -44,11 +66,9 @@ fn handle_nested_command(def: &HashMap<String, CommandLineInterface>, mut args:
let command = args.remove(0);
let sub_cmd = match def.get(&command) {
let sub_cmd = match find_command(def, &command) {
Some(cmd) => cmd,
None => {
bail!("no such command '{}'", command);
}
None => bail!("no such command '{}'", command),
};
match sub_cmd {