]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/completion.rs
use rustyline to replace readline
[proxmox-backup.git] / src / bin / completion.rs
CommitLineData
ca60ac13
DM
1use failure::*;
2use serde_json::Value;
3
4use proxmox::{sortable, identity};
5use proxmox::api::*;
6use proxmox::api::schema::*;
7
8use proxmox_backup::cli::*;
9
10#[sortable]
11const API_METHOD_TEST_COMMAND: ApiMethod = ApiMethod::new(
12 &ApiHandler::Sync(&test_command),
13 &ObjectSchema::new(
14 "Test command.",
15 &sorted!([
16 ( "verbose", true, &BooleanSchema::new("Verbose output.").schema() ),
17 ])
18 )
19);
20
21fn test_command(
22 _param: Value,
23 _info: &ApiMethod,
24 _rpcenv: &mut dyn RpcEnvironment,
25) -> Result<Value, Error> {
26
27
28 Ok(Value::Null)
29}
30
31fn command_map() -> CliCommandMap {
32 let cmd_def = CliCommandMap::new()
33 .insert("ls", CliCommand::new(&API_METHOD_TEST_COMMAND).into())
34 .insert("test", CliCommand::new(&API_METHOD_TEST_COMMAND).into())
35 .insert("help", help_command_def().into());
36
37 cmd_def
38}
39
40
41fn main() -> Result<(), Error> {
42
43 let def = CommandLineInterface::Nested(command_map());
44
45 let helper = CliHelper::new(def);
46
47 let config = rustyline::config::Builder::new()
48 //.completion_type(rustyline::config::CompletionType::List)
49 //.completion_prompt_limit(0)
50 .build();
51
52 let mut rl = rustyline::Editor::<CliHelper>::with_config(config);
53 rl.set_helper(Some(helper));
54
55 while let Ok(line) = rl.readline("# prompt: ") {
56 let helper = rl.helper().unwrap();
57 // readline already handles tabs, so here we only split on spaces
58 let args = shellword_split(&line)?;
59
60 let def = helper.cmd_def();
61 let _ = match def {
62 CommandLineInterface::Simple(ref cli_cmd) => {
63 handle_simple_command(def, "", &cli_cmd, args)
64 }
65 CommandLineInterface::Nested(ref map) => {
66 handle_nested_command(def, "", &map, args)
67 }
68 };
69 }
70
71 Ok(())
72}