Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: refactor print help #1154

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `WalletCommand::Mnemonic` now takes 2 optional arguments to avoid user interaction;
- `AccountCommand::Transaction` now accepts either an index or an ID;
- Use `CommandFactory` to print help programmatically;
- `print_wallet_help` changed to `WalletCli::print_help`;
- `print_account_help` changed to `AccountCli::print_help`;

## 1.0.0 - 2023-07-27

Expand Down
4 changes: 2 additions & 2 deletions cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
voting_power_command, AccountCli, AccountCommand,
},
error::Error,
helper::{bytes_from_hex_or_file, print_account_help},
helper::bytes_from_hex_or_file,
println_log_error,
};

Expand Down Expand Up @@ -67,7 +67,7 @@ pub async fn account_prompt_internal(
.completion_with(&AccountCompletion)
.interact_text()?;
match command.as_str() {
"h" | "help" => print_account_help(),
"h" | "help" => AccountCli::print_help()?,
"c" | "clear" => {
// Clear console
let _ = std::process::Command::new("clear").status();
Expand Down
9 changes: 8 additions & 1 deletion cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::str::FromStr;

use clap::{Parser, Subcommand};
use clap::{CommandFactory, Parser, Subcommand};
use iota_sdk::{
client::request_funds_from_faucet,
types::{
Expand Down Expand Up @@ -37,6 +37,13 @@ pub struct AccountCli {
pub command: AccountCommand,
}

impl AccountCli {
pub fn print_help() -> Result<(), Error> {
Self::command().bin_name("Account:").print_help()?;
Ok(())
}
}

#[derive(Debug, Subcommand)]
#[allow(clippy::large_enum_variant)]
pub enum AccountCommand {
Expand Down
9 changes: 8 additions & 1 deletion cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::path::Path;

use clap::{builder::BoolishValueParser, Args, Parser, Subcommand};
use clap::{builder::BoolishValueParser, Args, CommandFactory, Parser, Subcommand};
use iota_sdk::{
client::{
constants::SHIMMER_COIN_TYPE,
Expand Down Expand Up @@ -44,6 +44,13 @@ pub struct WalletCli {
pub command: Option<WalletCommand>,
}

impl WalletCli {
pub fn print_help() -> Result<(), Error> {
Self::command().bin_name("wallet").print_help()?;
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}

#[derive(Debug, Clone, Subcommand)]
pub enum WalletCommand {
/// List all accounts.
Expand Down
19 changes: 1 addition & 18 deletions cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::path::Path;

use chrono::{DateTime, NaiveDateTime, Utc};
use clap::Parser;
use dialoguer::{console::Term, theme::ColorfulTheme, Input, Select};
use iota_sdk::{
client::{utils::Password, verify_mnemonic},
Expand All @@ -17,11 +16,7 @@ use tokio::{
};
use zeroize::Zeroize;

use crate::{
command::{account::AccountCli, wallet::WalletCli},
error::Error,
println_log_error, println_log_info,
};
use crate::{error::Error, println_log_error, println_log_info};

const DEFAULT_MNEMONIC_FILE_PATH: &str = "./mnemonic.txt";

Expand Down Expand Up @@ -90,18 +85,6 @@ pub async fn pick_account(wallet: &Wallet) -> Result<Option<Account>, Error> {
}
}

pub fn print_wallet_help() {
if let Err(err) = WalletCli::try_parse_from(["Wallet:", "help"]) {
println!("{err}");
}
}

pub fn print_account_help() {
if let Err(err) = AccountCli::try_parse_from(["Account:", "help"]) {
println!("{err}");
}
}

pub async fn bytes_from_hex_or_file(hex: Option<String>, file: Option<String>) -> Result<Option<Vec<u8>>, Error> {
Ok(if let Some(hex) = hex {
Some(prefix_hex::decode(hex).map_err(|e| Error::Miscellaneous(e.to_string()))?)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
restore_command, set_node_url_command, sync_command, unlock_wallet, InitParameters, WalletCli, WalletCommand,
},
error::Error,
helper::{get_account_alias, get_decision, get_password, pick_account, print_wallet_help},
helper::{get_account_alias, get_decision, get_password, pick_account},
println_log_error, println_log_info,
};

Expand Down Expand Up @@ -92,7 +92,7 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<Accoun
println_log_info!("Created new wallet.");
create_initial_account(wallet).await?
} else {
print_wallet_help();
WalletCli::print_help()?;
(None, None)
}
}
Expand Down
Loading