clap-version-flag
Adding colorful version output to clap applications.
Features
- 🎨 Hex Color Support: Use hex color codes (
#RRGGBBor#RGB) for all text elements - 🔧 clap Integration: Seamlessly works with clap's derive and builder APIs
- 📦 Automatic Cargo.toml Detection: Reads package info from environment variables
- 🚀 Production Ready: Comprehensive error handling, testing, and documentation
- 🌈 Graceful Fallback: Works in terminals with and without color support
- ⚙️ Feature Flags: Optional
no-colorfeature for environments without color support 🔧 Flexible integration - Works with clap derive and builder patterns 🚀 Zero-config defaults - Beautiful colors out of the box
Installation
Add this to your Cargo.toml:
[]
= { = "4.5", = ["derive"] }
= "1.0.5"
Quick Start
Basic Usage
use Parser;
use colorful_version;
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)
Custom Colors
use colorful_version;
Manual Version Creation
use ColorfulVersion;
Using RGB Colors
use ColorfulVersion;
Integration with Clap
Method 1: Using parse_with_version (Recommended)
This automatically handles the --version flag:
use ;
use ;
Method 2: Manual Flag Checking
use ;
use ;
Method 3: Builder Pattern
use Command;
use ;
Default Colors
The default color scheme is designed for maximum readability:
- Package Name: White text (
#FFFFFF) on purple background (#AA00FF) - Version: Yellow text (
#FFFF00) - Author: Cyan text (
#00FFFF)
API Reference
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!;
ColorfulVersion
Main struct for version configuration.
Methods
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()- Getters
ColorfulVersionExt
Trait extension for clap::Command.
use ColorfulVersionExt;
let cmd = new.with_colorful_version;
parse_with_version()
Helper function to parse command-line arguments with automatic version handling.
use parse_with_version;
let cli: YourCli = parse_with_version?;
Color Format Support
Hex Colors
- 6-digit:
#RRGGBB(e.g.,#FF0000for red) - 3-digit:
#RGB(e.g.,#F00for red, expands to#FF0000) - Without #:
RRGGBB(automatically prepended)
RGB Tuples
// Red
// Green
// Blue
Testing
Run tests:
Run tests with output:
Examples
Check the examples/ directory for complete working examples:
Why This Crate?
Clap's built-in --version flag outputs plain text. This crate enhances the user experience with:
- Professional appearance - Colored output makes your CLI tool stand out
- Consistent branding - Use your brand colors in the version output
- Zero boilerplate - Automatic extraction from
Cargo.toml - Terminal compatibility - Uses
coloredcrate with proper terminal detection
Comparison
Without 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!)
How It Works
The colorful_version!() macro uses Rust's env!() macro which expands at compile time at the caller's location. This means:
- When you use
colorful_version!()in your project - The macro expands to read
CARGO_PKG_NAME,CARGO_PKG_VERSION, andCARGO_PKG_AUTHORS - These environment variables are set by Cargo during compilation
- They contain your project's information, not this library's
This is why the macro correctly picks up your package information!
Common Issues
Issue: Wrong package name displayed
Cause: Using an old version that had the bug.
Solution: Update to version 1.0.4 or later:
= "1.0.4"
Issue: Version flag not working
Cause: Need to disable clap's built-in version flag.
Solution: The with_colorful_version() extension trait automatically calls .disable_version_flag(true).
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Acknowledgments
Author
Hadi Cahyadi
- Email: cumulus13@gmail.com
- GitHub: cumulus13
