Skip to content

Commit

Permalink
feat(common::shell): add global verbosity level (-vvv) flag repla…
Browse files Browse the repository at this point in the history
…cing `--verbose` (#9273)

* remove --verbose, prefer output mode, introduce verbosity level (-vvv)

* remove leftover

* fix arg

* add ability to set verbosity level

* fix tests

* remove evm args specific verbosity arg in favor of global arg due to Clap limitation

* revert test modifications from #9244 for TestArgs, simply pass + flatten ShellOpts in args

* in lieu of a context specific help document the verbosity levels of the EVM as an example

* format comment, update tests

* fix clippy
  • Loading branch information
zerosnacks authored Nov 13, 2024
1 parent 4817280 commit 22cf683
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 180 deletions.
4 changes: 2 additions & 2 deletions crates/cast/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ pub enum CastSubcommand {
who: Option<String>,

/// Perform a reverse lookup to verify that the name is correct.
#[arg(long, short)]
#[arg(long)]
verify: bool,

#[command(flatten)]
Expand All @@ -807,7 +807,7 @@ pub enum CastSubcommand {
who: Option<Address>,

/// Perform a normal lookup to verify that the address is correct.
#[arg(long, short)]
#[arg(long)]
verify: bool,

#[command(flatten)]
Expand Down
8 changes: 2 additions & 6 deletions crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use foundry_cli::{
opts::{EtherscanOpts, RpcOpts},
utils::{handle_traces, init_progress, TraceResult},
};
use foundry_common::{is_known_system_sender, SYSTEM_TRANSACTION_TYPE};
use foundry_common::{is_known_system_sender, shell, SYSTEM_TRANSACTION_TYPE};
use foundry_compilers::artifacts::EvmVersion;
use foundry_config::{
figment::{
Expand Down Expand Up @@ -48,10 +48,6 @@ pub struct RunArgs {
#[arg(long)]
quick: bool,

/// Prints the full address of the contract.
#[arg(long, short)]
verbose: bool,

/// Label addresses in the trace.
///
/// Example: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045:vitalik.eth
Expand Down Expand Up @@ -252,7 +248,7 @@ impl RunArgs {
self.label,
self.debug,
self.decode_internal,
self.verbose,
shell::verbosity() > 0,
)
.await?;

Expand Down
7 changes: 1 addition & 6 deletions crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ pub enum WalletSubcommands {
#[arg(value_name = "MNEMONIC_INDEX_OR_DERIVATION_PATH")]
mnemonic_index_or_derivation_path_override: Option<String>,

/// Verbose mode, print the address and private key.
#[arg(short = 'v', long)]
verbose: bool,

#[command(flatten)]
wallet: WalletOpts,
},
Expand Down Expand Up @@ -462,7 +458,6 @@ flag to set your key via:
wallet,
mnemonic_override,
mnemonic_index_or_derivation_path_override,
verbose,
} => {
let (index_override, derivation_path_override) =
match mnemonic_index_or_derivation_path_override {
Expand All @@ -485,7 +480,7 @@ flag to set your key via:
.await?;
match wallet {
WalletSigner::Local(wallet) => {
if verbose {
if shell::verbosity() > 0 {
sh_println!("Address: {}", wallet.address())?;
sh_println!(
"Private key: 0x{}",
Expand Down
16 changes: 13 additions & 3 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Options:
Display options:
--color <COLOR>
Log messages coloring
The color of the log messages
Possible values:
- auto: Intelligently guess whether to use color output (default)
Expand All @@ -46,8 +46,18 @@ Display options:
-q, --quiet
Do not print log messages
--verbose
Use verbose output
-v, --verbosity...
Verbosity level of the log messages.
Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
Depending on the context the verbosity levels have different meanings.
For example, the verbosity levels of the EVM are:
- 2 (-vv): Print logs for all tests.
- 3 (-vvv): Print execution traces for failing tests.
- 4 (-vvvv): Print execution traces for all tests, and setup traces for failing tests.
- 5 (-vvvvv): Print execution and setup traces for all tests.
Find more information in the book: http://book.getfoundry.sh/reference/cast/cast.html
Expand Down
43 changes: 22 additions & 21 deletions crates/cli/src/opts/shell.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
use clap::Parser;
use foundry_common::shell::{ColorChoice, OutputFormat, Shell, Verbosity};
use clap::{ArgAction, Parser};
use foundry_common::shell::{ColorChoice, OutputFormat, OutputMode, Shell, Verbosity};

// note: `verbose` and `quiet` cannot have `short` because of conflicts with multiple commands.

/// Global shell options.
#[derive(Clone, Copy, Debug, Parser)]
#[derive(Clone, Copy, Debug, Default, Parser)]
pub struct ShellOpts {
/// Use verbose output.
#[clap(long, global = true, conflicts_with = "quiet", help_heading = "Display options")]
pub verbose: bool,
/// Verbosity level of the log messages.
///
/// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
///
/// Depending on the context the verbosity levels have different meanings.
///
/// For example, the verbosity levels of the EVM are:
/// - 2 (-vv): Print logs for all tests.
/// - 3 (-vvv): Print execution traces for failing tests.
/// - 4 (-vvvv): Print execution traces for all tests, and setup traces for failing tests.
/// - 5 (-vvvvv): Print execution and setup traces for all tests.
#[clap(short, long, global = true, verbatim_doc_comment, conflicts_with = "quiet", action = ArgAction::Count, help_heading = "Display options")]
pub verbosity: Verbosity,

/// Do not print log messages.
#[clap(
short,
long,
global = true,
alias = "silent",
conflicts_with = "verbose",
help_heading = "Display options"
)]
#[clap(short, long, global = true, alias = "silent", help_heading = "Display options")]
pub quiet: bool,

/// Format log messages as JSON.
Expand All @@ -31,25 +34,23 @@ pub struct ShellOpts {
)]
pub json: bool,

/// Log messages coloring.
/// The color of the log messages.
#[clap(long, global = true, value_enum, help_heading = "Display options")]
pub color: Option<ColorChoice>,
}

impl ShellOpts {
pub fn shell(self) -> Shell {
let verbosity = match (self.verbose, self.quiet) {
(true, false) => Verbosity::Verbose,
(false, true) => Verbosity::Quiet,
(false, false) => Verbosity::Normal,
(true, true) => unreachable!(),
let mode = match self.quiet {
true => OutputMode::Quiet,
false => OutputMode::Normal,
};
let color = self.json.then_some(ColorChoice::Never).or(self.color).unwrap_or_default();
let format = match self.json {
true => OutputFormat::Json,
false => OutputFormat::Text,
};

Shell::new_with(format, color, verbosity)
Shell::new_with(format, mode, color, self.verbosity)
}
}
21 changes: 5 additions & 16 deletions crates/common/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! CLI arguments for configuring the EVM settings.
use alloy_primitives::{map::HashMap, Address, B256, U256};
use clap::{ArgAction, Parser};
use clap::Parser;
use eyre::ContextCompat;
use foundry_config::{
figment::{
Expand All @@ -14,6 +14,8 @@ use foundry_config::{
};
use serde::Serialize;

use crate::shell;

/// Map keyed by breakpoints char to their location (contract address, pc)
pub type Breakpoints = HashMap<char, (Address, usize)>;

Expand Down Expand Up @@ -101,19 +103,6 @@ pub struct EvmArgs {
#[serde(skip)]
pub always_use_create_2_factory: bool,

/// Verbosity of the EVM.
///
/// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
///
/// Verbosity levels:
/// - 2: Print logs for all tests
/// - 3: Print execution traces for failing tests
/// - 4: Print execution traces for all tests, and setup traces for failing tests
/// - 5: Print execution and setup traces for all tests
#[arg(long, short, verbatim_doc_comment, action = ArgAction::Count)]
#[serde(skip)]
pub verbosity: u8,

/// Sets the number of assumed available compute units per second for this provider
///
/// default value: 330
Expand Down Expand Up @@ -163,9 +152,9 @@ impl Provider for EvmArgs {
let error = InvalidType(value.to_actual(), "map".into());
let mut dict = value.into_dict().ok_or(error)?;

if self.verbosity > 0 {
if shell::verbosity() > 0 {
// need to merge that manually otherwise `from_occurrences` does not work
dict.insert("verbosity".to_string(), self.verbosity.into());
dict.insert("verbosity".to_string(), shell::verbosity().into());
}

if self.ffi {
Expand Down
Loading

0 comments on commit 22cf683

Please sign in to comment.