#arguments-parser #io #text #noargs

noargs

Imperative command-line argument parser library with no dependencies, no macros, and no implicit I/O

8 unstable releases (3 breaking)

Uses new Rust 2024

new 0.3.2 Apr 17, 2025
0.3.1 Apr 11, 2025
0.2.1 Apr 7, 2025
0.2.0 Mar 31, 2025
0.0.1 Mar 23, 2025

#105 in Command-line interface

Download history 308/week @ 2025-03-21 501/week @ 2025-03-28 618/week @ 2025-04-04 547/week @ 2025-04-11

1,976 downloads per month
Used in 5 crates

MIT license

83KB
2K SLoC

noargs

noargs Documentation Actions Status License

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)

Examples

The following code demonstrates the basic usage of noargs:

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(|o| o.value().parse())?;
    let bar: bool = noargs::flag("bar")
        .take(&mut args).is_present();
    let baz: Option<String> = 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.

    Ok(())

No runtime deps