Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/z5labs/pflag
A crate implementing POSIX/GNU-style --flags.
https://github.com/z5labs/pflag
Last synced: 6 days ago
JSON representation
A crate implementing POSIX/GNU-style --flags.
- Host: GitHub
- URL: https://github.com/z5labs/pflag
- Owner: z5labs
- License: mit
- Created: 2020-09-07T01:31:32.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-22T17:03:10.000Z (about 4 years ago)
- Last Synced: 2024-04-26T05:23:41.369Z (7 months ago)
- Language: Rust
- Size: 43 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Documentation](https://docs.rs/pflag/badge.svg)](https://docs.rs/pflag)
# pflag
`pflag` is a port of the [spf13](https://github.com/spf13/pflag)'s popular fork of the
Go package by the same name.## Usage
```rust
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::("num").unwrap(), 1);
assert_eq!(*flags.value_of::("str").unwrap(), "world");
assert_eq!(flags.value_of::>("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");
```