Skip to content

Commit c20ee54

Browse files
authored
update trin-types from structopt to clap (#740)
1 parent cd3e6bc commit c20ee54

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

newsfragments/740.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace `structopt` with `clap` when parsing command line arguments in ``trin-types``.

trin-types/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ authors = ["https://github.com/ethereum/trin/graphs/contributors"]
1414
anyhow = "1.0.68"
1515
base64 = "0.13.0"
1616
bytes = "1.3.0"
17-
clap = "2.33.3"
17+
clap = { version = "4.2.1", features = ["derive"] }
1818
discv5 = { version = "0.2.1", features = ["serde"]}
1919
eth_trie = "0.1.0"
2020
ethereum-types = "0.12.1"
@@ -35,7 +35,6 @@ serde_yaml = "0.9.17"
3535
sha2 = "0.10.1"
3636
sha3 = "0.9.1"
3737
snap = "1.1.0"
38-
structopt = "0.3.26"
3938
stremio-serde-hex = "0.1.0"
4039
tree_hash = "0.4.0"
4140
tree_hash_derive = "0.4.0"

trin-types/src/cli.rs

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr};
22

3+
use clap::{arg, Parser};
34
use ethereum_types::H256;
4-
use structopt::StructOpt;
55
use url::Url;
66

77
use crate::bootnodes::Bootnodes;
@@ -34,142 +34,129 @@ impl fmt::Display for Web3TransportType {
3434
}
3535
}
3636

37-
#[derive(Debug, PartialEq, Eq)]
38-
pub struct ParseWeb3TransportError;
39-
40-
impl fmt::Display for ParseWeb3TransportError {
41-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42-
write!(
43-
f,
44-
"Invalid web3-transport arg. Expected either 'http' or 'ipc'"
45-
)
46-
}
47-
}
48-
4937
impl FromStr for Web3TransportType {
50-
type Err = ParseWeb3TransportError;
38+
type Err = &'static str;
5139

5240
fn from_str(s: &str) -> Result<Self, Self::Err> {
5341
match s {
5442
"http" => Ok(Web3TransportType::HTTP),
5543
"ipc" => Ok(Web3TransportType::IPC),
56-
_ => Err(ParseWeb3TransportError),
44+
_ => Err("Invalid web3-transport arg. Expected either 'http' or 'ipc'"),
5745
}
5846
}
5947
}
6048

61-
#[derive(StructOpt, Debug, PartialEq, Clone)]
62-
#[structopt(
49+
#[derive(Parser, Debug, PartialEq, Clone)]
50+
#[command(
6351
name = "trin",
6452
version = "0.0.1",
6553
author = "carver",
6654
about = "Run an eth portal client"
6755
)]
6856
pub struct TrinConfig {
69-
#[structopt(
70-
default_value(DEFAULT_WEB3_TRANSPORT),
57+
#[arg(
58+
default_value = DEFAULT_WEB3_TRANSPORT,
7159
long = "web3-transport",
7260
help = "select transport protocol to serve json-rpc endpoint"
7361
)]
7462
pub web3_transport: Web3TransportType,
7563

76-
#[structopt(
77-
default_value(DEFAULT_WEB3_HTTP_ADDRESS),
64+
#[arg(
65+
default_value = DEFAULT_WEB3_HTTP_ADDRESS,
7866
long = "web3-http-address",
7967
help = "address to accept json-rpc http connections"
8068
)]
8169
pub web3_http_address: Url,
8270

83-
#[structopt(
84-
default_value(DEFAULT_WEB3_IPC_PATH),
71+
#[arg(
72+
default_value = DEFAULT_WEB3_IPC_PATH,
8573
long = "web3-ipc-path",
8674
help = "path to json-rpc endpoint over IPC"
8775
)]
8876
pub web3_ipc_path: PathBuf,
8977

90-
#[structopt(
91-
default_value(DEFAULT_DISCOVERY_PORT),
78+
#[arg(
79+
default_value = DEFAULT_DISCOVERY_PORT,
9280
long = "discovery-port",
9381
help = "The UDP port to listen on."
9482
)]
9583
pub discovery_port: u16,
9684

97-
#[structopt(
98-
default_value("default"),
85+
#[arg(
86+
default_value = "default",
9987
long = "bootnodes",
10088
help = "One or more comma-delimited base64-encoded ENR's or multiaddr strings of peers to initially add to the local routing table"
10189
)]
10290
pub bootnodes: Bootnodes,
10391

104-
#[structopt(
92+
#[arg(
10593
long = "external-address",
10694
group = "external-ips",
10795
help = "(Only use this if you are behind a NAT) The address which will be advertised to peers (in an ENR). Changing it does not change which port or address trin binds to. Port number is required, ex: 127.0.0.1:9001"
10896
)]
10997
pub external_addr: Option<SocketAddr>,
11098

