fixed examles

This commit is contained in:
FroVolod 2022-02-18 16:26:50 +02:00
parent e147a52bdf
commit 5f2943af94
10 changed files with 128 additions and 267 deletions

View file

@ -0,0 +1,49 @@
// cargo run --example simple_struct -- --age-full-years 30 --first-name QWE --second-name QWERTY =>
// => args: Ok(Args { age: 30, first_name: "QWE", second_name: "QWERTY" })
// cargo run --example simple_struct => entered interactive mode
#[derive(Debug, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(skip_default_from_cli)]
struct Args {
#[interactive_clap(long = "age-full-years")] // hgfashdgfajdfsadajsdfh
#[interactive_clap(skip_default_from_cli)] // указать для чего этот атрибут нужен
#[interactive_clap(skip_default_input_arg)]
age: u64,
#[interactive_clap(long)]
///What is your first name?
first_name: String,
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
second_name: String
}
impl Args {
fn input_age(_context: &()) -> color_eyre::eyre::Result<u64> {
Ok(dialoguer::Input::new()
.with_prompt("How old are you?")
.interact_text()?)
}
fn input_second_name(_context: &()) -> color_eyre::eyre::Result<String> {
Ok(dialoguer::Input::new()
.with_prompt("What is your last name?")
.interact_text()?)
}
fn from_cli_age(
optional_cli_age: Option<u64>,
context: &(), // default: input_context = ()
) -> color_eyre::eyre::Result<u64> {
match optional_cli_age {
Some(age) => Ok(age),
None => Self::input_age(&context),
}
}
}
fn main() {
let cli_args = Args::parse();
let args = Args::from_cli(Some(cli_args), ());
println!("args: {:?}", args)
}

View file

@ -1,3 +0,0 @@
fn main() {
println!("Hello from an example!");
}

View file

@ -1,11 +1,22 @@
use strum::{EnumDiscriminants, EnumIter, EnumMessage};
// 1) собрать пример: cargo build --example simple_enum
// 2) cd target/debug/examples
// 3) запустить пример: ./simple_enum (без параметров) => entered interactive mode
// ./simple_enum network => mode: Ok(Network)
// ./simple_enum offline => mode: Ok(Offline)
use dialoguer::{theme::ColorfulTheme, Select};
use strum::{EnumDiscriminants, EnumIter, EnumMessage, IntoEnumIterator};
#[derive(Debug, Clone, EnumDiscriminants, interactive_clap_derive::InteractiveClap)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
#[interactive_clap(context = ())]
///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?
pub enum Mode {
/// Prepare and, optionally, submit a new transaction with online mode
#[strum_discriminants(strum(message = "Yes, I keep it simple"))]
Network,
/// Prepare and, optionally, submit a new transaction with offline mode
#[strum_discriminants(strum(
message = "No, I want to work in no-network (air-gapped) environment"
))]
@ -13,8 +24,8 @@ pub enum Mode {
}
fn main() {
let cli_mode = CliMode::Network;
println!("cli_mode: {:?}", cli_mode);
let variant = Mode::choose_variant(());
println!("variant: {:?}", variant)
let cli_mode = Mode::try_parse().ok();
let context = (); // default: input_context = ()
let mode = Mode::from_cli(cli_mode, context);
println!("mode: {:?}", mode)
}

View file

@ -1,27 +1,18 @@
use clap::Clap;
// cargo run --example simple_struct -- --age-full-years 30 --first-name QWE --second-name QWERTY =>
// => args: Ok(Args { age: 30, first_name: "QWE", second_name: "QWERTY" })
// cargo run --example simple_struct => entered interactive mode
#[derive(Debug, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = ())]
struct Args {
#[interactive_clap(long = "prepaid-gas")]
gas: u64,
#[interactive_clap(long)]
first_first: String,
}
impl Args {
fn input_gas(_context: &()) -> color_eyre::eyre::Result<u64> {
Ok(1_000_000_000)
}
fn input_first_first(_context: &()) -> color_eyre::eyre::Result<String> {
Ok("First".to_string())
}
age: u64,
first_name: String,
second_name: String
}
fn main() {
let cli_args = CliArgs::parse();
println!("cli: {:?}", &cli_args);
let args = Args::from_cli(Some(cli_args), ());
println!("args: {:#?}", args)
let cli_args = Args::parse();
let context = (); // default: input_context = ()
let args = Args::from_cli(Some(cli_args), context);
println!("args: {:?}", args)
}

View file

