]> git.proxmox.com Git - proxmox-backup.git/blame - examples/completion.rs
proxmox-rrd: split out load_rrd (cleanup)
[proxmox-backup.git] / examples / completion.rs
CommitLineData
f7d4e4b5 1use anyhow::{Error};
ca60ac13 2
6ef1b649
WB
3use proxmox_schema::*;
4use proxmox_router::cli::*;
ca60ac13 5
e8e9bae4
DM
6#[api(
7 input: {
8 properties: {
9 text: {
10 type: String,
11 description: "Some text.",
12 }
13 }
14 },
15)]
16/// Echo command. Print the passed text.
17///
18/// Returns: nothing
19fn echo_command(
20 text: String,
21) -> Result<(), Error> {
22 println!("{}", text);
23 Ok(())
24}
25
19d7a592
DM
26#[api(
27 input: {
28 properties: {
29 verbose: {
30 type: Boolean,
31 optional: true,
32 description: "Verbose output.",
33 }
34 }
35 },
36)]
37/// Hello command.
38///
39/// Returns: nothing
40fn hello_command(
41 verbose: Option<bool>,
42) -> Result<(), Error> {
43 if verbose.unwrap_or(false) {
44 println!("Hello, how are you!");
45 } else {
46 println!("Hello!");
47 }
48
49 Ok(())
ca60ac13
DM
50}
51
19d7a592 52#[api(input: { properties: {} })]
add5861e 53/// Quit command. Exit the program.
19d7a592
DM
54///
55/// Returns: nothing
56fn quit_command() -> Result<(), Error> {
57
58 println!("Goodbye.");
59
60 std::process::exit(0);
61}
62
63fn cli_definition() -> CommandLineInterface {
ca60ac13 64 let cmd_def = CliCommandMap::new()
48ef3c33
DM
65 .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
66 .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
e8e9bae4 67 .insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND)
48ef3c33
DM
68 .arg_param(&["text"])
69 )
2b691daf 70 .insert_help();
ca60ac13 71
19d7a592 72 CommandLineInterface::Nested(cmd_def)
ca60ac13
DM
73}
74
ca60ac13
DM
75fn main() -> Result<(), Error> {
76
19d7a592 77 let helper = CliHelper::new(cli_definition());
ca60ac13 78
19d7a592 79 let mut rl = rustyline::Editor::<CliHelper>::new();
ca60ac13
DM
80 rl.set_helper(Some(helper));
81
82 while let Ok(line) = rl.readline("# prompt: ") {
83 let helper = rl.helper().unwrap();
ca60ac13 84
2b691daf 85 let args = shellword_split(&line)?;
1201abcf 86
7b22acd0
DM
87 let rpcenv = CliEnvironment::new();
88 let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
7afc1af7
DM
89
90 rl.add_history_entry(line);
ca60ac13
DM
91 }
92
93 Ok(())
94}