-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement `&OsStr as Argument` on top of `&[u8] as Argument`. The unsafe code to convert back from the sliced `&[u8]` keeps to the safety invariants defined for the `OsStr::from_encoded_bytes_unchecked` function.
- Loading branch information
Showing
8 changed files
with
240 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
use getargs::{Arg, Options}; | ||
use std::ffi::OsStr; | ||
use getargs::{Arg, Argument, Options}; | ||
use std::ffi::OsString; | ||
use std::path::PathBuf; | ||
|
||
#[cfg(unix)] | ||
fn main() { | ||
use std::os::unix::ffi::OsStrExt; | ||
|
||
let args: Vec<_> = std::env::args_os().skip(1).collect(); | ||
|
||
let mut opts = Options::new(args.iter().map(|s| s.as_bytes())); | ||
let mut opts = Options::new(args.iter().map(OsString::as_os_str)); | ||
|
||
while let Some(arg) = opts.next_arg().expect("usage error") { | ||
match arg { | ||
Arg::Short('f') | Arg::Long("file") => { | ||
let f = OsStr::from_bytes(opts.value().expect("usage error")); | ||
let f = PathBuf::from(opts.value().expect("usage error")); | ||
println!("file option: {f:?}"); | ||
} | ||
Arg::Positional(pos) => { | ||
let pos = OsStr::from_bytes(pos); | ||
println!("positional: {pos:?}"); | ||
println!("positional: {}", Argument::display(pos)); | ||
} | ||
_ => println!("other: {arg:?}"), | ||
_ => println!("other: {}", arg), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(unix))] | ||
fn main() { | ||
eprintln!("Only supported on Unix because UTF-16 is hard, sorry :("); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.