mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
docs(cookbook): Style cargo plugin
Inspired by part of the conversation at #3234
This commit is contained in:
parent
ecb4dca96f
commit
631e54bc71
5 changed files with 37 additions and 3 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -413,6 +413,7 @@ name = "clap"
|
||||||
version = "4.5.13"
|
version = "4.5.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"automod",
|
"automod",
|
||||||
|
"clap-cargo",
|
||||||
"clap_builder 4.5.13",
|
"clap_builder 4.5.13",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
"humantime",
|
"humantime",
|
||||||
|
@ -423,6 +424,15 @@ dependencies = [
|
||||||
"trycmd",
|
"trycmd",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap-cargo"
|
||||||
|
version = "0.14.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "23b2ea69cefa96b848b73ad516ad1d59a195cdf9263087d977f648a818c8b43e"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_bench"
|
name = "clap_bench"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
|
@ -1432,7 +1442,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d"
|
checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"windows-targets 0.48.5",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -187,6 +187,7 @@ humantime = "2.1.0"
|
||||||
snapbox = "0.6.16"
|
snapbox = "0.6.16"
|
||||||
shlex = "1.3.0"
|
shlex = "1.3.0"
|
||||||
automod = "1.0.14"
|
automod = "1.0.14"
|
||||||
|
clap-cargo = { version = "0.14.1", default-features = false }
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "demo"
|
name = "demo"
|
||||||
|
@ -194,11 +195,11 @@ required-features = ["derive"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "cargo-example"
|
name = "cargo-example"
|
||||||
required-features = ["cargo"]
|
required-features = ["cargo", "color"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "cargo-example-derive"
|
name = "cargo-example-derive"
|
||||||
required-features = ["derive"]
|
required-features = ["derive", "color"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "escaped-positional"
|
name = "escaped-positional"
|
||||||
|
|
|
@ -3,10 +3,21 @@ use clap::Parser;
|
||||||
#[derive(Parser)] // requires `derive` feature
|
#[derive(Parser)] // requires `derive` feature
|
||||||
#[command(name = "cargo")]
|
#[command(name = "cargo")]
|
||||||
#[command(bin_name = "cargo")]
|
#[command(bin_name = "cargo")]
|
||||||
|
#[command(styles = CLAP_STYLING)]
|
||||||
enum CargoCli {
|
enum CargoCli {
|
||||||
ExampleDerive(ExampleDeriveArgs),
|
ExampleDerive(ExampleDeriveArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See also `clap_cargo::style::CLAP_STYLING`
|
||||||
|
pub const CLAP_STYLING: clap::builder::styling::Styles = clap::builder::styling::Styles::styled()
|
||||||
|
.header(clap_cargo::style::HEADER)
|
||||||
|
.usage(clap_cargo::style::USAGE)
|
||||||
|
.literal(clap_cargo::style::LITERAL)
|
||||||
|
.placeholder(clap_cargo::style::PLACEHOLDER)
|
||||||
|
.error(clap_cargo::style::ERROR)
|
||||||
|
.valid(clap_cargo::style::VALID)
|
||||||
|
.invalid(clap_cargo::style::INVALID);
|
||||||
|
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct ExampleDeriveArgs {
|
struct ExampleDeriveArgs {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let cmd = clap::Command::new("cargo")
|
let cmd = clap::Command::new("cargo")
|
||||||
.bin_name("cargo")
|
.bin_name("cargo")
|
||||||
|
.styles(CLAP_STYLING)
|
||||||
.subcommand_required(true)
|
.subcommand_required(true)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
clap::command!("example").arg(
|
clap::command!("example").arg(
|
||||||
|
@ -16,3 +17,13 @@ fn main() {
|
||||||
let manifest_path = matches.get_one::<std::path::PathBuf>("manifest-path");
|
let manifest_path = matches.get_one::<std::path::PathBuf>("manifest-path");
|
||||||
println!("{manifest_path:?}");
|
println!("{manifest_path:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See also `clap_cargo::style::CLAP_STYLING`
|
||||||
|
pub const CLAP_STYLING: clap::builder::styling::Styles = clap::builder::styling::Styles::styled()
|
||||||
|
.header(clap_cargo::style::HEADER)
|
||||||
|
.usage(clap_cargo::style::USAGE)
|
||||||
|
.literal(clap_cargo::style::LITERAL)
|
||||||
|
.placeholder(clap_cargo::style::PLACEHOLDER)
|
||||||
|
.error(clap_cargo::style::ERROR)
|
||||||
|
.valid(clap_cargo::style::VALID)
|
||||||
|
.invalid(clap_cargo::style::INVALID);
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
//! - Topics:
|
//! - Topics:
|
||||||
//! - Subcommands
|
//! - Subcommands
|
||||||
//! - Cargo plugins
|
//! - Cargo plugins
|
||||||
|
//! - custom terminal [styles][crate::Command::styles] (colors)
|
||||||
//!
|
//!
|
||||||
//! find-like interface: [builder][find]
|
//! find-like interface: [builder][find]
|
||||||
//! - Topics:
|
//! - Topics:
|
||||||
|
|
Loading…
Reference in a new issue