pflag

Crates.iopflag
lib.rspflag
version0.6.2
sourcesrc
created_at2020-09-07 02:19:47.535823
updated_at2020-10-22 17:05:17.564511
descriptionA crate implementing POSIX/GNU-style --flags.
homepagehttps://github.com/z5labs/pflag
repositoryhttps://github.com/z5labs/pflag
max_upload_size
id285576
size51,903
(Zaba505)

documentation

README

Documentation

pflag

pflag is a port of the spf13's popular fork of the Go package by the same name.

Usage

use pflag::{FlagSet, Slice};
use std::net::{IpAddr, Ipv4Addr};

let mut flags = FlagSet::new("name");

// Use higher level methods over add_flag directly.
flags.int8("num", 0, "a flag for a number");
flags.string_p("str", 's', String::from("default value"), "a flag for a String and has a shorthand");
flags.ip_addr_slice(
    "addrs",
    Slice::from([IpAddr::V4(Ipv4Addr::new(0,0,0,0)), IpAddr::V4(Ipv4Addr::new(127,0,0,1))]),
    "a multi-valued flag",
);

let args = "--num=1 -s world --addrs 192.168.1.1,192.168.0.1 --addrs=127.0.0.1 subcommand";
if let Err(err) = flags.parse(args.split(' ')) {
    panic!(err);
}

// Retrieving value is very easy with the value_of method.
assert_eq!(*flags.value_of::<i8>("num").unwrap(), 1);
assert_eq!(*flags.value_of::<String>("str").unwrap(), "world");
assert_eq!(flags.value_of::<Slice<IpAddr>>("addrs").unwrap().len(), 3);

// Any non-flag args i.e. positional args can be retrieved by...
let args = flags.args();
assert_eq!(args.len(), 1);
assert_eq!(args[0], "subcommand");
Commit count: 58

cargo fmt