Skip to content

Commit c74d72d

Browse files
zerosnacksrplusq
authored andcommitted
feat: rename ShellOtps to GlobalOpts (foundry-rs#9313)
* rename ShellOpts to GlobalOpts * prefer arg / command over clap * add global opts * remove redundant GlobalOpts injection, only use where access to the global variables is required * add global thread pool * add try_jobs method for global rayon pool * limit unnecessary globalopts injection where shell::* is preferred * fix tests * port custom threads iterator to use global rayon thread pool * remove redundant ignores * remove leftover from merge conflict, fix clashing args with inlined global in nodeargs / anvil top level args * leftovers * add back global args in script args * fix unused global opts * ignore attempted multiple initializations of the global thread pool * add init, default spawn with default rayon settings on forge test * make test thread number configurable * add back max threads back test to reduce pressure * remove global --jobs rayon pool, revert to current implementation * fix import
1 parent 2179abf commit c74d72d

File tree

14 files changed

+79
-52
lines changed

14 files changed

+79
-52
lines changed

crates/anvil/src/anvil.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use anvil::cmd::NodeArgs;
44
use clap::{CommandFactory, Parser, Subcommand};
55
use eyre::Result;
6-
use foundry_cli::{opts::ShellOpts, utils};
6+
use foundry_cli::{opts::GlobalOpts, utils};
77

88
#[cfg(all(feature = "jemalloc", unix))]
99
#[global_allocator]
@@ -13,14 +13,15 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1313
#[derive(Parser)]
1414
#[command(name = "anvil", version = anvil::VERSION_MESSAGE, next_display_order = None)]
1515
pub struct Anvil {
16+
/// Include the global options.
17+
#[command(flatten)]
18+
pub global: GlobalOpts,
19+
1620
#[command(flatten)]
1721
pub node: NodeArgs,
1822

1923
#[command(subcommand)]
2024
pub cmd: Option<AnvilSubcommand>,
21-
22-
#[clap(flatten)]
23-
pub shell: ShellOpts,
2425
}
2526

2627
#[derive(Subcommand)]
@@ -48,7 +49,7 @@ fn run() -> Result<()> {
4849
utils::load_dotenv();
4950

5051
let mut args = Anvil::parse();
51-
args.shell.shell().set();
52+
args.global.init()?;
5253
args.node.evm_opts.resolve_rpc_alias();
5354

5455
if let Some(cmd) = &args.cmd {

crates/cast/bin/args.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use alloy_primitives::{Address, B256, U256};
99
use alloy_rpc_types::BlockId;
1010
use clap::{Parser, Subcommand, ValueHint};
1111
use eyre::Result;
12-
use foundry_cli::opts::{EtherscanOpts, RpcOpts, ShellOpts};
12+
use foundry_cli::opts::{EtherscanOpts, GlobalOpts, RpcOpts};
1313
use foundry_common::ens::NameOrAddress;
1414
use std::{path::PathBuf, str::FromStr};
1515

@@ -31,11 +31,12 @@ const VERSION_MESSAGE: &str = concat!(
3131
next_display_order = None,
3232
)]
3333
pub struct Cast {
34+
/// Include the global options.
35+
#[command(flatten)]
36+
pub global: GlobalOpts,
37+
3438
#[command(subcommand)]
3539
pub cmd: CastSubcommand,
36-
37-
#[clap(flatten)]
38-
pub shell: ShellOpts,
3940
}
4041

4142
#[derive(Subcommand)]

crates/cast/bin/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ fn run() -> Result<()> {
5050
utils::load_dotenv();
5151
utils::subscriber();
5252
utils::enable_paint();
53+
5354
let args = CastArgs::parse();
54-
args.shell.shell().set();
55+
args.global.init()?;
5556
main_args(args)
5657
}
5758

crates/chisel/bin/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use clap::{Parser, Subcommand};
1111
use eyre::Context;
1212
use foundry_cli::{
1313
handler,
14-
opts::{CoreBuildArgs, ShellOpts},
14+
opts::{CoreBuildArgs, GlobalOpts},
1515
utils::{self, LoadConfig},
1616
};
1717
use foundry_common::{evm::EvmArgs, fs};
@@ -50,12 +50,13 @@ const VERSION_MESSAGE: &str = concat!(
5050
#[derive(Debug, Parser)]
5151
#[command(name = "chisel", version = VERSION_MESSAGE)]
5252
pub struct Chisel {
53+
/// Include the global options.
54+
#[command(flatten)]
55+
pub global: GlobalOpts,
56+
5357
#[command(subcommand)]
5458
pub cmd: Option<ChiselSubcommand>,
5559

56-
#[clap(flatten)]
57-
pub shell: ShellOpts,
58-
5960
/// Path to a directory containing Solidity files to import, or path to a single Solidity file.
6061
///
6162
/// These files will be evaluated before the top-level of the
@@ -117,8 +118,9 @@ fn run() -> eyre::Result<()> {
117118
handler::install();
118119
utils::subscriber();
119120
utils::load_dotenv();
121+
120122
let args = Chisel::parse();
121-
args.shell.shell().set();
123+
args.global.init()?;
122124
main_args(args)
123125
}
124126

crates/cli/src/opts/shell.rs renamed to crates/cli/src/opts/global.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clap::{ArgAction, Parser};
22
use foundry_common::shell::{ColorChoice, OutputFormat, OutputMode, Shell, Verbosity};
3+
use serde::{Deserialize, Serialize};
34

4-
// note: `verbose` and `quiet` cannot have `short` because of conflicts with multiple commands.
5-
6-
/// Global shell options.
7-
#[derive(Clone, Copy, Debug, Default, Parser)]
8-
pub struct ShellOpts {
5+
/// Global options.
6+
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, Parser)]
7+
pub struct GlobalOpts {
98
/// Verbosity level of the log messages.
109
///
1110
/// Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
@@ -22,7 +21,7 @@ pub struct ShellOpts {
2221

2322
/// Do not print log messages.
2423
#[clap(short, long, global = true, alias = "silent", help_heading = "Display options")]
25-
pub quiet: bool,
24+
quiet: bool,
2625

2726
/// Format log messages as JSON.
2827
#[clap(
@@ -32,14 +31,23 @@ pub struct ShellOpts {
3231
conflicts_with_all = &["quiet", "color"],
3332
help_heading = "Display options"
3433
)]
35-
pub json: bool,
34+
json: bool,
3635

3736
/// The color of the log messages.
3837
#[clap(long, global = true, value_enum, help_heading = "Display options")]
39-
pub color: Option<ColorChoice>,
38+
color: Option<ColorChoice>,
4039
}
4140

42-
impl ShellOpts {
41+
impl GlobalOpts {
42+
/// Initialize the global options.
43+
pub fn init(self) -> eyre::Result<()> {
44+
// Set the global shell.
45+
self.shell().set();
46+
47+
Ok(())
48+
}
49+
50+
/// Create a new shell instance.
4351
pub fn shell(self) -> Shell {
4452
let mode = match self.quiet {
4553
true => OutputMode::Quiet,

crates/cli/src/opts/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ mod build;
22
mod chain;
33
mod dependency;
44
mod ethereum;
5-
mod shell;
5+
mod global;
66
mod transaction;
77

88
pub use build::*;
99
pub use chain::*;
1010
pub use dependency::*;
1111
pub use ethereum::*;
12-
pub use shell::*;
12+
pub use global::*;
1313
pub use transaction::*;

crates/common/src/io/shell.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use anstream::AutoStream;
77
use anstyle::Style;
88
use clap::ValueEnum;
99
use eyre::Result;
10+
use serde::{Deserialize, Serialize};
1011
use std::{
1112
fmt,
1213
io::{prelude::*, IsTerminal},
@@ -34,7 +35,7 @@ pub fn is_quiet() -> bool {
3435

3536
/// Returns whether the output format is [`OutputFormat::Json`].
3637
pub fn is_json() -> bool {
37-
Shell::get().output_format().is_json()
38+
Shell::get().is_json()
3839
}
3940

4041
/// The global shell instance.
@@ -172,7 +173,7 @@ enum ShellOut {
172173
}
173174

174175
/// Whether messages should use color output.
175-
#[derive(Debug, Default, PartialEq, Clone, Copy, ValueEnum)]
176+
#[derive(Debug, Default, PartialEq, Clone, Copy, Serialize, Deserialize, ValueEnum)]
176177
pub enum ColorChoice {
177178
/// Intelligently guess whether to use color output (default).
178179
#[default]
@@ -262,6 +263,16 @@ impl Shell {
262263
self.needs_clear.swap(needs_clear, Ordering::Relaxed)
263264
}
264265

266+
/// Returns `true` if the output format is JSON.
267+
pub fn is_json(&self) -> bool {
268+
self.output_format.is_json()
269+
}
270+
271+
/// Returns `true` if the verbosity level is `Quiet`.
272+
pub fn is_quiet(&self) -> bool {
273+
self.output_mode.is_quiet()
274+
}
275+
265276
/// Returns `true` if the `needs_clear` flag is set.
266277
#[inline]
267278
pub fn needs_clear(&self) -> bool {

crates/forge/bin/cmd/create.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use alloy_signer::Signer;
1010
use alloy_transport::{Transport, TransportError};
1111
use clap::{Parser, ValueHint};
1212
use eyre::{Context, Result};
13-
use forge_verify::RetryArgs;
13+
use forge_verify::{RetryArgs, VerifierArgs, VerifyArgs};
1414
use foundry_cli::{
1515
opts::{CoreBuildArgs, EthereumOpts, EtherscanOpts, TransactionOpts},
1616
utils::{self, read_constructor_args_file, remove_contract, LoadConfig},
@@ -89,7 +89,7 @@ pub struct CreateArgs {
8989
eth: EthereumOpts,
9090

9191
#[command(flatten)]
92-
pub verifier: forge_verify::VerifierArgs,
92+
pub verifier: VerifierArgs,
9393

9494
#[command(flatten)]
9595
retry: RetryArgs,
@@ -201,7 +201,7 @@ impl CreateArgs {
201201
) -> Result<()> {
202202
// NOTE: this does not represent the same `VerifyArgs` that would be sent after deployment,
203203
// since we don't know the address yet.
204-
let mut verify = forge_verify::VerifyArgs {
204+
let mut verify = VerifyArgs {
205205
address: Default::default(),
206206
contract: Some(self.contract.clone()),
207207
compiler_version: Some(id.version.to_string()),
@@ -359,7 +359,7 @@ impl CreateArgs {
359359
} else {
360360
None
361361
};
362-
let verify = forge_verify::VerifyArgs {
362+
let verify = VerifyArgs {
363363
address,
364364
contract: Some(self.contract),
365365
compiler_version: Some(id.version.to_string()),

crates/forge/bin/cmd/soldeer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clap::Parser;
22
use eyre::Result;
3-
43
use soldeer_commands::Command;
54

65
// CLI arguments for `forge soldeer`.

crates/forge/bin/cmd/test/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use forge::{
1717
MultiContractRunner, MultiContractRunnerBuilder, TestFilter, TestOptions, TestOptionsBuilder,
1818
};
1919
use foundry_cli::{
20-
opts::{CoreBuildArgs, ShellOpts},
20+
opts::{CoreBuildArgs, GlobalOpts},
2121
utils::{self, LoadConfig},
2222
};
2323
use foundry_common::{compile::ProjectCompiler, evm::EvmArgs, fs, shell, TestFunctionExt};
@@ -65,6 +65,10 @@ foundry_config::merge_impl_figment_convert!(TestArgs, opts, evm_opts);
6565
#[derive(Clone, Debug, Parser)]
6666
#[command(next_help_heading = "Test options")]
6767
pub struct TestArgs {
68+
/// Include the global options.
69+
#[command(flatten)]
70+
pub global: GlobalOpts,
71+
6872
/// The contract file you want to test, it's a shortcut for --match-path.
6973
#[arg(value_hint = ValueHint::FilePath)]
7074
pub path: Option<GlobMatcher>,
@@ -178,9 +182,6 @@ pub struct TestArgs {
178182
/// Print detailed test summary table.
179183
#[arg(long, help_heading = "Display options", requires = "summary")]
180184
pub detailed: bool,
181-
182-
#[command(flatten)]
183-
shell: ShellOpts,
184185
}
185186

186187
impl TestArgs {

0 commit comments

Comments
 (0)