Flag-rs - A Cobra-inspired CLI Framework for Rust
Flag-rs is a command-line interface (CLI) framework for Rust inspired by Go's Cobra library. It provides dynamic command completion, self-registering commands, and a clean, modular architecture for building sophisticated CLI applications.
Key Features
- Zero Dependencies - Pure Rust implementation with no external crates
- Dynamic Runtime Completions - Generate completions based on runtime state (like kubectl)
- Self-Registering Commands - Commands can register themselves with parent commands
- Modular Architecture - Organize commands in separate files/modules
- Shell Completion Support - Generate completion scripts for bash, zsh, and fish
- Hierarchical Flag Inheritance - Global flags are available to all subcommands
- Idiomatic Error Handling - Uses standard Rust
Result
types - Colored Output - Beautiful help messages with ANSI color support (respects NO_COLOR and terminal detection)
Why Flag-rs?
Flag-rs enables dynamic completions that can query APIs, databases, or any runtime
state - just like kubectl
does when completing pod names. I've struggled and
failed for a long time at modifying the leading command line processing crate to
work this way - hence this new crate.
Why Not Flag-rs?
The Flag-rs implementation may be naive - the leading crate, Clap, is very well regarded and meets the needs of a huge knowledgeable user base.
Installation
Add this to your Cargo.toml
:
[]
= "0.6"
Quick Start
use ;
Dynamic Completions
The killer feature of Flag-rs is dynamic completions. Unlike static completions, these run at completion time and can return different values based on current state:
.arg_completion
.build
new
Completions with Descriptions
Flag-rs supports rich completions with descriptions, similar to Cobra. When descriptions are provided, they are handled appropriately by each shell:
.flag_completion
Shell-specific behavior:
- Bash: Shows only the completion values (descriptions not supported natively)
- Zsh: Shows descriptions using native format:
value:description
- Fish: Shows descriptions using tab-separated format:
value[TAB]description
For Zsh and Fish, descriptions appear alongside completions in the shell's native format, providing helpful context when selecting options.
Modular Command Structure
For larger applications, Flag supports a modular structure where commands register themselves:
// src/cmd/mod.rs
// src/cmd/serve.rs
Shell Completions
Generate completion scripts for popular shells:
// Add a completion command to your CLI
new
.short
.run
.build
Then users can enable completions:
# Bash
# Zsh
# Fish
|
Flag Types
Flag-rs supports multiple value types:
String
- Text valuesBool
- Boolean flagsInt
- Integer valuesFloat
- Floating point valuesStringSlice
- Multiple string values
Error Handling
Flag uses idiomatic Rust error handling with no forced dependencies:
Examples
See the examples/
directory for complete examples:
kubectl.rs
- A simple kubectl-like CLI demonstrating dynamic completionskubectl_modular/
- A modular kubectl implementation showing command self-registration
Design Philosophy
Flag is designed to be a foundational library with zero dependencies. This ensures:
- Fast compilation times
- No dependency conflicts
- Maximum flexibility for users
- Minimal binary size
Users can add their own dependencies for features like colored output, async runtime, or configuration file support without conflicts.
Development
Running Clippy Locally
Flag uses opinionated clippy linting to maintain code quality. Here are the most useful commands:
# Basic clippy check
# Strict clippy with all lints (what CI uses)
# Fix clippy warnings automatically
# Check specific target
Continuous Integration
The project uses GitHub Actions for CI with:
- Rust stable toolchain
- Format checking (
cargo fmt
) - Comprehensive clippy linting
- Test execution
- Documentation build verification
The clippy configuration in CI is very strict to catch potential issues early.
See .github/workflows/ci.yml
for the full configuration.
Publishing Releases
To publish a new release to crates.io:
- Update the version in
Cargo.toml
- Commit and push the version change
- Create a GitHub release with a tag like
v0.6.0
(matching the Cargo.toml version) - GitHub Actions will automatically publish to crates.io
Setup Required:
- Add your crates.io API token as a GitHub secret named
CRATES_IO_TOKEN
- Get your token from: https://crates.io/settings/tokens
License
MIT