111-
#[structopt(
99+
#[arg(
112100
long = "no-stun",
113101
group = "external-ips",
114102
help = "Do not use STUN to determine an external IP. Leaves ENR entry for IP blank. Some users report better connections over VPN."
115103
)]
116104
pub no_stun: bool,
117105

118-
#[structopt(
119-
validator(check_private_key_length),
106+
#[arg(
120107
long = "unsafe-private-key",
108+
value_parser = check_private_key_length,
121109
help = "Hex encoded 32 byte private key (with 0x prefix) (considered unsafe as it's stored in terminal history - keyfile support coming soon)"
122110
)]
123111
pub private_key: Option<H256>,
124112

125-
#[structopt(
126-
long = "networks",
113+
#[arg(
114+
long = "networks",
127115
help = "Comma-separated list of which portal subnetworks to activate",
128116
default_value = DEFAULT_SUBNETWORKS,
129-
use_delimiter = true
117+
use_value_delimiter = true
130118
)]
131119
pub networks: Vec<String>,
132120

133121
/// Storage capacity specified in megabytes.
134-
#[structopt(
122+
#[arg(
135123
default_value(DEFAULT_STORAGE_CAPACITY_MB),
136124
long,
137125
help = "Maximum number of megabytes of total data to store in the DB (actual usage will exceed limit due to overhead)"
138126
)]
139127
pub mb: u32,
140128

141-
#[structopt(
129+
#[arg(
142130
long = "enable-metrics-with-url",
143131
help = "Enable prometheus metrics reporting (provide local IP/Port from which your Prometheus server is configured to fetch metrics)"
144132
)]
145133
pub enable_metrics_with_url: Option<SocketAddr>,
146134

147-
#[structopt(
148-
short = "e",
135+
#[arg(
136+
short = 'e',
149137
long = "ephemeral",
150138
help = "Use temporary data storage that is deleted on exit."
151139
)]
152140
pub ephemeral: bool,
153141

154-
#[structopt(
142+
#[arg(
155143
long = "trusted-provider",
156144
help = "Trusted provider to use. (options: 'infura' (default), 'pandaops' (devops) or 'custom')",
157145
default_value(DEFAULT_TRUSTED_PROVIDER)
158146
)]
159147
pub trusted_provider: TrustedProviderType,
160148

161-
#[structopt(
149+
#[arg(
162150
long = "trusted-provider-url",
163-
help = "URL for a trusted http provider. Must include a base, host and port (e.g., '<base>://<host>:<port>').",
164-
validator(check_url_format)
151+
value_parser = check_url_format,
152+
help = "URL for a trusted http provider. Must include a base, host and port (e.g., '<base>://<host>:<port>')."
165153
)]
166154
pub trusted_provider_url: Option<Url>,
167155

168-
#[structopt(
156+
#[arg(
169157
long = "master-accumulator-path",
170158
help = "Path to master accumulator for validation",
171-
default_value(DEFAULT_MASTER_ACC_PATH),
172-
parse(from_os_str)
159+
default_value(DEFAULT_MASTER_ACC_PATH)
173160
)]
174161
pub master_acc_path: PathBuf,
175162
}
@@ -214,7 +201,7 @@ impl TrinConfig {
214201
I: Iterator<Item = T>,
215202
T: Into<OsString> + Clone,
216203
{
217-
let config = Self::from_iter_safe(args)?;
204+
let config = Self::try_parse_from(args)?;
218205

219206
match config.web3_transport {
220207
Web3TransportType::HTTP => match &config.web3_ipc_path.as_path().display().to_string()[..] {
@@ -260,16 +247,16 @@ impl TrinConfig {
260247
}
261248

262249
/// A validator function for CLI URL arguments.
263-
fn check_url_format(url: String) -> Result<(), String> {
264-
match Url::parse(&url) {
265-
Ok(_) => Ok(()),
250+
fn check_url_format(url: &str) -> Result<Url, String> {
251+
match Url::parse(url) {
252+
Ok(val) => Ok(val),
266253
Err(e) => panic!("Invalid URL '{url}', {e}"),
267254
}
268255
}
269256

270-
fn check_private_key_length(private_key: String) -> Result<(), String> {
257+
fn check_private_key_length(private_key: &str) -> Result<H256, String> {
271258
if private_key.len() == 66 {
272-
return Ok(());
259+
return H256::from_str(private_key).map_err(|err| format!("HexError: {}", err));
273260
}
274261
panic!(
275262
"Invalid private key length: {}, expected 66 (0x-prefixed 32 byte hexstring)",

0 commit comments

Comments
 (0)