EVEN BETTER argument parsing

This commit is contained in:
Emma Tebibyte 2023-04-11 18:49:11 -04:00
parent 1e69ae5e98
commit 1ef228faa2
1 changed files with 15 additions and 21 deletions

View File

@ -355,33 +355,27 @@ fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> {
fn main() -> Result<()> {
let argv = args().collect::<Vec<String>>();
let mut buf = Vec::new();
let text: String;
text = match argv.get(1) {
match argv.get(1).map(|s| s.as_str()) {
Some("-") | None => unsafe { File::from_raw_fd(0) },
Some(path) => {
match path.as_str() {
"-" => unsafe { File::from_raw_fd(0) },
_ => {
std::fs::File::open(path).unwrap_or_else(|_| {
eprintln!(
"{}: {}: No such file or directory.",
argv[0],
argv[1]
);
exit(EX_UNAVAILABLE);
})
},
}.read_to_end(&mut buf).unwrap();
String::from_utf8(buf).unwrap_or_else(|_| {
std::fs::File::open(path).unwrap_or_else(|_| {
eprintln!(
"{}: {}: File contents are invalid UTF-8.", argv[0], argv[1]
"{}: {}: No such file or directory.",
argv[0],
argv[1]
);
exit(EX_DATAERR);
exit(EX_UNAVAILABLE);
})
},
None => "".to_string(),
};
}.read_to_end(&mut buf).unwrap();
let text = String::from_utf8(buf).unwrap_or_else(|_| {
eprintln!(
"{}: {}: File contents are invalid UTF-8.", argv[0], argv[1]
);
exit(EX_DATAERR);
});
let state = State::from_str(&text)?;
let mut stdout = stdout();