@ -1,3 +1,8 @@
// cargo run --example struct_with_context account QWERTY => offline_args: Ok(OfflineArgs { account: Sender { sender_account_id: "QWERTY" } })
// cargo run --example struct_with_context => entered interactive mode
mod common;
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(input_context = ())]
#[interactive_clap(output_context = OfflineArgsContext)]
@ -27,24 +32,27 @@ impl From<OfflineArgsContext> for NetworkContext {
}
pub struct NetworkContext {
pub connection_config: Option<crate::common::ConnectionConfig>,
pub connection_config: Option<common::ConnectionConfig>,
}
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = OfflineArgsContext)]
pub struct Sender {
#[interactive_clap(skip_default_input_arg)]
pub sender_account_id: String,
}
impl Sender {
fn input_sender_account_id(context: &OfflineArgsContext) -> color_eyre::eyre::Result<String> {
Ok("Volodymyr".to_string())
Ok(dialoguer::Input::new()
.with_prompt("What is the account ID?")
.interact_text()?)
}
}
fn main() {
let cli_offline_args = CliOfflineArgs::default();
println!("cli_offline_args: {:?}", cli_offline_args);
let offline_args = OfflineArgs::from_cli(Some(cli_offline_args), ());
let cli_offline_args = OfflineArgs::parse();
let context = (); // #[interactive_clap(input_context = ())]
let offline_args = OfflineArgs::from_cli(Some(cli_offline_args), context);
println!("offline_args: {:?}", offline_args)
}

View file

@ -1,27 +1,23 @@
// cargo run --example struct_with_named_arg account QWERTY => account: Ok(Account { account: Sender { sender_account_id: "QWERTY" } })
// cargo run --example struct_with_named_arg => entered interactive mode
use clap::Clap;
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = ())]
struct OfflineArgs {
struct Account {
#[interactive_clap(named_arg)]
///Specify a sender
account: Sender,
}
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = ())]
pub struct Sender {
///What is the account ID?
pub sender_account_id: String,
}
impl Sender {
fn input_sender_account_id(context: &()) -> color_eyre::eyre::Result<String> {
Ok("Volodymyr".to_string())
}
}
fn main() {
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)
let cli_account = Account::parse();
let context = (); // default: input_context = ()
let account = Account::from_cli(Some(cli_account), context);
println!("account: {:?}", account)
}

View file

@ -0,0 +1,18 @@
// cargo run --example struct_with_subcommand offline => operation_mode: Ok(OperationMode { mode: Offline })
// cargo run --example struct_with_subcommand network => operation_mode: Ok(OperationMode { mode: Network })
// cargo run --example struct_with_subcommand => entered interactive mode
mod simple_enum;
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
pub struct OperationMode {
#[interactive_clap(subcommand)]
pub mode: simple_enum::Mode,
}
fn main() {
let cli_operation_mode = OperationMode::parse();
let context = (); // default: input_context = ()
let operation_mode = OperationMode::from_cli(Some(cli_operation_mode), context);
println!("operation_mode: {:?}", &operation_mode);
}

View file

@ -1,13 +0,0 @@
mod simple_enum;
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = ())]
pub struct OperationMode {
#[interactive_clap(subcommand)]
pub mode: simple_enum::Mode,
}
fn main() {
let cli_operation_mode = CliOperationMode::default();
println!("cli_operation_mode: {:?}", cli_operation_mode)
}

View file

@ -1,10 +1,17 @@
// 1) собрать пример: cargo build --example to_cli_args
// 2) cd target/debug/examples
// 3) запустить пример: ./to_cli_args (без параметров) => entered interactive mode
// ./to_cli_args send => Your console command: send
// ./to_cli_args display => Your console command: display
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)]
#[interactive_clap(context = common::ConnectionConfig)]
struct OnlineArgs {
#[interactive_clap(subcommand)]
submit: Submit,
@ -60,19 +67,12 @@ impl interactive_clap::ToCli for Submit {
fn main() {
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);
let context = common::ConnectionConfig::Testnet; //#[interactive_clap(context = common::ConnectionConfig)]
let online_args = OnlineArgs::from_cli(Some(cli_online_args), context).unwrap();
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 {}",
"Your console command: {}",
shell_words::join(&completed_cli)
);
}

View file

@ -2,139 +2,16 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "3.0.0-beta.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcd70aa5597dbc42f7217a543f9ef2768b2ef823ba29036072d30e1d88e98406"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.0.0-beta.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b5bb0d655624a0b8770d1c178fb8ffcb1f91cc722cb08f451e3dc72465421ac"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_generate"
version = "3.0.0-beta.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d9b1abef93569f290952eff3c4a0a92d6767bb5158db095b4dc9a512b1c3643"
dependencies = [
"clap",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "interactive_clap"
version = "0.1.0"
source = "git+https://github.com/FroVolod/interactive-clap#d9329061e105eba0ce6ced463e047abef4730286"
[[package]]
name = "interactive_clap_derive"
version = "0.1.0"
dependencies = [
"clap",
"clap_generate",
"interactive_clap",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a5ac8f984bfcf3a823267e5fde638acc3325f6496633a5da6bb6eb2171e103"
[[package]]
name = "os_str_bytes"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -177,12 +54,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.76"
@ -194,81 +65,14 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"