Skip to content

Commit fe9918a

Browse files
Clap 3 → Clap 4 Upgrade (#3431)
* Upgrade to Clap 4 Signed-off-by: Brian Hardock <brian.hardock@fermyon.com> * Remove vendored clap_markdown Signed-off-by: Brian Hardock <brian.hardock@fermyon.com> * Reintroduce cmd argument for external Signed-off-by: Brian Hardock <brian.hardock@fermyon.com> --------- Signed-off-by: Brian Hardock <brian.hardock@fermyon.com>
1 parent 9cdf859 commit fe9918a

26 files changed

Lines changed: 1202 additions & 1446 deletions

File tree

Cargo.lock

Lines changed: 136 additions & 177 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ rust-version = "1.86"
1818
anyhow = { workspace = true }
1919
async-trait = { workspace = true }
2020
bytes = { workspace = true }
21-
# 'deprecated' enables deprecation warnings
22-
clap = { workspace = true, features = ["deprecated", "derive", "env"] }
21+
clap = { workspace = true, features = ["derive", "env", "string", "wrap_help"] }
22+
clap-markdown = "0.1.5"
23+
clap_lex = "0.7.5"
2324
clearscreen = "4"
2425
comfy-table = "7"
2526
command-group = { version = "5", features = ["with-tokio"] }
@@ -133,7 +134,7 @@ async-trait = "0.1"
133134
base64 = "0.22"
134135
bytes = "1.11.1"
135136
chrono = "0.4"
136-
clap = "3.2"
137+
clap = "4.6.0"
137138
conformance-tests = { git = "https://github.com/fermyon/conformance-tests", rev = "ffc2c879cdcb3c619c8f95ae8e0b847af8c8247e" }
138139
ctrlc = { version = "3.4", features = ["termination"] }
139140
dialoguer = "0.11"

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = { workspace = true }
66

77
[dependencies]
88
anyhow = { workspace = true }
9+
clap = { workspace = true }
910
dirs = { workspace = true }
1011
sha2 = { workspace = true }
1112
tempfile = { workspace = true }

crates/common/src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Common CLI code and constants
2+
3+
use clap::builder::{styling::AnsiColor, Styles};
4+
5+
/// Clap [`Styles`] for Spin CLI and plugins.
6+
pub const CLAP_STYLES: Styles = Styles::styled()
7+
.header(AnsiColor::Yellow.on_default())
8+
.usage(AnsiColor::Green.on_default())
9+
.literal(AnsiColor::Green.on_default())
10+
.placeholder(AnsiColor::Green.on_default());

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
pub mod arg_parser;
1212
pub mod assert;
13+
pub mod cli;
1314
pub mod data_dir;
1415
pub mod paths;
1516
pub mod sha256;

crates/expressions/src/template.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ mod tests {
9898
let template = Template::new(tmpl).unwrap();
9999
assert!(
100100
template.parts().eq(&expected),
101-
"{:?} -> {:?} != {:?}",
102-
tmpl,
103-
template,
104-
expected,
101+
"{tmpl:?} -> {template:?} != {expected:?}",
105102
);
106103
}
107104
}

crates/trigger-http/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub struct CliArgs {
5757
pub address: SocketAddr,
5858

5959
/// The path to the certificate to use for https, if this is not set, normal http will be used. The cert should be in PEM format
60-
#[clap(long, env = "SPIN_TLS_CERT", requires = "tls-key")]
60+
#[clap(long, env = "SPIN_TLS_CERT", requires = "tls_key")]
6161
pub tls_cert: Option<PathBuf>,
6262

6363
/// The path to the certificate key to use for https, if this is not set, normal http will be used. The key should be in PKCS#8 format
64-
#[clap(long, env = "SPIN_TLS_KEY", requires = "tls-cert")]
64+
#[clap(long, env = "SPIN_TLS_KEY", requires = "tls_cert")]
6565
pub tls_key: Option<PathBuf>,
6666

6767
/// Sets the maximum buffer size (in bytes) for the HTTP connection. The minimum value allowed is 8192.
@@ -300,6 +300,10 @@ impl<F: RuntimeFactors> Trigger<F> for HttpTrigger {
300300
fn supported_host_requirements() -> Vec<&'static str> {
301301
vec![spin_app::locked::SERVICE_CHAINING_KEY]
302302
}
303+
304+
fn display_name() -> String {
305+
"HTTP".to_string()
306+
}
303307
}
304308

