2022-02-21 11:26:41 +02:00
// This example shows additional functionality of the "interactive-clap" macro for parsing command line data into a structure using the context attributes of the macro.
2022-02-20 21:14:24 +02:00
// 1) build an example: cargo build --example struct_with_context
// 2) go to the `examples` folder: cd target/debug/examples
// 3) run an example: ./struct_with_context (without parameters) => entered interactive mode
// ./struct_with_context account QWERTY => offline_args: Ok(OfflineArgs { account: Sender { sender_account_id: "QWERTY" } })
// To learn more about the parameters, use "help" flag: ./struct_with_context --help
2023-04-02 22:24:06 +03:00
use interactive_clap ::{ ResultFromCli , ToCliArgs } ;
2022-12-13 17:47:50 +02:00
2023-04-02 22:24:06 +03:00
mod simple_enum ;
2022-02-18 16:26:50 +02:00
2023-06-02 22:10:37 +02:00
#[ derive(Debug, Clone) ]
pub enum ConnectionConfig {
Testnet ,
Mainnet ,
Betanet ,
}
2023-04-02 22:24:06 +03:00
#[ derive(Debug, Clone, interactive_clap::InteractiveClap) ]
2022-02-06 16:01:58 +02:00
#[ interactive_clap(input_context = ()) ]
2023-04-02 22:24:06 +03:00
#[ interactive_clap(output_context = OfflineArgsContext) ]
2022-02-06 16:01:58 +02:00
pub struct OfflineArgs {
#[ interactive_clap(named_arg) ]
///Specify a sender
2023-04-02 22:24:06 +03:00
sender : Sender ,
2022-02-06 16:01:58 +02:00
}
2022-11-20 12:30:48 +01:00
#[ derive(Debug) ]
pub struct OfflineArgsContext {
pub some_context_field : i64 ,
}
2022-02-06 16:01:58 +02:00
impl OfflineArgsContext {
fn from_previous_context (
_previous_context : ( ) ,
2023-04-02 22:24:06 +03:00
_scope : & < OfflineArgs as interactive_clap ::ToInteractiveClapContextScope > ::InteractiveClapContextScope ,
2023-02-24 09:55:16 +02:00
) -> color_eyre ::eyre ::Result < Self > {
Ok ( Self {
2022-11-20 12:30:48 +01:00
some_context_field : 42 ,
2023-02-24 09:55:16 +02:00
} )
2022-02-06 16:01:58 +02:00
}
}
impl From < OfflineArgsContext > for NetworkContext {
fn from ( _ : OfflineArgsContext ) -> Self {
Self {
connection_config : None ,
}
}
}
2023-04-02 22:24:06 +03:00
impl From < ( ) > for NetworkContext {
fn from ( _ : ( ) ) -> Self {
Self {
connection_config : None ,
}
}
}
impl From < NetworkContext > for ( ) {
2025-01-07 20:32:49 +02:00
fn from ( _ : NetworkContext ) -> Self { }
2023-04-02 22:24:06 +03:00
}
2023-02-24 09:55:16 +02:00
#[ derive(Debug) ]
2022-02-06 16:01:58 +02:00
pub struct NetworkContext {
2023-06-02 22:10:37 +02:00
pub connection_config : Option < ConnectionConfig > ,
2022-02-06 16:01:58 +02:00
}
2023-04-02 22:24:06 +03:00
#[ derive(Debug, Clone, interactive_clap::InteractiveClap) ]
2023-02-24 09:55:16 +02:00
#[ interactive_clap(context = NetworkContext) ]
2022-02-06 16:01:58 +02:00
pub struct Sender {
2022-02-18 16:26:50 +02:00
#[ interactive_clap(skip_default_input_arg) ]
2023-04-02 22:24:06 +03:00
sender_account_id : String ,
#[ interactive_clap(subcommand) ]
network : simple_enum ::Mode ,
2022-02-06 16:01:58 +02:00
}
impl Sender {
2023-04-02 22:24:06 +03:00
fn input_sender_account_id (
context : & NetworkContext ,
) -> color_eyre ::eyre ::Result < Option < String > > {
2022-11-20 12:30:48 +01:00
println! ( " Let's use context: {:?} " , context ) ;
2023-04-02 22:24:06 +03:00
match inquire ::CustomType ::new ( " What is the account ID? " ) . prompt ( ) {
Ok ( value ) = > Ok ( Some ( value ) ) ,
Err (
inquire ::error ::InquireError ::OperationCanceled
| inquire ::error ::InquireError ::OperationInterrupted ,
) = > Ok ( None ) ,
Err ( err ) = > Err ( err . into ( ) ) ,
}
2022-02-06 16:01:58 +02:00
}
}
2023-04-02 22:24:06 +03:00
fn main ( ) -> color_eyre ::Result < ( ) > {
let mut cli_offline_args = OfflineArgs ::parse ( ) ;
2022-02-18 16:26:50 +02:00
let context = ( ) ; // #[interactive_clap(input_context = ())]
2023-04-02 22:24:06 +03:00
loop {
let offline_args = < OfflineArgs as interactive_clap ::FromCli > ::from_cli (
Some ( cli_offline_args . clone ( ) ) ,
context ,
) ;
match offline_args {
ResultFromCli ::Ok ( cli_offline_args ) | ResultFromCli ::Cancel ( Some ( cli_offline_args ) ) = > {
println! (
" Your console command: {} " ,
2025-01-07 20:32:49 +02:00
shell_words ::join ( cli_offline_args . to_cli_args ( ) )
2023-04-02 22:24:06 +03:00
) ;
return Ok ( ( ) ) ;
}
ResultFromCli ::Cancel ( None ) = > {
println! ( " Goodbye! " ) ;
return Ok ( ( ) ) ;
}
ResultFromCli ::Back = > {
cli_offline_args = Default ::default ( ) ;
}
ResultFromCli ::Err ( cli_offline_args , err ) = > {
if let Some ( cli_offline_args ) = cli_offline_args {
println! (
" Your console command: {} " ,
2025-01-07 20:32:49 +02:00
shell_words ::join ( cli_offline_args . to_cli_args ( ) )
2023-04-02 22:24:06 +03:00
) ;
}
return Err ( err ) ;
}
}
}
2022-02-06 16:01:58 +02:00
}