From baed30b702d5606fcd00a188ca20da207f668b1d Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 10 Dec 2018 18:13:55 +0100 Subject: [PATCH] cli/command.rs: implement prefix match --- debian/install | 1 + src/bin/{pbs-datastore.rs => pbs.rs} | 0 src/cli/command.rs | 28 ++++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) rename src/bin/{pbs-datastore.rs => pbs.rs} (100%) diff --git a/debian/install b/debian/install index ed2f20ae..0a0f2cd0 100644 --- a/debian/install +++ b/debian/install @@ -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/ \ No newline at end of file diff --git a/src/bin/pbs-datastore.rs b/src/bin/pbs.rs similarity index 100% rename from src/bin/pbs-datastore.rs rename to src/bin/pbs.rs diff --git a/src/cli/command.rs b/src/cli/command.rs index dc8d2158..e08d6855 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -27,6 +27,28 @@ fn handle_simple_command(cli_cmd: &CliCommand, args: Vec) -> Result<(), Ok(()) } +fn find_command<'a>(def: &'a HashMap, 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, mut args: Vec) -> Result<(), Error> { if args.len() < 1 { @@ -44,11 +66,9 @@ fn handle_nested_command(def: &HashMap, 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 {