mirror of
https://github.com/Drop-OSS/interactive-clap.git
synced 2026-01-30 20:55:25 +01:00
add examples
This commit is contained in:
1220
Cargo.lock
generated
1220
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
19
Cargo.toml
@@ -6,3 +6,22 @@ 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 }
|
||||
|
||||
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"]
|
||||
|
||||
20
examples/enum_example.rs
Normal file
20
examples/enum_example.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use strum::{EnumDiscriminants, EnumIter, EnumMessage};
|
||||
|
||||
#[derive(Debug, Clone, EnumDiscriminants, interactive_clap_derive::InteractiveClap)]
|
||||
#[strum_discriminants(derive(EnumMessage, EnumIter))]
|
||||
#[interactive_clap(context = ())]
|
||||
pub enum Mode {
|
||||
#[strum_discriminants(strum(message = "Yes, I keep it simple"))]
|
||||
Network,
|
||||
#[strum_discriminants(strum(
|
||||
message = "No, I want to work in no-network (air-gapped) environment"
|
||||
))]
|
||||
Offline,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli_mode = CliMode::Network;
|
||||
println!("cli_mode: {:?}", cli_mode);
|
||||
let variant = Mode::choose_variant(());
|
||||
println!("variant: {:?}", variant)
|
||||
}
|
||||
3
examples/hello.rs
Normal file
3
examples/hello.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello from an example!");
|
||||
}
|
||||
27
examples/struct_example.rs
Normal file
27
examples/struct_example.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use clap::Clap;
|
||||
|
||||
#[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())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli_args = CliArgs::parse();
|
||||
println!("cli: {:?}", &cli_args);
|
||||
let args = Args::from_cli(Some(cli_args), ());
|
||||
println!("args: {:#?}", args)
|
||||
}
|
||||
50
examples/struct_with_context.rs
Normal file
50
examples/struct_with_context.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(input_context = ())]
|
||||
#[interactive_clap(output_context = OfflineArgsContext)]
|
||||
pub struct OfflineArgs {
|
||||
#[interactive_clap(named_arg)]
|
||||
///Specify a sender
|
||||
account: Sender,
|
||||
}
|
||||
|
||||
pub struct OfflineArgsContext {}
|
||||
|
||||
impl OfflineArgsContext {
|
||||
fn from_previous_context(
|
||||
_previous_context: (),
|
||||
_scope: &<OfflineArgs as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
|
||||
) -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OfflineArgsContext> for NetworkContext {
|
||||
fn from(_: OfflineArgsContext) -> Self {
|
||||
Self {
|
||||
connection_config: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NetworkContext {
|
||||
pub connection_config: Option<crate::common::ConnectionConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(context = OfflineArgsContext)]
|
||||
pub struct Sender {
|
||||
pub sender_account_id: String,
|
||||
}
|
||||
|
||||
impl Sender {
|
||||
fn input_sender_account_id(context: &OfflineArgsContext) -> color_eyre::eyre::Result<String> {
|
||||
Ok("Volodymyr".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
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), ());
|
||||
println!("offline_args: {:?}", offline_args)
|
||||
}
|
||||
26
examples/struct_with_named_attrs_and_custom_type_example.rs
Normal file
26
examples/struct_with_named_attrs_and_custom_type_example.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(context = ())]
|
||||
struct OfflineArgs {
|
||||
#[interactive_clap(named_arg)]
|
||||
///Specify a sender
|
||||
account: Sender,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(context = ())]
|
||||
pub struct Sender {
|
||||
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::default();
|
||||
println!("cli_offline_args: {:?}", cli_offline_args);
|
||||
let offline_args = OfflineArgs::from_cli(Some(cli_offline_args), ());
|
||||
println!("offline_args: {:?}", offline_args)
|
||||
}
|
||||
13
examples/struct_with_subcommand_example.rs
Normal file
13
examples/struct_with_subcommand_example.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod enum_example;
|
||||
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(context = ())]
|
||||
pub struct OperationMode {
|
||||
#[interactive_clap(subcommand)]
|
||||
pub mode: enum_example::Mode,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli_operation_mode = CliOperationMode::default();
|
||||
println!("cli_operation_mode: {:?}", cli_operation_mode)
|
||||
}
|
||||
64
examples/to_cli_args.rs
Normal file
64
examples/to_cli_args.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use strum::{EnumDiscriminants, EnumIter, EnumMessage, IntoEnumIterator};
|
||||
|
||||
#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
|
||||
#[interactive_clap(context = crate::common::ConnectionConfig)]
|
||||
struct OnlineArgs {
|
||||
#[interactive_clap(subcommand)]
|
||||
submit: Submit,
|
||||
}
|
||||
|
||||
#[derive(Debug, EnumDiscriminants, Clone, clap::Clap, interactive_clap_derive::ToCliArgs)]
|
||||
#[strum_discriminants(derive(EnumMessage, EnumIter))]
|
||||
pub enum Submit {
|
||||
#[strum_discriminants(strum(message = "I want to send the transaction to the network"))]
|
||||
Send,
|
||||
#[strum_discriminants(strum(
|
||||
message = "I only want to print base64-encoded transaction for JSON RPC input and exit"
|
||||
))]
|
||||
Display,
|
||||
}
|
||||
|
||||
impl Submit {
|
||||
fn choose_variant(connection_config: crate::common::ConnectionConfig) -> color_eyre::eyre::Result<Self> {
|
||||
let variants = SubmitDiscriminants::iter().collect::<Vec<_>>();
|
||||
let submits = variants
|
||||
.iter()
|
||||
.map(|p| p.get_message().unwrap().to_owned())
|
||||
.collect::<Vec<_>>();
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_cli(
|
||||
optional_clap_variant: Option<<Submit as interactive_clap::ToCli>::CliVariant>,
|
||||
context: crate::common::ConnectionConfig,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
let submit: Option<Submit> = optional_clap_variant
|
||||
.clone();
|
||||
match submit {
|
||||
Some(_) => Ok(Submit::Send),
|
||||
None => Ok(Submit::Display)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl interactive_clap::ToCli for Submit {
|
||||
type CliVariant = 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)
|
||||
}
|
||||
@@ -16,6 +16,6 @@ syn = "1.0"
|
||||
clap = "3.0.0-beta.2"
|
||||
clap_generate = "3.0.0-beta.2"
|
||||
|
||||
interactive_clap = { path = "../" }
|
||||
# interactive_clap = { path = "../" }
|
||||
# interactive_clap = { git = "https://github.com/FroVolod/interactive-clap" }
|
||||
|
||||
|
||||
33
src/common.rs
Normal file
33
src/common.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
pub fn prompt_variant<T>(prompt: &str) -> T
|
||||
where
|
||||
T: IntoEnumIterator + EnumMessage,
|
||||
T: Copy + Clone,
|
||||
{
|
||||
let variants = T::iter().collect::<Vec<_>>();
|
||||
let actions = variants
|
||||
.iter()
|
||||
.map(|p| {
|
||||
p.get_message()
|
||||
.unwrap_or_else(|| "error[This entry does not have an option message!!]")
|
||||
.to_owned()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let selected = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(prompt)
|
||||
.items(&actions)
|
||||
.default(0)
|
||||
.interact()
|
||||
.unwrap();
|
||||
|
||||
variants[selected]
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConnectionConfig {
|
||||
Testnet,
|
||||
Mainnet,
|
||||
Betanet,
|
||||
}
|
||||
Reference in New Issue
Block a user