]> git.proxmox.com Git - proxmox-backup.git/blob - examples/completion.rs
www: use TFA widgets from widget toolkit
[proxmox-backup.git] / examples / completion.rs
1 use anyhow::{Error};
2
3 use proxmox_schema::*;
4 use proxmox_router::cli::*;
5
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
19 fn echo_command(
20 text: String,
21 ) -> Result<(), Error> {
22 println!("{}", text);
23 Ok(())
24 }
25
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
40 fn 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(())
50 }
51
52 #[api(input: { properties: {} })]
53 /// Quit command. Exit the program.
54 ///
55 /// Returns: nothing
56 fn quit_command() -> Result<(), Error> {
57
58 println!("Goodbye.");
59
60 std::process::exit(0);
61 }
62
63 fn cli_definition() -> CommandLineInterface {
64 let cmd_def = CliCommandMap::new()
65 .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
66 .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
67 .insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND)
68 .arg_param(&["text"])
69 )
70 .insert_help();
71
72 CommandLineInterface::Nested(cmd_def)
73 }
74
75 fn main() -> Result<(), Error> {
76
77 let helper = CliHelper::new(cli_definition());
78
79 let mut rl = rustyline::Editor::<CliHelper>::new();
80 rl.set_helper(Some(helper));
81
82 while let Ok(line) = rl.readline("# prompt: ") {
83 let helper = rl.helper().unwrap();
84
85 let args = shellword_split(&line)?;
86
87 let rpcenv = CliEnvironment::new();
88 let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
89
90 rl.add_history_entry(line);
91 }
92
93 Ok(())
94 }