diff --git a/Cargo.lock b/Cargo.lock index b65c0d7..a96d16a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,9 +96,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.0.0-beta.4" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcd70aa5597dbc42f7217a543f9ef2768b2ef823ba29036072d30e1d88e98406" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" dependencies = [ "atty", "bitflags", @@ -109,14 +109,15 @@ dependencies = [ "strsim", "termcolor", "textwrap", + "unicode-width", "vec_map", ] [[package]] name = "clap_derive" -version = "3.0.0-beta.4" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5bb0d655624a0b8770d1c178fb8ffcb1f91cc722cb08f451e3dc72465421ac" +checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" dependencies = [ "heck", "proc-macro-error", @@ -127,9 +128,9 @@ dependencies = [ [[package]] name = "clap_generate" -version = "3.0.0-beta.4" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d9b1abef93569f290952eff3c4a0a92d6767bb5158db095b4dc9a512b1c3643" +checksum = "adf420f8b687b628d2915ccfd43a660c437a170432e3fbcb66944e8717a0d68f" dependencies = [ "clap", ] @@ -474,11 +475,13 @@ name = "interactive_clap" version = "0.1.0" dependencies = [ "clap", + "clap_derive", "clap_generate", "color-eyre", "dialoguer", "interactive_clap_derive", "near-ledger", + "shell-words", "strum", "strum_macros 0.20.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -487,9 +490,6 @@ dependencies = [ name = "interactive_clap_derive" version = "0.1.0" dependencies = [ - "clap", - "clap_derive", - "clap_generate", "proc-macro-error", "proc-macro2", "quote", @@ -634,9 +634,9 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "os_str_bytes" -version = "3.1.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d" +checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" [[package]] name = "owo-colors" @@ -832,6 +832,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "signature" version = "1.5.0" @@ -956,9 +962,9 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.14.2" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" dependencies = [ "unicode-width", ] diff --git a/Cargo.toml b/Cargo.toml index f7b25cc..3d23c71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,22 +6,24 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] - -# [dev-dependencies] -clap = "3.0.0-beta.4" -clap_generate = "3.0.0-beta.4" - -color-eyre = "0.5" - interactive_clap_derive = { path = "./interactive_clap_derive" } near-ledger = { version = "0.1.1", optional = true } +[features] +default = ["ledger"] +ledger = ["near-ledger"] + +[dev-dependencies] +shell-words = "1.0.0" + +clap = "=3.0.0-beta.2" +clap_derive = "=3.0.0-beta.2" +clap_generate = "=3.0.0-beta.2" + strum = { git = "https://github.com/frol/strum", branch = "feat/discriminants-pass-through-attributes", features = ["derive"] } strum_macros = "0.20" dialoguer = "0.9" -[features] -default = ["ledger"] -ledger = ["near-ledger"] +color-eyre = "0.5" diff --git a/examples/common.rs b/examples/common.rs new file mode 100644 index 0000000..308ba45 --- /dev/null +++ b/examples/common.rs @@ -0,0 +1,6 @@ +#[derive(Debug, Clone)] +pub enum ConnectionConfig { + Testnet, + Mainnet, + Betanet, +} diff --git a/examples/enum_example.rs b/examples/simple_enum.rs similarity index 100% rename from examples/enum_example.rs rename to examples/simple_enum.rs diff --git a/examples/struct_example.rs b/examples/simple_struct.rs similarity index 100% rename from examples/struct_example.rs rename to examples/simple_struct.rs diff --git a/examples/struct_with_named_attrs_and_custom_type_example.rs b/examples/struct_with_named_arg.rs similarity index 91% rename from examples/struct_with_named_attrs_and_custom_type_example.rs rename to examples/struct_with_named_arg.rs index 89aa29f..d5af216 100644 --- a/examples/struct_with_named_attrs_and_custom_type_example.rs +++ b/examples/struct_with_named_arg.rs @@ -1,3 +1,4 @@ +use clap::Clap; #[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)] #[interactive_clap(context = ())] struct OfflineArgs { @@ -19,7 +20,7 @@ impl Sender { } fn main() { - let cli_offline_args = CliOfflineArgs::default(); + let cli_offline_args = CliOfflineArgs::parse(); println!("cli_offline_args: {:?}", cli_offline_args); let offline_args = OfflineArgs::from_cli(Some(cli_offline_args), ()); println!("offline_args: {:?}", offline_args) diff --git a/examples/struct_with_subcommand_example.rs b/examples/struct_with_subcommand_example.rs index 3edf7ad..bbefdcc 100644 --- a/examples/struct_with_subcommand_example.rs +++ b/examples/struct_with_subcommand_example.rs @@ -1,10 +1,10 @@ -mod enum_example; +mod simple_enum; #[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)] #[interactive_clap(context = ())] pub struct OperationMode { #[interactive_clap(subcommand)] - pub mode: enum_example::Mode, + pub mode: simple_enum::Mode, } fn main() { diff --git a/examples/to_cli_args.rs b/examples/to_cli_args.rs index 52cdd7b..ce211d8 100644 --- a/examples/to_cli_args.rs +++ b/examples/to_cli_args.rs @@ -1,6 +1,8 @@ use dialoguer::{theme::ColorfulTheme, Select}; use strum::{EnumDiscriminants, EnumIter, EnumMessage, IntoEnumIterator}; +mod common; + #[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)] #[interactive_clap(context = crate::common::ConnectionConfig)] struct OnlineArgs { @@ -20,7 +22,7 @@ pub enum Submit { } impl Submit { - fn choose_variant(connection_config: crate::common::ConnectionConfig) -> color_eyre::eyre::Result { + fn choose_variant(connection_config: common::ConnectionConfig) -> color_eyre::eyre::Result { let variants = SubmitDiscriminants::iter().collect::>(); let submits = variants .iter() @@ -40,12 +42,12 @@ impl Submit { fn from_cli( optional_clap_variant: Option<::CliVariant>, - context: crate::common::ConnectionConfig, + context: common::ConnectionConfig, ) -> color_eyre::eyre::Result { let submit: Option = optional_clap_variant .clone(); match submit { - Some(_) => Ok(Submit::Send), + Some(submit) => Ok(submit), None => Ok(Submit::Display) } @@ -57,8 +59,16 @@ impl interactive_clap::ToCli for Submit { } fn main() { - let cli_online_args = CliOnlineArgs::default(); - println!("cli_online_args: {:?}", cli_online_args); - let online_args = OnlineArgs::from_cli(Some(cli_online_args), crate::common::ConnectionConfig::Testnet); - println!("online_args: {:?}", online_args) + let mut cli_online_args = OnlineArgs::parse(); + println!("cli_online_args: {:?}", &cli_online_args); + let online_args = OnlineArgs::from_cli(Some(cli_online_args.clone()), common::ConnectionConfig::Testnet).unwrap(); + println!("online_args: {:?}", online_args); + // cli_online_args = CliOnlineArgs::from(online_args); + cli_online_args = online_args.into(); + println!("cli_online_args: {:?}", &cli_online_args); + let completed_cli = cli_online_args.to_cli_args(); + println!( + "Your console command:\n./near-cli {}", + shell_words::join(&completed_cli) + ); } diff --git a/interactive_clap_derive/Cargo.toml b/interactive_clap_derive/Cargo.toml index f1cee25..1ee0128 100644 --- a/interactive_clap_derive/Cargo.toml +++ b/interactive_clap_derive/Cargo.toml @@ -13,10 +13,3 @@ proc-macro2 = "1.0.24" proc-macro-error = "1" quote = "1.0" syn = "1.0" -clap = "=3.0.0-beta.4" -clap_derive = "=3.0.0-beta.4" -clap_generate = "=3.0.0-beta.4" - -# interactive_clap = { path = "../" } -# interactive_clap = { git = "https://github.com/FroVolod/interactive-clap" } - diff --git a/interactive_clap_derive/src/derives/interactive_clap/methods/choose_variant.rs b/interactive_clap_derive/src/derives/interactive_clap/methods/choose_variant.rs index 4262708..ccc137f 100644 --- a/interactive_clap_derive/src/derives/interactive_clap/methods/choose_variant.rs +++ b/interactive_clap_derive/src/derives/interactive_clap/methods/choose_variant.rs @@ -105,11 +105,37 @@ pub fn fn_choose_variant(ast: &syn::DeriveInput, variants: &syn::punctuated::Pun }); - + cli_variant = quote! { - let cli_variant = match crate::common::prompt_variant(#literal.to_string().as_str()) { + use dialoguer::{theme::ColorfulTheme, Select}; + use strum::{EnumMessage, IntoEnumIterator}; + fn prompt_variant(prompt: &str) -> T + where + T: IntoEnumIterator + EnumMessage, + T: Copy + Clone, + { + let variants = T::iter().collect::>(); + let actions = variants + .iter() + .map(|p| { + p.get_message() + .unwrap_or_else(|| "error[This entry does not have an option message!!]") + .to_owned() + }) + .collect::>(); + + let selected = Select::with_theme(&ColorfulTheme::default()) + .with_prompt(prompt) + .items(&actions) + .default(0) + .interact() + .unwrap(); + + variants[selected] + } + let cli_variant = match prompt_variant(#literal.to_string().as_str()) { #( #enum_variants, )* - }; + }; }; }; }