Bump clap to 4.0.x and adjust to best practices (#12381)

* Bump clap to 3.2.22

* Replace `from_os_str` with `value_parser`

* Replace `from_str` and `try_from_str` with `value_parser`

* Move possible_values to the new format

* Remove unwanted print

* Add missing match branch

* Update clap to 4.0.9 and make it compile

* Replace deprecated `clap` macro with `command` and `value`

* Move remaining `clap` attributes to `arg`

* Remove no-op value_parsers

* Adjust value_parser for state_version

* Remove "deprecated" feature flag and bump to 4.0.11

* Improve range

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions

* Trigger CI

* Fix unused error warning

* Fix doc errors

* Fix ArgGroup naming conflict

* Change default_value to default_value_t

* Use 1.. instead of 0..

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Sebastian Kunert
2022-10-18 08:52:46 +02:00
committed by GitHub
parent ca15fe7e3d
commit f687db40f7
70 changed files with 434 additions and 443 deletions
@@ -23,17 +23,11 @@ use clap::Args;
#[derive(Debug, Clone, PartialEq, Args)]
pub struct DatabaseParams {
/// Select database backend to use.
#[clap(
long,
alias = "db",
value_name = "DB",
ignore_case = true,
possible_values = Database::variants(),
)]
#[arg(long, alias = "db", value_name = "DB", value_enum)]
pub database: Option<Database>,
/// Limit the memory the database cache can use.
#[clap(long = "db-cache", value_name = "MiB")]
#[arg(long = "db-cache", value_name = "MiB")]
pub database_cache_size: Option<usize>,
}
@@ -42,12 +42,12 @@ pub struct ImportParams {
pub database_params: DatabaseParams,
/// Method for executing Wasm runtime code.
#[clap(
#[arg(
long = "wasm-execution",
value_name = "METHOD",
possible_values = WasmExecutionMethod::variants(),
value_enum,
ignore_case = true,
default_value = DEFAULT_WASM_EXECUTION_METHOD,
default_value_t = DEFAULT_WASM_EXECUTION_METHOD,
)]
pub wasm_method: WasmExecutionMethod,
@@ -64,18 +64,18 @@ pub struct ImportParams {
/// The `legacy-instance-reuse` strategy is deprecated and will
/// be removed in the future. It should only be used in case of
/// issues with the default instantiation strategy.
#[clap(
#[arg(
long,
value_name = "STRATEGY",
default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
arg_enum,
value_enum,
)]
pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy,
/// Specify the path where local WASM runtimes are stored.
///
/// These runtimes will override on-chain runtimes when the version matches.
#[clap(long, value_name = "PATH", parse(from_os_str))]
#[arg(long, value_name = "PATH")]
pub wasm_runtime_overrides: Option<PathBuf>,
#[allow(missing_docs)]
@@ -85,13 +85,13 @@ pub struct ImportParams {
/// Specify the state cache size.
///
/// Providing `0` will disable the cache.
#[clap(long, value_name = "Bytes", default_value = "67108864")]
#[arg(long, value_name = "Bytes", default_value_t = 67108864)]
pub trie_cache_size: usize,
/// DEPRECATED
///
/// Switch to `--trie-cache-size`.
#[clap(long)]
#[arg(long)]
state_cache_size: Option<usize>,
}
@@ -156,39 +156,39 @@ impl ImportParams {
pub struct ExecutionStrategiesParams {
/// The means of execution used when calling into the runtime for importing blocks as
/// part of an initial sync.
#[clap(long, value_name = "STRATEGY", arg_enum, ignore_case = true)]
#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
pub execution_syncing: Option<ExecutionStrategy>,
/// The means of execution used when calling into the runtime for general block import
/// (including locally authored blocks).
#[clap(long, value_name = "STRATEGY", arg_enum, ignore_case = true)]
#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
pub execution_import_block: Option<ExecutionStrategy>,
/// The means of execution used when calling into the runtime while constructing blocks.
#[clap(long, value_name = "STRATEGY", arg_enum, ignore_case = true)]
#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
pub execution_block_construction: Option<ExecutionStrategy>,
/// The means of execution used when calling into the runtime while using an off-chain worker.
#[clap(long, value_name = "STRATEGY", arg_enum, ignore_case = true)]
#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
pub execution_offchain_worker: Option<ExecutionStrategy>,
/// The means of execution used when calling into the runtime while not syncing, importing or
/// constructing blocks.
#[clap(long, value_name = "STRATEGY", arg_enum, ignore_case = true)]
#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
pub execution_other: Option<ExecutionStrategy>,
/// The execution strategy that should be used by all execution contexts.
#[clap(
#[arg(
long,
value_name = "STRATEGY",
arg_enum,
value_enum,
ignore_case = true,
conflicts_with_all = &[
"execution-other",
"execution-offchain-worker",
"execution-block-construction",
"execution-import-block",
"execution-syncing",
"execution_other",
"execution_offchain_worker",
"execution_block_construction",
"execution_import_block",
"execution_syncing",
]
)]
pub execution: Option<ExecutionStrategy>,
@@ -32,32 +32,31 @@ const DEFAULT_KEYSTORE_CONFIG_PATH: &str = "keystore";
#[derive(Debug, Clone, Args)]
pub struct KeystoreParams {
/// Specify custom URIs to connect to for keystore-services
#[clap(long)]
#[arg(long)]
pub keystore_uri: Option<String>,
/// Specify custom keystore path.
#[clap(long, value_name = "PATH", parse(from_os_str))]
#[arg(long, value_name = "PATH")]
pub keystore_path: Option<PathBuf>,
/// Use interactive shell for entering the password used by the keystore.
#[clap(long, conflicts_with_all = &["password", "password-filename"])]
#[arg(long, conflicts_with_all = &["password", "password_filename"])]
pub password_interactive: bool,
/// Password used by the keystore. This allows appending an extra user-defined secret to the
/// seed.
#[clap(
#[arg(
long,
parse(try_from_str = secret_string_from_str),
conflicts_with_all = &["password-interactive", "password-filename"]
value_parser = secret_string_from_str,
conflicts_with_all = &["password_interactive", "password_filename"]
)]
pub password: Option<SecretString>,
/// File that contains the password used by the keystore.
#[clap(
#[arg(
long,
value_name = "PATH",
parse(from_os_str),
conflicts_with_all = &["password-interactive", "password"]
conflicts_with_all = &["password_interactive", "password"]
)]
pub password_filename: Option<PathBuf>,
}
+16 -6
View File
@@ -27,7 +27,7 @@ mod transaction_pool_params;
use crate::arg_enums::{CryptoScheme, OutputType};
use clap::Args;
use sp_core::crypto::Ss58AddressFormat;
use sp_core::crypto::{Ss58AddressFormat, Ss58AddressFormatRegistry};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, NumberFor},
@@ -40,6 +40,17 @@ pub use crate::params::{
transaction_pool_params::*,
};
/// Parse Ss58AddressFormat
pub fn parse_ss58_address_format(x: &str) -> Result<Ss58AddressFormat, String> {
match Ss58AddressFormatRegistry::try_from(x) {
Ok(format_registry) => Ok(format_registry.into()),
Err(_) => Err(format!(
"Unable to parse variant. Known variants: {:?}",
Ss58AddressFormat::all_names()
)),
}
}
/// Wrapper type of `String` that holds an unsigned integer of arbitrary size, formatted as a
/// decimal.
#[derive(Debug, Clone)]
@@ -118,7 +129,7 @@ impl BlockNumberOrHash {
#[derive(Debug, Clone, Args)]
pub struct CryptoSchemeFlag {
/// cryptography scheme
#[clap(long, value_name = "SCHEME", arg_enum, ignore_case = true, default_value = "sr25519")]
#[arg(long, value_name = "SCHEME", value_enum, ignore_case = true, default_value_t = CryptoScheme::Sr25519)]
pub scheme: CryptoScheme,
}
@@ -126,7 +137,7 @@ pub struct CryptoSchemeFlag {
#[derive(Debug, Clone, Args)]
pub struct OutputTypeFlag {
/// output format
#[clap(long, value_name = "FORMAT", arg_enum, ignore_case = true, default_value = "text")]
#[arg(long, value_name = "FORMAT", value_enum, ignore_case = true, default_value_t = OutputType::Text)]
pub output_type: OutputType,
}
@@ -134,13 +145,12 @@ pub struct OutputTypeFlag {
#[derive(Debug, Clone, Args)]
pub struct NetworkSchemeFlag {
/// network address format
#[clap(
#[arg(
short = 'n',
long,
value_name = "NETWORK",
possible_values = &Ss58AddressFormat::all_names()[..],
ignore_case = true,
parse(try_from_str = Ss58AddressFormat::try_from),
value_parser = parse_ss58_address_format,
)]
pub network: Option<Ss58AddressFormat>,
}
@@ -33,11 +33,11 @@ use std::{borrow::Cow, path::PathBuf};
#[derive(Debug, Clone, Args)]
pub struct NetworkParams {
/// Specify a list of bootnodes.
#[clap(long, value_name = "ADDR", multiple_values(true))]
#[arg(long, value_name = "ADDR", num_args = 1..)]
pub bootnodes: Vec<MultiaddrWithPeerId>,
/// Specify a list of reserved node addresses.
#[clap(long, value_name = "ADDR", multiple_values(true))]
#[arg(long, value_name = "ADDR", num_args = 1..)]
pub reserved_nodes: Vec<MultiaddrWithPeerId>,
/// Whether to only synchronize the chain with reserved nodes.
@@ -48,12 +48,12 @@ pub struct NetworkParams {
/// In particular, if you are a validator your node might still connect to other
/// validator nodes and collator nodes regardless of whether they are defined as
/// reserved nodes.
#[clap(long)]
#[arg(long)]
pub reserved_only: bool,
/// The public address that other nodes will use to connect to it.
/// This can be used if there's a proxy in front of this node.
#[clap(long, value_name = "PUBLIC_ADDR", multiple_values(true))]
#[arg(long, value_name = "PUBLIC_ADDR", num_args = 1..)]
pub public_addr: Vec<Multiaddr>,
/// Listen on this multiaddress.
@@ -61,49 +61,49 @@ pub struct NetworkParams {
/// By default:
/// If `--validator` is passed: `/ip4/0.0.0.0/tcp/<port>` and `/ip6/[::]/tcp/<port>`.
/// Otherwise: `/ip4/0.0.0.0/tcp/<port>/ws` and `/ip6/[::]/tcp/<port>/ws`.
#[clap(long, value_name = "LISTEN_ADDR", multiple_values(true))]
#[arg(long, value_name = "LISTEN_ADDR", num_args = 1..)]
pub listen_addr: Vec<Multiaddr>,
/// Specify p2p protocol TCP port.
#[clap(long, value_name = "PORT", conflicts_with_all = &[ "listen-addr" ])]
#[arg(long, value_name = "PORT", conflicts_with_all = &[ "listen_addr" ])]
pub port: Option<u16>,
/// Always forbid connecting to private IPv4 addresses (as specified in
/// [RFC1918](https://tools.ietf.org/html/rfc1918)), unless the address was passed with
/// `--reserved-nodes` or `--bootnodes`. Enabled by default for chains marked as "live" in
/// their chain specifications.
#[clap(long, conflicts_with_all = &["allow-private-ipv4"])]
#[arg(long, conflicts_with_all = &["allow_private_ipv4"])]
pub no_private_ipv4: bool,
/// Always accept connecting to private IPv4 addresses (as specified in
/// [RFC1918](https://tools.ietf.org/html/rfc1918)). Enabled by default for chains marked as
/// "local" in their chain specifications, or when `--dev` is passed.
#[clap(long, conflicts_with_all = &["no-private-ipv4"])]
#[arg(long, conflicts_with_all = &["no_private_ipv4"])]
pub allow_private_ipv4: bool,
/// Specify the number of outgoing connections we're trying to maintain.
#[clap(long, value_name = "COUNT", default_value = "25")]
#[arg(long, value_name = "COUNT", default_value_t = 25)]
pub out_peers: u32,
/// Maximum number of inbound full nodes peers.
#[clap(long, value_name = "COUNT", default_value = "25")]
#[arg(long, value_name = "COUNT", default_value_t = 25)]
pub in_peers: u32,
/// Maximum number of inbound light nodes peers.
#[clap(long, value_name = "COUNT", default_value = "100")]
#[arg(long, value_name = "COUNT", default_value_t = 100)]
pub in_peers_light: u32,
/// Disable mDNS discovery.
///
/// By default, the network will use mDNS to discover other nodes on the
/// local network. This disables it. Automatically implied when using --dev.
#[clap(long)]
#[arg(long)]
pub no_mdns: bool,
/// Maximum number of peers from which to ask for the same blocks in parallel.
///
/// This allows downloading announced blocks from multiple peers. Decrease to save
/// traffic and risk increased latency.
#[clap(long, value_name = "COUNT", default_value = "5")]
#[arg(long, value_name = "COUNT", default_value_t = 5)]
pub max_parallel_downloads: u32,
#[allow(missing_docs)]
@@ -114,7 +114,7 @@ pub struct NetworkParams {
///
/// By default this option is `true` for `--dev` or when the chain type is
/// `Local`/`Development` and false otherwise.
#[clap(long)]
#[arg(long)]
pub discover_local: bool,
/// Require iterative Kademlia DHT queries to use disjoint paths for increased resiliency in
@@ -122,11 +122,11 @@ pub struct NetworkParams {
///
/// See the S/Kademlia paper for more information on the high level design as well as its
/// security improvements.
#[clap(long)]
#[arg(long)]
pub kademlia_disjoint_query_paths: bool,
/// Join the IPFS network and serve transactions over bitswap protocol.
#[clap(long)]
#[arg(long)]
pub ipfs_server: bool,
/// Blockchain syncing mode.
@@ -135,11 +135,11 @@ pub struct NetworkParams {
/// - `fast`: Download blocks and the latest state only.
/// - `fast-unsafe`: Same as `fast`, but skip downloading state proofs.
/// - `warp`: Download the latest state and proof.
#[clap(
#[arg(
long,
arg_enum,
value_enum,
value_name = "SYNC_MODE",
default_value = "full",
default_value_t = SyncMode::Full,
ignore_case = true,
verbatim_doc_comment
)]
@@ -46,7 +46,7 @@ pub struct NodeKeyParams {
/// WARNING: Secrets provided as command-line arguments are easily exposed.
/// Use of this option should be limited to development and testing. To use
/// an externally managed secret key, use `--node-key-file` instead.
#[clap(long, value_name = "KEY")]
#[arg(long, value_name = "KEY")]
pub node_key: Option<String>,
/// The type of secret key to use for libp2p networking.
@@ -66,7 +66,7 @@ pub struct NodeKeyParams {
///
/// The node's secret key determines the corresponding public key and hence the
/// node's peer ID in the context of libp2p.
#[clap(long, value_name = "TYPE", arg_enum, ignore_case = true, default_value = "ed25519")]
#[arg(long, value_name = "TYPE", value_enum, ignore_case = true, default_value_t = NodeKeyType::Ed25519)]
pub node_key_type: NodeKeyType,
/// The file from which to read the node's secret key to use for libp2p networking.
@@ -79,7 +79,7 @@ pub struct NodeKeyParams {
///
/// If the file does not exist, it is created with a newly generated secret key of
/// the chosen type.
#[clap(long, value_name = "FILE")]
#[arg(long, value_name = "FILE")]
pub node_key_file: Option<PathBuf>,
}
@@ -122,7 +122,7 @@ fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519S
#[cfg(test)]
mod tests {
use super::*;
use clap::ArgEnum;
use clap::ValueEnum;
use sc_network::config::identity::{ed25519, Keypair};
use std::fs;
@@ -35,12 +35,12 @@ pub struct OffchainWorkerParams {
/// Should execute offchain workers on every block.
///
/// By default it's only enabled for nodes that are authoring new blocks.
#[clap(
#[arg(
long = "offchain-worker",
value_name = "ENABLED",
arg_enum,
value_enum,
ignore_case = true,
default_value = "when-validating"
default_value_t = OffchainWorkerEnabled::WhenValidating
)]
pub enabled: OffchainWorkerEnabled,
@@ -48,7 +48,7 @@ pub struct OffchainWorkerParams {
///
/// Enables a runtime to write directly to a offchain workers
/// DB during block import.
#[clap(long = "enable-offchain-indexing", value_name = "ENABLE_OFFCHAIN_INDEXING")]
#[arg(long = "enable-offchain-indexing", value_name = "ENABLE_OFFCHAIN_INDEXING")]
pub indexing_enabled: bool,
}
@@ -28,7 +28,7 @@ pub struct PruningParams {
/// Default is to keep only the last 256 blocks,
/// otherwise, the state can be kept for all of the blocks (i.e 'archive'),
/// or for all of the canonical blocks (i.e 'archive-canonical').
#[clap(alias = "pruning", long, value_name = "PRUNING_MODE")]
#[arg(alias = "pruning", long, value_name = "PRUNING_MODE")]
pub state_pruning: Option<String>,
/// Specify the blocks pruning mode, a number of blocks to keep or 'archive'.
///
@@ -38,7 +38,7 @@ pub struct PruningParams {
/// or for the last N blocks (i.e a number).
///
/// NOTE: only finalized blocks are subject for removal!
#[clap(alias = "keep-blocks", long, value_name = "COUNT")]
#[arg(alias = "keep-blocks", long, value_name = "COUNT")]
pub blocks_pruning: Option<String>,
}
@@ -28,25 +28,25 @@ pub struct SharedParams {
///
/// It can be one of the predefined ones (dev, local, or staging) or it can be a path to a file
/// with the chainspec (such as one exported by the `build-spec` subcommand).
#[clap(long, value_name = "CHAIN_SPEC")]
#[arg(long, value_name = "CHAIN_SPEC")]
pub chain: Option<String>,
/// Specify the development chain.
///
/// This flag sets `--chain=dev`, `--force-authoring`, `--rpc-cors=all`,
/// `--alice`, and `--tmp` flags, unless explicitly overridden.
#[clap(long, conflicts_with_all = &["chain"])]
#[arg(long, conflicts_with_all = &["chain"])]
pub dev: bool,
/// Specify custom base path.
#[clap(long, short = 'd', value_name = "PATH", parse(from_os_str))]
#[arg(long, short = 'd', value_name = "PATH")]
pub base_path: Option<PathBuf>,
/// Sets a custom logging filter. Syntax is <target>=<level>, e.g. -lsync=debug.
///
/// Log levels (least to most verbose) are error, warn, info, debug, and trace.
/// By default, all targets log `info`. The global log level can be set with -l<level>.
#[clap(short = 'l', long, value_name = "LOG_PATTERN", multiple_values(true))]
#[arg(short = 'l', long, value_name = "LOG_PATTERN", num_args = 1..)]
pub log: Vec<String>,
/// Enable detailed log output.
@@ -54,11 +54,11 @@ pub struct SharedParams {
/// This includes displaying the log target, log level and thread name.
///
/// This is automatically enabled when something is logged with any higher level than `info`.
#[clap(long)]
#[arg(long)]
pub detailed_log_output: bool,
/// Disable log color output.
#[clap(long)]
#[arg(long)]
pub disable_log_color: bool,
/// Enable feature to dynamically update and reload the log filter.
@@ -68,15 +68,15 @@ pub struct SharedParams {
///
/// The `system_addLogFilter` and `system_resetLogFilter` RPCs will have no effect with this
/// option not being set.
#[clap(long)]
#[arg(long)]
pub enable_log_reloading: bool,
/// Sets a custom profiling filter. Syntax is the same as for logging: <target>=<level>
#[clap(long, value_name = "TARGETS")]
#[arg(long, value_name = "TARGETS")]
pub tracing_targets: Option<String>,
/// Receiver to process tracing messages.
#[clap(long, value_name = "RECEIVER", arg_enum, ignore_case = true, default_value = "log")]
#[arg(long, value_name = "RECEIVER", value_enum, ignore_case = true, default_value_t = TracingReceiver::Log)]
pub tracing_receiver: TracingReceiver,
}
@@ -23,15 +23,15 @@ use sc_service::config::TransactionPoolOptions;
#[derive(Debug, Clone, Args)]
pub struct TransactionPoolParams {
/// Maximum number of transactions in the transaction pool.
#[clap(long, value_name = "COUNT", default_value = "8192")]
#[arg(long, value_name = "COUNT", default_value_t = 8192)]
pub pool_limit: usize,
/// Maximum number of kilobytes of all transactions stored in the pool.
#[clap(long, value_name = "COUNT", default_value = "20480")]
#[arg(long, value_name = "COUNT", default_value_t = 20480)]
pub pool_kbytes: usize,
/// How long a transaction is banned for, if it is considered invalid. Defaults to 1800s.
#[clap(long, value_name = "SECONDS")]
#[arg(long, value_name = "SECONDS")]
pub tx_ban_seconds: Option<u64>,
}