Skip to content

Commit 9e29032

Browse files
authored
chore: upgrade deprecated clap usage (#3346)
1 parent d6fae73 commit 9e29032

File tree

17 files changed

+81
-75
lines changed

17 files changed

+81
-75
lines changed

cli/src/cmd/cast/call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use foundry_config::{Chain, Config};
1717

1818
#[derive(Debug, Parser)]
1919
pub struct CallArgs {
20-
#[clap(help = "The destination of the transaction.", parse(try_from_str = parse_name_or_address), value_name = "TO")]
20+
#[clap(help = "The destination of the transaction.", value_parser = parse_name_or_address, value_name = "TO")]
2121
to: Option<NameOrAddress>,
2222
#[clap(help = "The signature of the function to call.", value_name = "SIG")]
2323
sig: Option<String>,
@@ -28,7 +28,7 @@ pub struct CallArgs {
2828
#[clap(flatten)]
2929
// TODO: We only need RPC URL and Etherscan API key here.
3030
eth: EthereumOpts,
31-
#[clap(long, short, help = "the block you want to query, can also be earliest/latest/pending", parse(try_from_str = parse_block_id), value_name = "BLOCK")]
31+
#[clap(long, short, help = "the block you want to query, can also be earliest/latest/pending", value_parser = parse_block_id, value_name = "BLOCK")]
3232
block: Option<BlockId>,
3333
#[clap(subcommand)]
3434
command: Option<CallSubcommands>,
@@ -50,7 +50,7 @@ pub enum CallSubcommands {
5050
long_help = r#"Ether to send in the transaction, either specified in wei, or as a string with a unit type.
5151
5252
Examples: 1ether, 10gwei, 0.01ether"#,
53-
parse(try_from_str = parse_ether_value),
53+
value_parser = parse_ether_value,
5454
value_name = "VALUE"
5555
)]
5656
value: Option<U256>,

cli/src/cmd/cast/estimate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use foundry_config::{Chain, Config};
1414

1515
#[derive(Debug, Parser)]
1616
pub struct EstimateArgs {
17-
#[clap(help = "The destination of the transaction.", parse(try_from_str = parse_name_or_address), value_name = "TO")]
17+
#[clap(help = "The destination of the transaction.", value_parser = parse_name_or_address, value_name = "TO")]
1818
to: Option<NameOrAddress>,
1919
#[clap(help = "The signature of the function to call.", value_name = "SIG")]
2020
sig: Option<String>,
@@ -26,7 +26,7 @@ pub struct EstimateArgs {
2626
long_help = r#"Ether to send in the transaction, either specified in wei, or as a string with a unit type.
2727
2828
Examples: 1ether, 10gwei, 0.01ether"#,
29-
parse(try_from_str = parse_ether_value),
29+
value_parser = parse_ether_value,
3030
value_name = "VALUE"
3131
)]
3232
value: Option<U256>,
@@ -53,7 +53,7 @@ pub enum EstimateSubcommands {
5353
long_help = r#"Ether to send in the transaction, either specified in wei, or as a string with a unit type.
5454
5555
Examples: 1ether, 10gwei, 0.01ether"#,
56-
parse(try_from_str = parse_ether_value),
56+
value_parser = parse_ether_value,
5757
value_name = "VALUE"
5858
)]
5959
value: Option<U256>,

cli/src/cmd/cast/send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::Arc;
1111
pub struct SendTxArgs {
1212
#[clap(
1313
help = "The destination of the transaction. If not provided, you must use cast send --create.",
14-
parse(try_from_str = parse_name_or_address),
14+
value_parser = parse_name_or_address,
1515
value_name = "TO"
1616
)]
1717
to: Option<NameOrAddress>,

cli/src/cmd/cast/wallet/vanity.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::cmd::Cmd;
44
use cast::SimpleCast;
5-
use clap::Parser;
5+
use clap::{builder::TypedValueParser, Parser};
66
use ethers::{
77
core::{k256::ecdsa::SigningKey, rand::thread_rng},
88
prelude::{LocalWallet, Signer},
@@ -22,11 +22,11 @@ pub struct VanityArgs {
2222
long,
2323
help = "Prefix for the vanity address.",
2424
required_unless_present = "ends-with",
25-
validator = hex_address_validator(),
25+
value_parser = HexAddressValidator::default(),
2626
value_name = "HEX"
2727
)]
2828
pub starts_with: Option<String>,
29-
#[clap(long, help = "Suffix for the vanity address.", validator = hex_address_validator(), value_name = "HEX")]
29+
#[clap(long, help = "Suffix for the vanity address.", value_parser = HexAddressValidator::default(), value_name = "HEX")]
3030
pub ends_with: Option<String>,
3131
#[clap(
3232
long,
@@ -280,13 +280,30 @@ impl VanityMatcher for RegexMatcher {
280280
}
281281
}
282282

