Skip to content

Instantly share code, notes, and snippets.

@gsquire
Created October 27, 2017 01:56
Show Gist options
  • Save gsquire/7157b74a755298ea121c317b76a7e1e9 to your computer and use it in GitHub Desktop.
Save gsquire/7157b74a755298ea121c317b76a7e1e9 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate error_chain;
extern crate clap;
extern crate reqwest;
use std::fs::File;
use std::io;
use clap::{App, Arg};
error_chain! {
foreign_links {
IoErr(std::io::Error);
ReqErr(reqwest::Error);
}
}
fn run(args: &clap::ArgMatches) -> Result<u64> {
let url = args.value_of("URL").unwrap();
let file_name = args.value_of("output").unwrap_or("output");
let mut resp = reqwest::get(url)?;
let mut output = File::create(file_name)?;
let b = io::copy(&mut resp, &mut output)?;
Ok(b)
}
fn main() {
let matches = App::new("wget")
.version("0.1")
.author("me")
.about("wget clone")
.arg(
Arg::with_name("URL")
.help("the url to get")
.required(true)
.index(1),
)
.arg(
Arg::with_name("output")
.help("the file name to output to")
.short("O")
.long("out")
.value_name("FILE")
.required(false),
)
.get_matches();
let res = run(&matches);
if res.is_err() {
println!("error running get: {}", res.unwrap_err().description());
} else {
println!("downloaded {} bytes", res.unwrap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment