2019-11-29 11:58:01 +01:00
use failure ::* ;
use proxmox ::api ::* ;
use proxmox_backup ::cli ::* ;
2019-12-02 10:57:19 +01: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 11:58:01 +01:00
}
2019-12-02 10:57:19 +01:00
#[ api(input: { properties: {} }) ]
/// Quit command. Exit the programm.
///
/// Returns: nothing
fn quit_command ( ) -> Result < ( ) , Error > {
println! ( " Goodbye. " ) ;
std ::process ::exit ( 0 ) ;
}
fn cli_definition ( ) -> CommandLineInterface {
2019-11-29 11:58:01 +01:00
let cmd_def = CliCommandMap ::new ( )
2019-12-02 10:57:19 +01:00
. insert ( " quit " , CliCommand ::new ( & API_METHOD_QUIT_COMMAND ) . into ( ) )
. insert ( " hello " , CliCommand ::new ( & API_METHOD_HELLO_COMMAND ) . into ( ) )
2019-11-30 14:56:31 +01:00
. insert_help ( ) ;
2019-11-29 11:58:01 +01:00
2019-12-02 10:57:19 +01:00
CommandLineInterface ::Nested ( cmd_def )
2019-11-29 11:58:01 +01:00
}
fn main ( ) -> Result < ( ) , Error > {
2019-12-02 10:57:19 +01:00
let helper = CliHelper ::new ( cli_definition ( ) ) ;
2019-11-29 11:58:01 +01:00
2019-12-02 10:57:19 +01:00
let mut rl = rustyline ::Editor ::< CliHelper > ::new ( ) ;
2019-11-29 11:58:01 +01:00
rl . set_helper ( Some ( helper ) ) ;
while let Ok ( line ) = rl . readline ( " # prompt: " ) {
let helper = rl . helper ( ) . unwrap ( ) ;
2019-11-30 14:56:31 +01:00
let args = shellword_split ( & line ) ? ;
2019-11-30 12:57:02 +01:00
2019-11-30 15:08:37 +01:00
let _ = handle_command ( helper . cmd_def ( ) , " " , args ) ;
rl . add_history_entry ( line ) ;
2019-11-29 11:58:01 +01:00
}
Ok ( ( ) )
}