๐ Overview
flexiargs is a lightweight and dependency-friendly Rust crate designed for rule-based
command-line argument parsing without relying on macros, derive systems, or bloated
abstractions.
Instead of hiding behavior behind procedural magic, flexiargs provides a clean and explicit API that allows developers to bind CLI arguments directly to variables while retaining full control over parsing logic and application flow.
Built with simplicity and flexibility in mind, it gives you low-level control when you need it, without sacrificing ergonomics. With flexiargs, you can easily manage:
-
โ๏ธ Parsing behavior and argument rules;
-
โ Validation and constraint handling;
-
๐ฆ Unmatched or forwarded arguments;
-
๐ Execution flow and command dispatching;
-
๐งฉ Custom CLI architectures and dynamic behaviors;
Unlike heavy CLI frameworks, flexiargs focuses on predictable behavior, small footprint, and straightforward integration, making it ideal for projects where control and portability matter. Perfect for:
-
๐ฆ Package managers;
-
๐ ๏ธ System utilities;
-
๐ฅ Installers and bootstrap tools;
-
๐งฉ Embedded CLI environments;
-
๐ Custom shell-like applications;
-
๐งช Internal developer tooling;
-
โก Lightweight standalone binaries;
-
๐ Experimental runtime environments;
โจ Features
flexiargs is designed to keep CLI parsing simple, explicit, and predictable, without relying on macros or heavy abstractions. Instead of hiding behavior behind complex frameworks, it exposes clear rule-based control over how arguments are interpreted and processed.
-
๐งฉ Simple rule-based parser API
Defines parsing rules in a declarative way, without DSLs or code generation. You have full control over how each argument is interpreted. -
๐ซ No procedural macros
No dependency onderiveor procedural macros. This reduces compile time, avoids hidden behavior, and improves debugging clarity. -
๐ Supports short and long flags
Full support for both short flags (-v,-h) and long flags (--verbose,--help) for flexible CLI design. -
๐ Supports argument formats
--flag=value
Inline assignment for compact CLI usage.--flag value
Classic POSIX-style separation for readability in interactive usage.
-
๐ Typed parsing with
FromStr
Automatically converts string inputs into Rust types using theFromStrtrait, ensuring type safety and reducing manual parsing code. -
โ ๏ธ Automatic error formatting
Parsing errors are automatically formatted in a clear and consistent way, improving end-user feedback. -
๐ Optional and required arguments
Explicit support for required and optional parameters, removing the need for manual validation logic. -
๐ Multi-value collection
Support for multiple values for the same flag (e.g.--file a b c), collected into typed containers. -
๐ฅ Positional argument passthrough
Positional arguments can be captured or forwarded directly to subprocesses or higher-level handlers. -
๐ Strict validation modes
Enables strict parsing mode to reject unknown or malformed arguments, ideal for robust tools and system utilities. -
๐งต Thread-safe shared settings (
RwLock)
Safe shared state across threads usingRwLock, useful for concurrent CLI applications or embedded runtimes. -
โก Custom actions/callbacks
Allows execution of custom callbacks during parsing for dynamic or context-aware behavior. -
๐ชถ Minimal and dependency-light design
Keeps the core lightweight with minimal dependencies, focusing on portability and predictable behavior.
๐ฆ Installation
Add the crate to your Cargo.toml:
[]
= "2.0"
๐ Public API
The crate intentionally exposes a very small API surface:
pub use ;
pub use ArgHelp;
pub use ParserOptions;
pub use ;
๐ช Main Entry Points
| Item | Description |
|---|---|
Arg |
Defines parsing rules |
parse_into_vars |
Executes parsing with ParserOptions |
ParserOptions |
Configuration for parsing behavior |
invalid_arg |
Standardized invalid argument error |
missing_arg |
Standardized missing parameter error |
๐ Quick Example
use ;
use VecDeque;
let mut sync_mode = false;
let mut packages = Vecnew;
let mut cache_dir = Stringnew;
let mut config_file: = None;
let mut use_overlay = true;
let opts = ParserOptions ;
let mut rules = ;
let args = from;
parse_into_vars.ok;
drop;
println!;
println!;
println!;
println!;
๐ง Core Concepts
๐ Parsing Rules
Every CLI behavior is defined using an Arg.
Each rule describes:
- ๐ท๏ธ Accepted flags;
- โ๏ธ Parsing behavior;
- ๐ฏ Target variable;
- โ Validation requirements;
Example:
bool
๐งฑ Supported Rule Types
flexiargs is built around a small set of explicit rule types that define how command-line
input is interpreted, validated, and transformed. Instead of relying on implicit behavior
or hidden conventions, each rule type has a clear and predictable responsibility,
allowing you to compose CLI behavior in a controlled and deterministic way.
๐ฉ Boolean Flags
Sets a boolean to true when matched.
let mut verbose = false;
bool;
โ Supports
-v--verbose
๐ข Typed Values
Parses values using FromStr.
let mut port: u16 = 0;
value;
โ Supports
--port 8080--port=8080-p 8080
โ Optional Values
Stores values in Option<String>.
let mut config: = None;
option;
๐ง Fixed State Assignment
Assigns a predefined value when matched.
let mut overlay = true;
set;
๐ Multi-Value Collection
Collects sequential positional values until another flag appears.
let mut packages = Vecnew;
collect_list;
๐งช Example
๐ค Result
โก Custom Actions
Executes arbitrary logic.
action;
๐ก Useful for
- ๐ Version output
- ๐ ๏ธ Custom handlers
- ๐ช Early exits
- ๐ Dynamic state manipulation
โ Required Arguments
Arguments can be marked as essential:
value.essential;
If no essential rule is matched:
myapp: setup: no essential parameter specified
๐งต Thread-Safe Global State
flexiargs includes built-in support for shared application state using RwLock.
๐ฉ Shared Boolean Flags
use RwLock;
static DEBUG: = new;
rw_bool;
๐ข Shared Typed Values
use RwLock;
static PORT: = new;
rw_value;
๐ง Shared Fixed Assignment
rw_set;
๐ Environment Variables
flexiargs supports documentation and parsing of environment variables,
allowing configuration via the environment.
env,
๐ Parsing
Parsing in flexiargs is explicit, deterministic, and fully rule-driven. Each step of the parsing process is defined by clear rules that transform raw command-line input into structured, validated, and typed data.
๐ฅ Basic Parsing
let opts = ParserOptions ;
parse_into_vars.ok?;
๐ฆ ParseResult
The parser returns a ParseResult. This structure provides a clean,
streamlined interface for controlling your CLI's execution flow:
- โ Result Handling: Encapsulates the success or failure of the parsing logic.
- ๐ Automatic Help/Version: Handles
--help,--version, and--help-allautomatically, including output generation and exit state tracking. - ๐ Execution Flow: Provides a fluent API to bridge the gap between parsing and your main application logic.
โ .help_or_err()
The primary method to control execution flow. It automatically handles help flags, returns errors if parsing failed, and indicates if the application should exit after showing help.
match parse_into_vars.help_or_err
๐ .ok()
Extracts the raw parsing result, useful if you need to handle the
Result<(), Box<dyn Error>> manually without the built-in help/error logic.
parse_into_vars.ok?;
๐ Positional Arguments
The parser supports -- to stop option parsing.
๐งช Example
Everything after -- becomes positional data.
โ ๏ธ Error Messages
flexiargs provides standardized and human-readable errors automatically.
โ Invalid arguments
myapp: invalid argument '--unknown'
Use 'myapp --help' to see available options.
โ Missing values
myapp: setup: --port requires a <port>.
Usage: myapp setup --port <port>
๐ฏ Argument Matching
Rules in flexiargs define how input tokens are interpreted and matched against declared CLI arguments. This matching system is flexible but explicit, allowing multiple naming styles and aliasing strategies while keeping behavior predictable and rule-based. Rules support:
- ๐ค Short flags;
- ๐ท๏ธ Long flags;
- ๐ Aliases via
|; - ๐งท Inline assignment;
๐งช Example
bool;
โ Support
-v--verbose--debug
Argument matching in flexiargs is designed to be both flexible and explicit. Instead of enforcing a single naming convention, it allows multiple identifiers and aliases per argument while keeping resolution deterministic and rule-based.
โ๏ธ ParserOptions
ParserOptions is the central configuration structure for flexiargs. It replaces
direct function parameters with a robust, extensible configuration object,
allowing you to fine-tune parsing behavior, validation rules, and context.
๐ Definition
๐ก Why use ParserOptions?
- Centralized Control: Configure all parsing aspects (strictness, collection,
requirements) in one place before invoking
parse_into_vars. - Cleaner API: Keeps the
parse_into_varssignature stable even as you add new features. - Fluent Setup: Easily derive from
Defaultand override only the fields you need.
๐ Usage Example
let mut remaining = new;
let opts = ParserOptions ;
parse_into_vars.help_or_err?;
๐ค AutoHelp System
flexiargs provides an automated help generation system that maps your defined rules
and metadata into a structured, readable documentation output. Instead of maintaining
a separate manual, you define the application properties and command context directly
in your code.
๐ฆ App Properties
Defines the core metadata for the application, such as name, version, and a brief description.
properties;
๐ ๏ธ Subcommand Definition
Documents a subcommand. Use this to organize complex CLI tools into logical groups.
subcommand;
๐ Argument Documentation
Documents a specific argument. You can add metadata strings (like <DIR>) to improve clarity.
arg
.meta;
๐ Environment Variable Documentation
Documents environment variables that influence the application behavior, ensuring they appear in the generated help output.
env;
๐ Contextual Grouping
Limits the visibility of an argument to specific subcommands. This is essential
when generating full help outputs with --help-all.
arg
.context;
๐ค Contributing
Contributions, improvements, and issue reports are welcome.
๐ง Possible Future Extensions
- ๐ Shell completion generation
๐ MIT License
This repository has scripts created to be free software.
Therefore, they can be distributed and/or modified within the terms of the MIT License.
See the MIT License file for details.
๐ฌ Contact & Support
- ๐ง Email: m10ferrari1200@gmail.com
- ๐ง Email: contatolinuxdicaspro@gmail.com