2023-06-02 22:10:37 +02:00
#![ allow(dead_code) ]
2022-02-21 11:26:41 +02:00
//This example shows how to parse data from the command line to an enum using the "interactive-clap" macro.
2022-02-20 21:14:24 +02:00
// 1) build an example: cargo build --example simple_enum
// 2) go to the `examples` folder: cd target/debug/examples
// 3) run an example: ./simple_enum (without parameters) => entered interactive mode
// ./simple_enum network => mode: Ok(Network)
// ./simple_enum offline => mode: Ok(Offline)
// To learn more about the parameters, use "help" flag: ./simple_enum --help
2022-02-18 16:26:50 +02:00
2023-04-02 22:24:06 +03:00
use interactive_clap ::{ ResultFromCli , ToCliArgs } ;
2022-11-20 12:30:48 +01:00
use strum ::{ EnumDiscriminants , EnumIter , EnumMessage } ;
2022-02-06 16:01:58 +02:00
2023-04-02 22:24:06 +03:00
#[ derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap) ]
2022-02-06 16:01:58 +02:00
#[ strum_discriminants(derive(EnumMessage, EnumIter)) ]
2022-02-18 16:26:50 +02:00
///To construct a transaction you will need to provide information about sender (signer) and receiver accounts, and actions that needs to be performed.
///Do you want to derive some information required for transaction construction automatically querying it online?
2022-02-06 16:01:58 +02:00
pub enum Mode {
2022-02-18 16:26:50 +02:00
/// Prepare and, optionally, submit a new transaction with online mode
2022-02-06 16:01:58 +02:00
#[ strum_discriminants(strum(message = " Yes, I keep it simple " )) ]
Network ,
2022-02-18 16:26:50 +02:00
/// Prepare and, optionally, submit a new transaction with offline mode
2022-02-06 16:01:58 +02:00
#[ strum_discriminants(strum(
message = " No, I want to work in no-network (air-gapped) environment "
) ) ]
Offline ,
}
2023-04-02 22:24:06 +03:00
fn main ( ) -> color_eyre ::Result < ( ) > {
2022-02-18 16:26:50 +02:00
let cli_mode = Mode ::try_parse ( ) . ok ( ) ;
let context = ( ) ; // default: input_context = ()
2023-04-02 22:24:06 +03:00
loop {
let mode = < Mode as interactive_clap ::FromCli > ::from_cli ( cli_mode . clone ( ) , context ) ;
match mode {
ResultFromCli ::Ok ( cli_mode ) | ResultFromCli ::Cancel ( Some ( cli_mode ) ) = > {
println! (
" Your console command: {} " ,
2025-01-07 20:32:49 +02:00
shell_words ::join ( cli_mode . to_cli_args ( ) )
2023-04-02 22:24:06 +03:00
) ;
return Ok ( ( ) ) ;
}
ResultFromCli ::Cancel ( None ) = > {
println! ( " Goodbye! " ) ;
return Ok ( ( ) ) ;
}
ResultFromCli ::Back = > { }
ResultFromCli ::Err ( optional_cli_mode , err ) = > {
if let Some ( cli_mode ) = optional_cli_mode {
println! (
" Your console command: {} " ,
2025-01-07 20:32:49 +02:00
shell_words ::join ( cli_mode . to_cli_args ( ) )
2023-04-02 22:24:06 +03:00
) ;
}
return Err ( err ) ;
}
}
}
2022-02-06 16:01:58 +02:00
}