2020-04-17 15:11:25 +03:00
use anyhow ::{ Error } ;
2019-11-29 13:58:01 +03:00
2019-12-02 13:56:29 +03:00
use proxmox ::api ::{ * , cli ::* } ;
2019-11-29 13:58:01 +03:00
2019-12-02 13:04:40 +03: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 12:57:19 +03: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 13:58:01 +03:00
}
2019-12-02 12:57:19 +03: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 13:58:01 +03:00
let cmd_def = CliCommandMap ::new ( )
2019-12-09 19:40:34 +03:00
. insert ( " quit " , CliCommand ::new ( & API_METHOD_QUIT_COMMAND ) )
. insert ( " hello " , CliCommand ::new ( & API_METHOD_HELLO_COMMAND ) )
2019-12-02 13:04:40 +03:00
. insert ( " echo " , CliCommand ::new ( & API_METHOD_ECHO_COMMAND )
2019-12-09 19:40:34 +03:00
. arg_param ( & [ " text " ] )
)
2019-11-30 16:56:31 +03:00
. insert_help ( ) ;
2019-11-29 13:58:01 +03:00
2019-12-02 12:57:19 +03:00
CommandLineInterface ::Nested ( cmd_def )
2019-11-29 13:58:01 +03:00
}
fn main ( ) -> Result < ( ) , Error > {
2019-12-02 12:57:19 +03:00
let helper = CliHelper ::new ( cli_definition ( ) ) ;
2019-11-29 13:58:01 +03:00
2019-12-02 12:57:19 +03:00
let mut rl = rustyline ::Editor ::< CliHelper > ::new ( ) ;
2019-11-29 13:58:01 +03:00
rl . set_helper ( Some ( helper ) ) ;
while let Ok ( line ) = rl . readline ( " # prompt: " ) {
let helper = rl . helper ( ) . unwrap ( ) ;
2019-11-30 16:56:31 +03:00
let args = shellword_split ( & line ) ? ;
2019-11-30 14:57:02 +03:00
2020-01-27 20:08:43 +03:00
let _ = handle_command ( helper . cmd_def ( ) , " " , args , None ) ;
2019-11-30 17:08:37 +03:00
rl . add_history_entry ( line ) ;
2019-11-29 13:58:01 +03:00
}
Ok ( ( ) )
}