| Crates.io | clap-version-flag |
| lib.rs | clap-version-flag |
| version | 1.0.7 |
| created_at | 2025-12-11 18:04:25.338469+00 |
| updated_at | 2025-12-12 02:12:53.910834+00 |
| description | colorful version handler for clap |
| homepage | https://github.com/cumulus13/clap-version-flag |
| repository | https://github.com/cumulus13/clap-version-flag |
| max_upload_size | |
| id | 1980225 |
| size | 162,904 |
Adding colorful version output to clap applications.
#RRGGBB or #RGB) for all text elementsno-color feature for environments without color support
🔧 Flexible integration - Works with clap derive and builder patterns
🚀 Zero-config defaults - Beautiful colors out of the boxAdd this to your Cargo.toml:
[dependencies]
clap = { version = "4.5", features = ["derive"] }
clap-version-flag = "1.0.5"
use clap::Parser;
use clap_version_flag::colorful_version;
#[derive(Parser)]
#[command(name = "myapp")]
struct Cli {
#[arg(short, long)]
input: String,
}
fn main() {
// Create version info from your Cargo.toml
let version = colorful_version!();
// Parse arguments
let cli = Cli::parse();
// Your app logic here
println!("Input: {}", cli.input);
}
When a user runs myapp --version, they'll see:
myapp v1.0.0 by Your Name
With colors (when terminal supports it):
myapp: White text (#FFFFFF) on purple background (#AA00FF)v1.0.0: Yellow text (#FFFF00)by Author Name: Cyan text (#00FFFF)use clap_version_flag::colorful_version;
fn main() {
// Custom hex colors: name_fg, name_bg, version, author
let version = colorful_version!(
"#FF0000", // Red foreground for name
"#0000FF", // Blue background for name
"#00FF00", // Green for version
"#FFFF00" // Yellow for author
);
version.print();
}
use clap_version_flag::ColorfulVersion;
fn main() {
let version = ColorfulVersion::new("myapp", "1.0.0", "John Doe")
.with_hex_colors("#FFFFFF", "#AA00FF", "#FFFF00", "#00FFFF")
.expect("Invalid hex colors");
version.print();
}
use clap_version_flag::ColorfulVersion;
fn main() {
let version = ColorfulVersion::new("myapp", "1.0.0", "John Doe")
.with_rgb_colors(
(255, 255, 255), // White foreground
(170, 0, 255), // Purple background
(255, 255, 0), // Yellow version
(0, 255, 255) // Cyan author
);
version.print();
}
parse_with_version (Recommended)This automatically handles the --version flag:
use clap::{Parser, CommandFactory};
use clap_version_flag::{colorful_version, parse_with_version};
#[derive(Parser)]
struct Cli {
#[arg(short, long)]
name: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let version = colorful_version!();
let cli: Cli = parse_with_version(Cli::command(), &version)?;
println!("Hello, {}!", cli.name);
Ok(())
}
use clap::{Parser, CommandFactory};
use clap_version_flag::{colorful_version, ColorfulVersionExt};
#[derive(Parser)]
struct Cli {
#[arg(short, long)]
name: String,
}
fn main() {
let version = colorful_version!();
let matches = Cli::command()
.with_colorful_version(&version)
.get_matches();
// Check if version flag was used
version.check_and_exit(&matches);
// Continue with normal parsing
let cli = Cli::from_arg_matches(&matches).unwrap();
println!("Hello, {}!", cli.name);
}
use clap::Command;
use clap_version_flag::{colorful_version, ColorfulVersionExt};
fn main() {
let version = colorful_version!();
let matches = Command::new("myapp")
.with_colorful_version(&version)
.arg(clap::arg!(-n --name <NAME> "Your name"))
.get_matches();
version.check_and_exit(&matches);
if let Some(name) = matches.get_one::<String>("name") {
println!("Hello, {}!", name);
}
}
The default color scheme is designed for maximum readability:
#FFFFFF) on purple background (#AA00FF)#FFFF00)#00FFFF)colorful_version!()Macro to create a ColorfulVersion using information from your Cargo.toml.
Important: This macro uses env!() which expands at the caller's location, so it correctly picks up your package information, not this library's.
// With default colors
let version = colorful_version!();
// With custom colors
let version = colorful_version!("#FFFFFF", "#AA00FF", "#FFFF00", "#00FFFF");
ColorfulVersionMain struct for version configuration.
new(name, version, author) - Create with custom valueswith_hex_colors(name_fg, name_bg, version, author) - Set colors using hex codeswith_rgb_colors(name_fg, name_bg, version, author) - Set colors using RGB tuplesprint() - Print colored version to stdoutprint_and_exit() - Print and exit with code 0as_plain_string() - Get plain text versionto_colored_string() - Get colored version with ANSI codespackage_name(), version(), author() - GettersColorfulVersionExtTrait extension for clap::Command.
use clap_version_flag::ColorfulVersionExt;
let cmd = Command::new("myapp").with_colorful_version(&version);
parse_with_version()Helper function to parse command-line arguments with automatic version handling.
use clap_version_flag::parse_with_version;
let cli: YourCli = parse_with_version(YourCli::command(), &version)?;
#RRGGBB (e.g., #FF0000 for red)#RGB (e.g., #F00 for red, expands to #FF0000)RRGGBB (automatically prepended)(255, 0, 0) // Red
(0, 255, 0) // Green
(0, 0, 255) // Blue
Run tests:
cargo test
Run tests with output:
cargo test -- --nocapture
Check the examples/ directory for complete working examples:
cargo run --example basic
cargo run --example custom_colors
cargo run --example derive_helper
cargo run --example full_integration
Clap's built-in --version flag outputs plain text. This crate enhances the user experience with:
Cargo.tomlcolored crate with proper terminal detectionWithout clap-version-flag:
$ myapp --version
myapp 1.0.0
With clap-version-flag:
$ myapp --version
myapp v1.0.0 by Your Name
(But with beautiful colors that respect your terminal theme!)
The colorful_version!() macro uses Rust's env!() macro which expands at compile time at the caller's location. This means:
colorful_version!() in your projectCARGO_PKG_NAME, CARGO_PKG_VERSION, and CARGO_PKG_AUTHORSThis is why the macro correctly picks up your package information!
Cause: Using an old version that had the bug.
Solution: Update to version 1.0.4 or later:
clap-version-flag = "1.0.4"
Cause: Need to disable clap's built-in version flag.
Solution: The with_colorful_version() extension trait automatically calls .disable_version_flag(true).
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is dual-licensed under either:
at your option.
Hadi Cahyadi