1 unstable release

0.1.0 Sep 16, 2019

#2323 in Parser implementations

Download history 2948/week @ 2024-03-14 2096/week @ 2024-03-21 2239/week @ 2024-03-28 2633/week @ 2024-04-04 2251/week @ 2024-04-11 2163/week @ 2024-04-18 2022/week @ 2024-04-25 1772/week @ 2024-05-02 1974/week @ 2024-05-09 2347/week @ 2024-05-16 2237/week @ 2024-05-23 2269/week @ 2024-05-30 1870/week @ 2024-06-06 2301/week @ 2024-06-13 2880/week @ 2024-06-20 1667/week @ 2024-06-27

9,157 downloads per month
Used in 8 crates (6 directly)

MIT/Apache

65KB
1.5K SLoC

A command-line parser with compatibility of Go's flag in its main focus.

Design Goals

Go comes with a built-in support for command-line parsing: the flag library. This is known to be incompatible with GNU convention, such as:

  • Short/long flags. POSIX/GNU flags sometimes have a pair of short and long flags like -f/--force or -n/--lines. flag doesn't have such distinction.
  • Combined short flags. In POSIX/GNU convention, -fd means -f plus -d. flag parses it as a single flag named fd.
  • Flags after arguments. POSIX/GNU allows flags to appear after positional arguments like ./command arg1 --flag arg2 unless explicitly separated by --. flag parses it as a consecutive list of positional arguments.

The go-flag crate is designed to allow Rust programmers to easily port Go CLI programs written using flag without breaking compatibility.

Therefore, our priority is the following:

  1. Behavioral compatibility. It's meant to be compatible with the Go's built-in flag library in its command-line behavior. Note that API compatibility (similarity) is a different matter.
  2. Migration. Being unable to use more sophisticated parsers like structopt is painful. Therefore, this library comes with an ability to check typical incompatible usages to allow gradual migration.
  3. Simplicity. This library isn't meant to provide full parser functionality. For example, subcommand parsing is out of scope for this library. Try to migrate to e.g. structopt if you want to extend your program to accept more complex flags.

Example

Typically you can use the parse function.

let mut force = false;
let mut lines = 10_i32;
let args: Vec<String> = go_flag::parse(|flags| {
    flags.add_flag("f", &mut force);
    flags.add_flag("lines", &mut lines);
});

If you want a list of file paths, use PathBuf or OsString to allow non-UTF8 strings.

use std::path::PathBuf;
let args: Vec<PathBuf> = go_flag::parse(|_| {});

If an incompatible usage is detected, parse issues warnings and continues processing. You can alter the behavior using parse_with_warnings.

For example, when enough time passed since the first release of your Rust port, you can start to deny the incompatible usages by specifying WarningMode::Error:

use go_flag::WarningMode;
let mut force = false;
let mut lines = 10_i32;
let args: Vec<String> =
    go_flag::parse_with_warnings(WarningMode::Error, |flags| {
        flags.add_flag("f", &mut force);
        flags.add_flag("lines", &mut lines);
    });

Dependencies

~8KB