proxmox-api/src/cli.rs: simplify cli setup using generics.

This commit is contained in:
Dietmar Maurer 2019-12-09 17:38:18 +01:00
parent c9cec98ece
commit bf7b939bdf
3 changed files with 12 additions and 9 deletions

View File

@ -107,8 +107,12 @@ impl CliCommandMap {
}
/// Insert another command.
pub fn insert<S: Into<String>>(mut self, name: S, cli: CommandLineInterface) -> Self {
self.commands.insert(name.into(), cli);
pub fn insert<C: Into<CommandLineInterface>>(
mut self,
name: &'static str,
cli: C,
) -> Self {
self.commands.insert(name.into(), cli.into());
self
}

View File

@ -211,8 +211,9 @@ pub fn handle_command(
/// - ``bashcomplete``: Output bash completions instead of running the command.
/// - ``printdoc``: Output ReST documentation.
///
pub fn run_cli_command(def: CommandLineInterface) {
let def = match def {
pub fn run_cli_command<C: Into<CommandLineInterface>>(def: C) {
let def = match def.into() {
CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd),
CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help()),
};

View File

@ -328,24 +328,22 @@ mod test {
fn get_complex_test_cmddef() -> CommandLineInterface {
let sub_def = CliCommandMap::new()
.insert("l1c1", CliCommand::new(&API_METHOD_SIMPLE1).into())
.insert("l1c2", CliCommand::new(&API_METHOD_SIMPLE1).into());
.insert("l1c1", CliCommand::new(&API_METHOD_SIMPLE1))
.insert("l1c2", CliCommand::new(&API_METHOD_SIMPLE1));
let cmd_def = CliCommandMap::new()
.insert_help()
.insert("l0sub", CommandLineInterface::Nested(sub_def))
.insert("l0c1", CliCommand::new(&API_METHOD_SIMPLE1).into())
.insert("l0c1", CliCommand::new(&API_METHOD_SIMPLE1))
.insert(
"l0c2",
CliCommand::new(&API_METHOD_SIMPLE1)
.arg_param(&["required-arg"])
.into(),
)
.insert(
"l0c3",
CliCommand::new(&API_METHOD_SIMPLE1)
.arg_param(&["required-arg", "optional-arg"])
.into(),
);
cmd_def.into()