283-
/// Validates an address from cli args.
284-
pub fn hex_address_validator() -> impl FnMut(&str) -> eyre::Result<()> {
285-
move |v: &str| -> eyre::Result<()> {
286-
if v.len() > 40 {
287-
eyre::bail!("vanity patterns length exceeded. cannot be more than 40 characters")
283+
/// Parse 40 byte addresses
284+
#[derive(Copy, Clone, Debug, Default)]
285+
#[non_exhaustive]
286+
pub struct HexAddressValidator;
287+
288+
impl TypedValueParser for HexAddressValidator {
289+
type Value = String;
290+
291+
fn parse_ref(
292+
&self,
293+
_cmd: &clap::Command,
294+
_arg: Option<&clap::Arg>,
295+
value: &std::ffi::OsStr,
296+
) -> Result<Self::Value, clap::Error> {
297+
if value.len() > 40 {
298+
return Err(clap::Error::raw(
299+
clap::ErrorKind::InvalidValue,
300+
"vanity patterns length exceeded. cannot be more than 40 characters",
301+
))
288302
}
289-
Ok(())
303+
let value = value.to_str().ok_or_else(|| {
304+
clap::Error::raw(clap::ErrorKind::InvalidUtf8, "address must be valid utf8")
305+
})?;
306+
Ok(value.to_string())
290307
}
291308
}
292309

cli/src/cmd/forge/cache.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! cache command
22
3-
use clap::{Parser, Subcommand};
3+
use clap::{builder::PossibleValuesParser, Parser, Subcommand};
44
use std::str::FromStr;
55
use strum::VariantNames;
66

@@ -16,7 +16,7 @@ pub struct CacheArgs {
1616
pub sub: CacheSubcommands,
1717
}
1818

19-
#[derive(Debug)]
19+
#[derive(Debug, Clone)]
2020
pub enum ChainOrAll {
2121
Chain(Chain),
2222
All,
@@ -43,8 +43,7 @@ pub struct CleanArgs {
4343
#[clap(
4444
env = "CHAIN",
4545
default_value = "all",
46-
possible_value = "all",
47-
possible_values = Chain::VARIANTS,
46+
value_parser = chain_value_parser(),
4847
value_name = "CHAINS"
4948
)]
5049
chains: Vec<ChainOrAll>,
@@ -70,8 +69,7 @@ pub struct LsArgs {
7069
#[clap(
7170
env = "CHAIN",
7271
default_value = "all",
73-
possible_value = "all",
74-
possible_values = Chain::VARIANTS,
72+
value_parser = chain_value_parser(),
7573
value_name = "CHAINS"
7674
)]
7775
chains: Vec<ChainOrAll>,
@@ -146,3 +144,7 @@ fn clean_chain_cache(
146144
}
147145
Ok(())
148146
}
147+
148+
fn chain_value_parser() -> PossibleValuesParser {
149+
Some(&"all").into_iter().chain(Chain::VARIANTS).into()
150+
}

cli/src/cmd/forge/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct FmtArgs {
2424
conflicts_with = "root",
2525
value_hint = ValueHint::FilePath,
2626
value_name = "PATH",
27-
multiple = true
27+
multiple_values = true
2828
)]
2929
paths: Vec<PathBuf>,
3030
#[clap(

cli/src/cmd/forge/geiger/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct GeigerArgs {
2121
conflicts_with = "root",
2222
value_hint = ValueHint::FilePath,
2323
value_name = "PATH",
24-
multiple = true
24+
multiple_values = true
2525
)]
2626
paths: Vec<PathBuf>,
2727
#[clap(

cli/src/cmd/forge/script/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct ScriptArgs {
161161
long,
162162
help = "Gas price for legacy transactions, or max fee per gas for EIP1559 transactions.",
163163
env = "ETH_GAS_PRICE",
164-
parse(try_from_str = parse_ether_value),
164+
value_parser = parse_ether_value,
165165
value_name = "PRICE"
166166
)]
167167
pub with_gas_price: Option<U256>,

cli/src/cmd/forge/snapshot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{
66
test,
77
test::{Test, TestOutcome},
88
},
9-
u32_validator, Cmd,
9+
Cmd,
1010
},
1111
utils::STATIC_FUZZ_SEED,
1212
};
13-
use clap::{Parser, ValueHint};
13+
use clap::{builder::RangedU64ValueParser, Parser, ValueHint};
1414
use ethers::types::U256;
1515
use eyre::Context;
1616
use forge::result::TestKindReport;
@@ -83,7 +83,7 @@ pub struct SnapshotArgs {
8383
#[clap(
8484
help = "Tolerates gas deviations up to the specified percentage.",
8585
long,
86-
validator = u32_validator(0, 100),
86+
value_parser = RangedU64ValueParser::<u32>::new().range(0..100),
8787
value_name = "SNAPSHOT_THRESHOLD"
8888
)]
8989
tolerance: Option<u32>,

cli/src/cmd/forge/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub struct TestArgs {
102102
#[clap(
103103
long,
104104
help = "Set seed used to generate randomness during your fuzz runs",
105-
parse(try_from_str = utils::parse_u256)
105+
value_parser = utils::parse_u256
106106
)]
107107
pub fuzz_seed: Option<U256>,
108108
}

0 commit comments

Comments
 (0)