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 v4 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
Installation
[]
= "1.0"
= { = "4.4", = ["derive"] }
Quick Start
use ;
use ;
Run with --version to see:
myapp v1.0.0 by Author 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;
// Use custom hex colors
let version = colorful_version!;
Advanced Usage
Using the Extension Trait
use ;
use ;
RGB Colors
use ColorfulVersion;
let version = from_cargo
.with_rgb_colors;
API Reference
Main Types
ColorfulVersion: Configuration for colorful version outputVersionError: Error type for color parsing and I/O errorsColorfulVersionExt: Extension trait forclap::Command
Key Methods
ColorfulVersion::from_cargo(): Create from Cargo.toml env varsColorfulVersion::with_hex_colors(): Set custom hex colorsColorfulVersion::print_and_exit(): Print version and exitparse_with_version(): Parse clap arguments with version handling
Macros
colorful_version!(): CreateColorfulVersionwith optional custom colors
Feature Flags
no-color: Disable colored output (uses plain text only)derive: Enable derive macro support (requiresclap/derive)
Testing
# Run all tests
# Test without color support
NO_COLOR=1
# Test examples
Error Handling
All errors are properly typed using thiserror:
VersionError::InvalidHexColor: Invalid hex color formatVersionError::IoError: I/O errors during printing
Contributing
Contributions are welcome! Please ensure all code:
- Passes
cargo fmtandcargo clippy - Includes appropriate tests
- Updates documentation as needed
License
Dual-licensed under either:
- MIT License (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
Acknowledgments
- Built on top of the excellent clap crate
- Uses colored for terminal colors
- Inspired by the need for more visually distinct CLI version output
Author
Hadi Cahyadi
- Email: cumulus13@gmail.com
- GitHub: cumulus13
