proxmox-backup/examples/completion.rs

95 lines
1.9 KiB
Rust
Raw Normal View History

use anyhow::{Error};
2019-11-29 10:58:01 +00:00
use proxmox_schema::*;
use proxmox_router::cli::*;
2019-11-29 10:58:01 +00:00
#[api(
input: {
properties: {
text: {
type: String,
description: "Some text.",
}
}
},
)]
/// Echo command. Print the passed text.
///
/// Returns: nothing
fn echo_command(
text: String,
) -> Result<(), Error> {
println!("{}", text);
Ok(())
}
2019-12-02 09:57:19 +00:00
#[api(
input: {
properties: {
verbose: {
type: Boolean,
optional: true,
description: "Verbose output.",
}
}
},
)]
/// Hello command.
///
/// Returns: nothing
fn hello_command(
verbose: Option<bool>,
) -> Result<(), Error> {
if verbose.unwrap_or(false) {
println!("Hello, how are you!");
} else {
println!("Hello!");
}
Ok(())
2019-11-29 10:58:01 +00:00
}
2019-12-02 09:57:19 +00:00
#[api(input: { properties: {} })]
/// Quit command. Exit the program.
2019-12-02 09:57:19 +00:00
///
/// Returns: nothing
fn quit_command() -> Result<(), Error> {
println!("Goodbye.");
std::process::exit(0);
}
fn cli_definition() -> CommandLineInterface {
2019-11-29 10:58:01 +00:00
let cmd_def = CliCommandMap::new()
.insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
.insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
.insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND)
.arg_param(&["text"])
)
2019-11-30 13:56:31 +00:00
.insert_help();
2019-11-29 10:58:01 +00:00
2019-12-02 09:57:19 +00:00
CommandLineInterface::Nested(cmd_def)
2019-11-29 10:58:01 +00:00
}
fn main() -> Result<(), Error> {
2019-12-02 09:57:19 +00:00
let helper = CliHelper::new(cli_definition());
2019-11-29 10:58:01 +00:00
2019-12-02 09:57:19 +00:00
let mut rl = rustyline::Editor::<CliHelper>::new();
2019-11-29 10:58:01 +00:00
rl.set_helper(Some(helper));
while let Ok(line) = rl.readline("# prompt: ") {
let helper = rl.helper().unwrap();
2019-11-30 13:56:31 +00:00
let args = shellword_split(&line)?;
2019-11-30 11:57:02 +00:00
let rpcenv = CliEnvironment::new();
let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
rl.add_history_entry(line);
2019-11-29 10:58:01 +00:00
}
Ok(())
}