5 releases

new 0.2.0 Feb 22, 2024
0.1.3 Feb 12, 2024
0.1.2 Apr 13, 2023
0.1.1 Apr 12, 2023
0.1.0 Apr 10, 2023

#417 in Command-line interface

Download history 13/week @ 2023-11-05 13/week @ 2023-11-12 14/week @ 2023-11-19 23/week @ 2023-11-26 12/week @ 2023-12-03 15/week @ 2023-12-10 12/week @ 2023-12-17 16/week @ 2023-12-24 5/week @ 2023-12-31 14/week @ 2024-01-07 12/week @ 2024-01-14 9/week @ 2024-01-21 14/week @ 2024-01-28 9/week @ 2024-02-04 73/week @ 2024-02-11 297/week @ 2024-02-18

395 downloads per month
Used in onlyargs_derive

MIT license

18KB
260 lines

Crates.io Documentation unsafe forbidden GitHub actions GitHub activity GitHub Sponsors

Only argument parsing! Nothing more.

Why?

  • 100% safe Rust 🦀.
  • Correctness: Paths with invalid UTF-8 work correctly on all platforms.
  • Fast compile times.
  • Convenience: #[derive(OnlyArgs)] on a struct and parse CLI arguments from the environment into it with minimal boilerplate.

MSRV Policy

The Minimum Supported Rust Version for onlyargs will always be made available in the MSRV.md file on GitHub.

Rationale

There's an argument parsing crate for everyone. So why write another?

onlyargs is an example of extreme minimalism! The only thing it provides is a trait and some utility functions; you're expected to do the actual work to implement it for your CLI argument struct. But don't let that scare you away! The parser implementation in the "full" example is only around 50 lines! (Most of the file is boilerplate.)

The goals of this parser are correctness, fast compile times, and convenience.

100% safe Rust

No shenanigans! The only unsafe code is abstracted away in the standard library.

Correctness

  • The main parsing loop uses OsString so that invalid UTF-8 can be accepted as an argument.
  • Arguments can either be stored directly as an OsString or converted to a PathBuf with no extra cost. Easily access your mojibake file systems!
  • Conversions from OsString are handled by your parser implementation. It's only as correct as you want it to be!
  • Play with the examples. Try to break it. Have fun!

Fast compile times

See myn benchmark results.

Convenience

Argument parsing is dead simple (assuming your preferred DSL is opinionated and no-nonsense). There is no reason to overcomplicate it by supporting multiple forms like --argument 123 and --argument=123 or -a 123 and -a123. Just pick one!

The provided examples use the former in both cases: --argument 123 and -a 123 are accepted for arguments with a value. Supporting both long and short argument names is just a pattern!

Some("--argument") | Some("-a")

It is fairly straightforward to derive an implementation with a proc_macro. Compare the "full-derive" example to the "full" example.

No runtime deps