Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

LoganDark/getargs-os

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

getargs-os

Adds a newtype wrapper (OsArgument) around OsStr that allows it to be parsed by getargs::Options.

In combination with the argv crate, this allows for lowest-cost argument parsing across all platforms (zero-cost on Linux).

This is a separate crate from getargs because it requires (wildly) unsafe code. std does not want us messing with OsStrs at all!

Usage

First, obtain an iterator over OsStrs somehow - I recommend argv once again - then wrap them in OsArgument and pass that to Options::new.

use getargs::Options;
use getargs_os::OsArgument;
let mut opts = Options::new(argv::iter().skip(1).map(<&OsArgument>::from));

Then use Optionsgetargs::Options as normal - check its documentation for more usage examples.

You can use the os! macro to create new OS strings to compare arguments against. This macro works on all operating systems. For example:

while let Some(arg) = opts.next_arg().expect("some ooga booga just happened") {
	if arg == Arg::Long(os!("help")) {
		// print help...
	} else {
		// ...
	}
}

os_str_bytes feature

To unlock From<&str> and PartialEq<&str> impls for &OsArgument, you must enable the unstable os_str_bytes feature, which depends on Nightly. This is because earlier versions of Rust didn't provide guarantees that OS strings are a superset of UTF-8 (even though getargs-os relied on this anyway in the past). Since the feature now exists, I don't want to make getargs-os unconditionally require Nightly, but new features relying on this guarantee will be gated behind the os_str_bytes feature until it is stabilized.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages