Skip to content

Commit 631e54b

Browse files
committed
docs(cookbook): Style cargo plugin
Inspired by part of the conversation at #3234
1 parent ecb4dca commit 631e54b

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

Cargo.lock

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,19 @@ humantime = "2.1.0"
187187
snapbox = "0.6.16"
188188
shlex = "1.3.0"
189189
automod = "1.0.14"
190+
clap-cargo = { version = "0.14.1", default-features = false }
190191

191192
[[example]]
192193
name = "demo"
193194
required-features = ["derive"]
194195

195196
[[example]]
196197
name = "cargo-example"
197-
required-features = ["cargo"]
198+
required-features = ["cargo", "color"]
198199

199200
[[example]]
200201
name = "cargo-example-derive"
201-
required-features = ["derive"]
202+
required-features = ["derive", "color"]
202203

203204
[[example]]
204205
name = "escaped-positional"

examples/cargo-example-derive.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@ use clap::Parser;
33
#[derive(Parser)] // requires `derive` feature
44
#[command(name = "cargo")]
55
#[command(bin_name = "cargo")]
6+
#[command(styles = CLAP_STYLING)]
67
enum CargoCli {
78
ExampleDerive(ExampleDeriveArgs),
89
}
910

11+
// See also `clap_cargo::style::CLAP_STYLING`
12+
pub const CLAP_STYLING: clap::builder::styling::Styles = clap::builder::styling::Styles::styled()
13+
.header(clap_cargo::style::HEADER)
14+
.usage(clap_cargo::style::USAGE)
15+
.literal(clap_cargo::style::LITERAL)
16+
.placeholder(clap_cargo::style::PLACEHOLDER)
17+
.error(clap_cargo::style::ERROR)
18+
.valid(clap_cargo::style::VALID)
19+
.invalid(clap_cargo::style::INVALID);
20+
1021
#[derive(clap::Args)]
1122
#[command(version, about, long_about = None)]
1223
struct ExampleDeriveArgs {

examples/cargo-example.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fn main() {
22
let cmd = clap::Command::new("cargo")
33
.bin_name("cargo")
4+
.styles(CLAP_STYLING)
45
.subcommand_required(true)
56
.subcommand(
67
clap::command!("example").arg(
@@ -16,3 +17,13 @@ fn main() {
1617
let manifest_path = matches.get_one::<std::path::PathBuf>("manifest-path");
1718
println!("{manifest_path:?}");
1819
}
20+
21+
// See also `clap_cargo::style::CLAP_STYLING`
22+
pub const CLAP_STYLING: clap::builder::styling::Styles = clap::builder::styling::Styles::styled()
23+
.header(clap_cargo::style::HEADER)
24+
.usage(clap_cargo::style::USAGE)
25+
.literal(clap_cargo::style::LITERAL)
26+
.placeholder(clap_cargo::style::PLACEHOLDER)
27+
.error(clap_cargo::style::ERROR)
28+
.valid(clap_cargo::style::VALID)
29+
.invalid(clap_cargo::style::INVALID);

src/_cookbook/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! - Topics:
1616
//! - Subcommands
1717
//! - Cargo plugins
18+
//! - custom terminal [styles][crate::Command::styles] (colors)
1819
//!
1920
//! find-like interface: [builder][find]
2021
//! - Topics:

0 commit comments

Comments
 (0)