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
+57 -63
View File
@@ -16,13 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Definitions of [`ArgEnum`] types.
//! Definitions of [`ValueEnum`] types.
use clap::ArgEnum;
use clap::{builder::PossibleValue, ValueEnum};
/// The instantiation strategy to use in compiled mode.
#[derive(Debug, Clone, Copy, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Clone, Copy, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum WasmtimeInstantiationStrategy {
/// Pool the instances to avoid initializing everything from scratch
/// on each instantiation. Use copy-on-write memory when possible.
@@ -59,20 +59,22 @@ pub enum WasmExecutionMethod {
Compiled,
}
impl std::fmt::Display for WasmExecutionMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Interpreted => write!(f, "Interpreted"),
Self::Compiled => write!(f, "Compiled"),
const INTERPRETED_NAME: &str = "interpreted-i-know-what-i-do";
impl clap::ValueEnum for WasmExecutionMethod {
/// All possible argument values, in display order.
fn value_variants<'a>() -> &'a [Self] {
let variants = &[Self::Interpreted, Self::Compiled];
if cfg!(feature = "wasmtime") {
variants
} else {
&variants[..1]
}
}
}
impl std::str::FromStr for WasmExecutionMethod {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
if s.eq_ignore_ascii_case("interpreted-i-know-what-i-do") {
/// Parse an argument into `Self`.
fn from_str(s: &str, _: bool) -> Result<Self, String> {
if s.eq_ignore_ascii_case(INTERPRETED_NAME) {
Ok(Self::Interpreted)
} else if s.eq_ignore_ascii_case("compiled") {
#[cfg(feature = "wasmtime")]
@@ -84,19 +86,29 @@ impl std::str::FromStr for WasmExecutionMethod {
Err("`Compiled` variant requires the `wasmtime` feature to be enabled".into())
}
} else {
Err(format!("Unknown variant `{}`, known variants: {:?}", s, Self::variants()))
Err(format!("Unknown variant `{}`", s))
}
}
/// The canonical argument value.
///
/// The value is `None` for skipped variants.
fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
#[cfg(feature = "wasmtime")]
WasmExecutionMethod::Compiled => Some(PossibleValue::new("compiled")),
#[cfg(not(feature = "wasmtime"))]
WasmExecutionMethod::Compiled => None,
WasmExecutionMethod::Interpreted => Some(PossibleValue::new(INTERPRETED_NAME)),
}
}
}
impl WasmExecutionMethod {
/// Returns all the variants of this enum to be shown in the cli.
pub fn variants() -> &'static [&'static str] {
let variants = &["interpreted-i-know-what-i-do", "compiled"];
if cfg!(feature = "wasmtime") {
variants
} else {
&variants[..1]
impl std::fmt::Display for WasmExecutionMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Interpreted => write!(f, "Interpreted"),
Self::Compiled => write!(f, "Compiled"),
}
}
}
@@ -133,15 +145,15 @@ pub fn execution_method_from_cli(
/// The default [`WasmExecutionMethod`].
#[cfg(feature = "wasmtime")]
pub const DEFAULT_WASM_EXECUTION_METHOD: &str = "compiled";
pub const DEFAULT_WASM_EXECUTION_METHOD: WasmExecutionMethod = WasmExecutionMethod::Compiled;
/// The default [`WasmExecutionMethod`].
#[cfg(not(feature = "wasmtime"))]
pub const DEFAULT_WASM_EXECUTION_METHOD: &str = "interpreted-i-know-what-i-do";
pub const DEFAULT_WASM_EXECUTION_METHOD: WasmExecutionMethod = WasmExecutionMethod::Interpreted;
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum TracingReceiver {
/// Output the tracing records using the log.
Log,
@@ -156,16 +168,16 @@ impl Into<sc_tracing::TracingReceiver> for TracingReceiver {
}
/// The type of the node key.
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum NodeKeyType {
/// Use ed25519.
Ed25519,
}
/// The crypto scheme to use.
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum CryptoScheme {
/// Use ed25519.
Ed25519,
@@ -176,8 +188,8 @@ pub enum CryptoScheme {
}
/// The type of the output format.
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum OutputType {
/// Output as json.
Json,
@@ -186,8 +198,8 @@ pub enum OutputType {
}
/// How to execute blocks
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum ExecutionStrategy {
/// Execute with native build (if available, WebAssembly otherwise).
Native,
@@ -212,8 +224,8 @@ impl Into<sc_client_api::ExecutionStrategy> for ExecutionStrategy {
/// Available RPC methods.
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Copy, Clone, PartialEq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum RpcMethods {
/// Expose every RPC method only when RPC is listening on `localhost`,
/// otherwise serve only safe RPC methods.
@@ -235,7 +247,8 @@ impl Into<sc_service::config::RpcMethods> for RpcMethods {
}
/// Database backend
#[derive(Debug, Clone, PartialEq, Copy)]
#[derive(Debug, Clone, PartialEq, Copy, clap::ValueEnum)]
#[value(rename_all = "lower")]
pub enum Database {
/// Facebooks RocksDB
#[cfg(feature = "rocksdb")]
@@ -246,29 +259,10 @@ pub enum Database {
/// instance of ParityDb
Auto,
/// ParityDb. <https://github.com/paritytech/parity-db/>
#[value(name = "paritydb-experimental")]
ParityDbDeprecated,
}
impl std::str::FromStr for Database {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
#[cfg(feature = "rocksdb")]
if s.eq_ignore_ascii_case("rocksdb") {
return Ok(Self::RocksDb)
}
if s.eq_ignore_ascii_case("paritydb-experimental") {
return Ok(Self::ParityDbDeprecated)
} else if s.eq_ignore_ascii_case("paritydb") {
return Ok(Self::ParityDb)
} else if s.eq_ignore_ascii_case("auto") {
Ok(Self::Auto)
} else {
Err(format!("Unknown variant `{}`, known variants: {:?}", s, Self::variants()))
}
}
}
impl Database {
/// Returns all the variants of this enum to be shown in the cli.
pub const fn variants() -> &'static [&'static str] {
@@ -284,8 +278,8 @@ impl Database {
/// Whether off-chain workers are enabled.
#[allow(missing_docs)]
#[derive(Debug, Clone, ArgEnum)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Clone, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum OffchainWorkerEnabled {
/// Always have offchain worker enabled.
Always,
@@ -296,8 +290,8 @@ pub enum OffchainWorkerEnabled {
}
/// Syncing mode.
#[derive(Debug, Clone, Copy, ArgEnum, PartialEq)]
#[clap(rename_all = "kebab-case")]
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq)]
#[value(rename_all = "kebab-case")]
pub enum SyncMode {
/// Full sync. Download end verify all blocks.
Full,