mirror of
https://github.com/Drop-OSS/interactive-clap.git
synced 2026-07-19 20:03:34 -04:00
added "back" option
This commit is contained in:
@@ -26,7 +26,7 @@ impl Args {
|
||||
pub fn from_cli(
|
||||
optional_clap_variant: Option<CliArgs>,
|
||||
context: (),
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
let age = Self::from_cli_age(
|
||||
optional_clap_variant
|
||||
.clone()
|
||||
@@ -50,11 +50,11 @@ impl Args {
|
||||
first_name,
|
||||
second_name,
|
||||
};
|
||||
Ok(Self {
|
||||
Ok(Some(Self {
|
||||
age: new_context_scope.age,
|
||||
first_name: new_context_scope.first_name,
|
||||
second_name: new_context_scope.second_name,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn input_age(_context: &()) -> color_eyre::eyre::Result<u64> {
|
||||
|
||||
+17
-9
@@ -33,32 +33,34 @@ pub enum Submit {
|
||||
impl Submit {
|
||||
fn choose_variant(
|
||||
connection_config: common::ConnectionConfig,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
let variants = SubmitDiscriminants::iter().collect::<Vec<_>>();
|
||||
let submits = variants
|
||||
let mut submits = variants
|
||||
.iter()
|
||||
.map(|p| p.get_message().unwrap().to_owned())
|
||||
.collect::<Vec<_>>();
|
||||
submits.push("back".to_string());
|
||||
let select_submit = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("How would you like to proceed")
|
||||
.items(&submits)
|
||||
.default(0)
|
||||
.interact()
|
||||
.unwrap();
|
||||
match variants[select_submit] {
|
||||
SubmitDiscriminants::Send => Ok(Submit::Send),
|
||||
SubmitDiscriminants::Display => Ok(Submit::Display),
|
||||
match variants.get(select_submit) {
|
||||
Some(SubmitDiscriminants::Send) => Ok(Some(Submit::Send)),
|
||||
Some(SubmitDiscriminants::Display) => Ok(Some(Submit::Display)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_cli(
|
||||
optional_clap_variant: Option<<Submit as interactive_clap::ToCli>::CliVariant>,
|
||||
context: common::ConnectionConfig,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
let submit: Option<Submit> = optional_clap_variant.clone();
|
||||
match submit {
|
||||
Some(submit) => Ok(submit),
|
||||
None => Ok(Submit::Display),
|
||||
Some(submit) => Ok(Some(submit)),
|
||||
None => Ok(Some(Submit::Display)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,7 +72,13 @@ impl interactive_clap::ToCli for Submit {
|
||||
fn main() {
|
||||
let mut cli_online_args = OnlineArgs::parse();
|
||||
let context = common::ConnectionConfig::Testnet; //#[interactive_clap(context = common::ConnectionConfig)]
|
||||
let online_args = OnlineArgs::from_cli(Some(cli_online_args), context).unwrap();
|
||||
let online_args = loop {
|
||||
if let Some(args) =
|
||||
OnlineArgs::from_cli(Some(cli_online_args.clone()), context.clone()).unwrap()
|
||||
{
|
||||
break args;
|
||||
}
|
||||
};
|
||||
cli_online_args = online_args.into();
|
||||
let completed_cli = cli_online_args.to_cli_args();
|
||||
println!(
|
||||
|
||||
@@ -102,13 +102,13 @@ pub fn fn_choose_variant(
|
||||
cli_variant = quote! {
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
fn prompt_variant<T>(prompt: &str) -> T
|
||||
fn prompt_variant<T>(prompt: &str) -> Option<T>
|
||||
where
|
||||
T: IntoEnumIterator + EnumMessage,
|
||||
T: Copy + Clone,
|
||||
{
|
||||
let variants = T::iter().collect::<Vec<_>>();
|
||||
let actions = variants
|
||||
let mut actions = variants
|
||||
.iter()
|
||||
.map(|p| {
|
||||
p.get_message()
|
||||
@@ -116,6 +116,7 @@ pub fn fn_choose_variant(
|
||||
.to_owned()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
actions.push("back".to_string());
|
||||
|
||||
let selected = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(prompt)
|
||||
@@ -124,9 +125,14 @@ pub fn fn_choose_variant(
|
||||
.interact()
|
||||
.unwrap();
|
||||
|
||||
variants[selected]
|
||||
}
|
||||
let cli_variant = match prompt_variant(#literal.to_string().as_str()) {
|
||||
variants.get(selected).cloned()
|
||||
};
|
||||
let variant = if let Some(variant) = prompt_variant(#literal.to_string().as_str()) {
|
||||
variant
|
||||
} else {
|
||||
return Ok(None);
|
||||
};
|
||||
let cli_variant = match variant {
|
||||
#( #enum_variants, )*
|
||||
};
|
||||
};
|
||||
@@ -141,9 +147,13 @@ pub fn fn_choose_variant(
|
||||
let input_context = interactive_clap_attrs_context.get_input_context_dir();
|
||||
|
||||
quote! {
|
||||
pub fn choose_variant(context: #input_context) -> color_eyre::eyre::Result<Self> {
|
||||
#cli_variant
|
||||
Ok(Self::from_cli(Some(cli_variant), context.clone())?)
|
||||
pub fn choose_variant(context: #input_context) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
loop {
|
||||
#cli_variant
|
||||
if let Some(variant) = Self::from_cli(Some(cli_variant), context.clone())? {
|
||||
return Ok(Some(variant));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,21 +28,33 @@ pub fn from_cli_for_enum(
|
||||
|
||||
match &interactive_clap_attrs_context.output_context_dir {
|
||||
Some(_) => quote! {
|
||||
Some(#cli_name::#variant_ident(args)) => {
|
||||
Some(#cli_name::#variant_ident(inner_cli_args)) => {
|
||||
type Alias = <#name as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope;
|
||||
let new_context_scope = Alias::#variant_ident;
|
||||
let new_context = #context_name::from_previous_context((), &new_context_scope);
|
||||
Ok(Self::#variant_ident(#ty::from_cli(Some(args), new_context.into())?,))
|
||||
let new_context = #context_name::from_previous_context(context.clone(), &new_context_scope);
|
||||
let optional_inner_args = #ty::from_cli(Some(inner_cli_args), new_context.into())?;
|
||||
if let Some(inner_args) = optional_inner_args {
|
||||
Ok(Some(Self::#variant_ident(inner_args,)))
|
||||
} else {
|
||||
Self::choose_variant(context.clone())
|
||||
}
|
||||
}
|
||||
},
|
||||
None => quote! {
|
||||
Some(#cli_name::#variant_ident(args)) => Ok(Self::#variant_ident(#ty::from_cli(Some(args), context.clone())?,)),
|
||||
Some(#cli_name::#variant_ident(inner_cli_args)) => {
|
||||
let optional_inner_args = #ty::from_cli(Some(inner_cli_args), context.clone())?;
|
||||
if let Some(inner_args) = optional_inner_args {
|
||||
Ok(Some(Self::#variant_ident(inner_args,)))
|
||||
} else {
|
||||
Self::choose_variant(context.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
syn::Fields::Unit => {
|
||||
quote! {
|
||||
Some(#cli_name::#variant_ident) => Ok(Self::#variant_ident),
|
||||
Some(#cli_name::#variant_ident) => Ok(Some(Self::#variant_ident)),
|
||||
}
|
||||
},
|
||||
_ => abort_call_site!("Only option `Fields::Unnamed` or `Fields::Unit` is needed")
|
||||
@@ -57,7 +69,7 @@ pub fn from_cli_for_enum(
|
||||
pub fn from_cli(
|
||||
optional_clap_variant: Option<#cli_name>,
|
||||
context: #input_context_dir,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
match optional_clap_variant {
|
||||
#(#from_cli_variants)*
|
||||
None => Self::choose_variant(context.clone()),
|
||||
|
||||
+23
-3
@@ -84,12 +84,12 @@ pub fn from_cli_for_struct(
|
||||
pub fn from_cli(
|
||||
optional_clap_variant: Option<#cli_name>,
|
||||
context: #input_context_dir,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
) -> color_eyre::eyre::Result<Option<Self>> {
|
||||
#(#fields_value)*
|
||||
#new_context_scope
|
||||
#field_value_named_arg
|
||||
#field_value_subcommand
|
||||
Ok(Self{ #(#struct_fields,)* })
|
||||
#field_value_subcommand;
|
||||
Ok(Some(Self{ #(#struct_fields,)* }))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,11 @@ fn field_value_named_arg(
|
||||
}),
|
||||
new_context.into(),
|
||||
)?;
|
||||
let #ident_field = if let Some(value) = #ident_field {
|
||||
value
|
||||
} else {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
},
|
||||
None => quote! {
|
||||
@@ -172,6 +177,11 @@ fn field_value_named_arg(
|
||||
}),
|
||||
context.into(),
|
||||
)?;
|
||||
let #ident_field = if let Some(value) = #ident_field {
|
||||
value
|
||||
} else {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -218,6 +228,11 @@ fn field_value_subcommand(
|
||||
Some(cli_arg) => #ty::from_cli(Some(cli_arg), new_context.into())?,
|
||||
None => #ty::choose_variant(new_context.into())?,
|
||||
};
|
||||
let #ident_field = if let Some(value) = #ident_field {
|
||||
value
|
||||
} else {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
},
|
||||
None => quote! {
|
||||
@@ -225,6 +240,11 @@ fn field_value_subcommand(
|
||||
Some(cli_arg) => #ty::from_cli(Some(cli_arg), context)?,
|
||||
None => #ty::choose_variant(context.into())?,
|
||||
};
|
||||
let #ident_field = if let Some(value) = #ident_field {
|
||||
value
|
||||
} else {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user