https://github.com/sile/noargs
Imperative command-line argument parser library for Rust with no dependencies, no macros, and no implicit I/O
https://github.com/sile/noargs
command-line-parser rust
Last synced: about 1 month ago
JSON representation
Imperative command-line argument parser library for Rust with no dependencies, no macros, and no implicit I/O
- Host: GitHub
- URL: https://github.com/sile/noargs
- Owner: sile
- License: mit
- Created: 2025-03-16T06:19:26.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2026-01-10T05:40:00.000Z (about 1 month ago)
- Last Synced: 2026-01-11T01:28:05.808Z (about 1 month ago)
- Topics: command-line-parser, rust
- Language: Rust
- Homepage:
- Size: 201 KB
- Stars: 27
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
noargs
======
[](https://crates.io/crates/noargs)
[](https://docs.rs/noargs)
[](https://github.com/sile/noargs/actions)

`noargs` is an imperative command-line argument parser library for Rust with no dependencies, no macros, and no implicit I/O.
Features
--------
- Supports the following argument types:
- Positional arguments ([`Arg`])
- Named arguments with values ([`Opt`])
- Named arguments without values ([`Flag`])
- Subcommands ([`Cmd`])
- Automatically generates help text
- Simple and minimal interface due to its imperative nature (no complex DSL)
[`Arg`]: https://docs.rs/noargs/latest/noargs/struct.Arg.html
[`Opt`]: https://docs.rs/noargs/latest/noargs/struct.Opt.html
[`Flag`]: https://docs.rs/noargs/latest/noargs/struct.Flag.html
[`Cmd`]: https://docs.rs/noargs/latest/noargs/struct.Cmd.html
Examples
--------
### Basic Usage
The following code demonstrates the basic usage of `noargs`:
```rust
fn main() -> noargs::Result<()> {
// Create `noargs::RawArgs` having the result of `std::env::args()`.
let mut args = noargs::raw_args();
// Set metadata for help
args.metadata_mut().app_name = env!("CARGO_PKG_NAME");
args.metadata_mut().app_description = env!("CARGO_PKG_DESCRIPTION");
// Handle well-known flags
if noargs::VERSION_FLAG.take(&mut args).is_present() {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(());
}
noargs::HELP_FLAG.take_help(&mut args);
// Handle application specific args
let foo: usize = noargs::opt("foo")
.default("1").take(&mut args).then(|a| a.value().parse())?;
let bar: bool = noargs::flag("bar")
.take(&mut args).is_present();
let baz: Option = noargs::arg("[BAZ]")
.take(&mut args).present_and_then(|a| a.value().parse())?;
// Check unexpected args and build help text if need
if let Some(help) = args.finish()? {
print!("{help}");
return Ok(());
}
// Do application logic
println!("foo: {}, bar: {}, baz: {:?}", foo, bar, baz);
Ok(())
}
```
### Subcommands
The following example shows how to handle subcommands:
```rust
fn main() -> noargs::Result<()> {
let mut args = noargs::raw_args();
args.metadata_mut().app_name = env!("CARGO_PKG_NAME");
args.metadata_mut().app_description = env!("CARGO_PKG_DESCRIPTION");
// Handle well-known flags
if noargs::VERSION_FLAG.take(&mut args).is_present() {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(());
}
noargs::HELP_FLAG.take_help(&mut args);
// Handle subcommands
if noargs::cmd("start")
.doc("Start the service")
.take(&mut args)
.is_present()
{
let port: u16 = noargs::opt("port")
.short('p')
.default("8080")
.take(&mut args)
.then(|o| o.value().parse())?;
println!("Starting service on port {}", port);
} else if noargs::cmd("stop")
.doc("Stop the service")
.take(&mut args)
.is_present()
{
println!("Stopping service");
} else if let Some(help) = args.finish()? {
print!("{help}");
return Ok(());
}
Ok(())
}
```