Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions scraper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ enum Output {
Text,
}

// Based on sysexits.h
const USAGE: i32 = 64;

fn query<T: Read>(input: &Input, output: &Output, selector: &Selector, file: &mut T) -> bool {
let mut html = String::new();
file.read_to_string(&mut html).unwrap();
Expand Down Expand Up @@ -71,7 +74,10 @@ fn main() {
let args: Vec<String> = env::args().collect();
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!("{}", f.to_string()),
Err(f) => {
eprintln!("{}", f);
process::exit(USAGE);
}
};
if matches.opt_present("h") {
print!(
Expand Down Expand Up @@ -118,10 +124,16 @@ fn main() {
Output::Html
};

let selector = matches.free.first().expect("missing selector");
let selector = matches.free.first().unwrap_or_else(|| {
eprintln!("missing selector");
process::exit(USAGE);
});
let files = &matches.free[1..];

let selector = Selector::parse(selector).unwrap();
let selector = Selector::parse(selector).unwrap_or_else(|e| {
eprintln!("failed to parse selector: {}", e);
process::exit(USAGE);
});

let matched = if files.is_empty() {
query(&input, &output, &selector, &mut io::stdin())
Expand Down