Crates.io | go-flag |
lib.rs | go-flag |
version | 0.1.0 |
source | src |
created_at | 2019-09-16 05:43:12.9029 |
updated_at | 2019-09-16 05:43:12.9029 |
description | Command line parser library, made to be compatible with Go's flag |
homepage | |
repository | https://github.com/qnighy/go-flag.rs |
max_upload_size | |
id | 165083 |
size | 69,041 |
A command-line parser with compatibility of Go's flag
in its main focus.
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:
-f
/--force
or -n
/--lines
. flag
doesn't have such
distinction.-fd
means -f
plus -d
.
flag
parses it as a single flag named fd
../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:
flag
library in its command-line behavior.
Note that API compatibility (similarity) is a different matter.structopt
is painful. Therefore, this library comes with an ability to
check typical incompatible usages to allow gradual migration.structopt
if you want to extend
your program to accept more complex flags.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);
});