๐ 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:
[]
= "1.0"
๐ Public API
The crate intentionally exposes a very small API surface:
pub use ;
pub use ;
๐ช Main Entry Points
| Item | Description |
|---|---|
Arg |
Defines parsing rules |
parse_into_vars |
Executes parsing |
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 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;
๐ 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. This makes behavior predictable, easier to debug, and consistent across different CLI designs.
๐ฅ Basic Parsing
parse_into_vars.ok?;
๐ฆ ParseResult
The parser returns a ParseResult.
This provides:
- โ Parsing success/failure;
- ๐ฆ Unmatched arguments;
- ๐ Positional handling;
- ๐ Strict validation helpers;
โ
.ok()
Extracts the parsing result.
parse_into_vars
.ok?;
๐ .strict()
Rejects any unmatched arguments.
parse_into_vars
.strict
.ok?;
๐ฅ .strict_first()
Ensures the first argument matches a rule.
.strict_first
๐ .strict_level(n)
Rejects unmatched arguments up to a given depth.
.strict_level
๐ชถ .passthrough()
Suppresses parsing errors.
Useful for optional parsing stages.
.passthrough
๐ .require_args()
Fails if no arguments were supplied.
.require_args?
๐ฆ .collect_rest()
Collects unmatched positional arguments.
let mut remaining = Vecnew;
parse_into_vars
.collect_rest?;
๐ 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.
๐ค Contributing
Contributions, improvements, and issue reports are welcome.
๐ง Possible Future Extensions
- ๐ค Auto-help generation
- ๐ Environment variable integration
- ๐ 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