305309
impl HttpTrigger {

crates/trigger/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ experimental-wasm-features = []
1818

1919
[dependencies]
2020
anyhow = { workspace = true }
21-
clap = { workspace = true, features = ["derive", "env"] }
21+
clap = { workspace = true, features = ["derive", "env", "wrap_help"] }
2222
ctrlc = { workspace = true }
2323
futures = { workspace = true }
24+
heck = { workspace = true }
2425
sanitize-filename = "0.5"
2526
serde = { workspace = true }
2627
serde_json = { workspace = true }

crates/trigger/src/cli.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{future::Future, sync::Arc};
1212
use anyhow::{Context, Result};
1313
#[cfg(feature = "experimental-wasm-features")]
1414
use clap::ValueEnum;
15-
use clap::{Args, IntoApp, Parser};
15+
use clap::{Args, CommandFactory, Parser};
1616
use spin_app::App;
1717
use spin_common::sloth;
1818
use spin_common::ui::quoted_path;
@@ -45,6 +45,7 @@ pub const SPIN_WORKING_DIR: &str = "SPIN_WORKING_DIR";
4545
/// A command that runs a TriggerExecutor.
4646
#[derive(Parser, Debug)]
4747
#[clap(
48+
styles = spin_common::cli::CLAP_STYLES,
4849
override_usage = "spin [COMMAND] [OPTIONS]",
4950
next_help_heading = help_heading::<T, B::Factors>()
5051
)]
@@ -72,7 +73,6 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
7273
long = "disable-cache",
7374
env = DISABLE_WASMTIME_CACHE,
7475
conflicts_with = WASMTIME_CACHE_FILE,
75-
takes_value = false,
7676
)]
7777
pub disable_cache: bool,
7878

@@ -91,7 +91,7 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
9191

9292
/// Enable Wasmtime's debug info for Wasm guests, allowing debugging
9393
/// with gdb or lldb.
94-
#[clap(long = "debug-info", takes_value = false)]
94+
#[clap(long = "debug-info")]
9595
pub debug_info: bool,
9696

9797
/// Print output to stdout/stderr only for given component(s)
@@ -330,9 +330,9 @@ fn warn_if_wasm_build_slothful() -> sloth::SlothGuard {
330330

331331
fn help_heading<T: Trigger<F>, F: RuntimeFactors>() -> Option<&'static str> {
332332
if T::TYPE == <help::HelpArgsOnlyTrigger as Trigger<F>>::TYPE {
333-
Some("TRIGGER OPTIONS")
333+
Some("Trigger Options")
334334
} else {
335-
let heading = format!("{} TRIGGER OPTIONS", T::TYPE.to_uppercase());
335+
let heading = format!("{} Trigger Options", T::display_name());
336336
let as_str = Box::new(heading).leak();
337337
Some(as_str)
338338
}

crates/trigger/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod cli;
22
pub mod loader;
33

4+
use heck::ToTitleCase;
45
use std::future::Future;
56

67
use clap::Args;
@@ -71,4 +72,9 @@ pub trait Trigger<F: RuntimeFactors>: Sized + Send {
7172
fn supported_host_requirements() -> Vec<&'static str> {
7273
Vec::new()
7374
}
75+
76+
/// Returns the display name for the type of this trigger. Defaults to title case.
77+
fn display_name() -> String {
78+
Self::TYPE.to_title_case()
79+
}
7480
}

0 commit comments

Comments
 (0)