Skip to content

Commit

Permalink
docs(cookbook): Style cargo plugin
Browse files Browse the repository at this point in the history
Inspired by part of the conversation at clap-rs#3234
  • Loading branch information
epage committed Aug 8, 2024
1 parent ecb4dca commit 65625b0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ humantime = "2.1.0"
snapbox = "0.6.16"
shlex = "1.3.0"
automod = "1.0.14"
clap-cargo = { version = "0.14.1", default-features = false }

[[example]]
name = "demo"
Expand Down
11 changes: 11 additions & 0 deletions examples/cargo-example-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ use clap::Parser;
#[derive(Parser)] // requires `derive` feature
#[command(name = "cargo")]
#[command(bin_name = "cargo")]
#[command(styles = CLAP_STYLING)]
enum CargoCli {
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)]
#[command(version, about, long_about = None)]
struct ExampleDeriveArgs {
Expand Down
11 changes: 11 additions & 0 deletions examples/cargo-example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fn main() {
let cmd = clap::Command::new("cargo")
.bin_name("cargo")
.styles(CLAP_STYLING)
.subcommand_required(true)
.subcommand(
clap::command!("example").arg(
Expand All @@ -16,3 +17,13 @@ fn main() {
let manifest_path = matches.get_one::<std::path::PathBuf>("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);
1 change: 1 addition & 0 deletions src/_cookbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! - Topics:
//! - Subcommands
//! - Cargo plugins
//! - custom terminal [styles][crate::Command::styles] (colors)
//!
//! find-like interface: [builder][find]
//! - Topics:
Expand Down

0 comments on commit 65625b0

Please sign in to comment.