From dac68c8ff77680576fdcbadd9e119cd2c826781d Mon Sep 17 00:00:00 2001 From: ArloFilley Date: Sun, 27 Aug 2023 15:42:38 +0100 Subject: [PATCH] added clap support --- Cargo.toml | 5 +++-- src/main.rs | 47 +++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3b112fa..226d70b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rusty_torrent" -version = "0.9.2" +version = "0.9.3" edition = "2021" description = "A BitTorrent client implemented in Rust that allows you to interact with the BitTorrent protocol and download torrents." @@ -16,6 +16,7 @@ repository = "https://github.com/arlofilley/rusty_torrent" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.0", features = ["derive"] } dns-lookup = "2.0.2" log = "0.4.20" regex = "1.9.4" @@ -25,4 +26,4 @@ serde_bencode = "0.2.3" serde_bytes = "0.11.12" sha1 = "0.10.5" simple-logging = "2.0.2" -tokio = { version = "1.30.0", features = ["full"] } \ No newline at end of file +tokio = { version = "1.30.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 4ab985c..bfb9400 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,41 +31,40 @@ use crate::{ }; // External Ipmorts -use log::{ debug, info, LevelFilter, error }; +use clap::Parser; +use log::{ debug, info, LevelFilter }; + +/// Struct Respresenting needed arguments +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + #[arg(short, long)] + log_file_path: Option, + + #[arg(short, long)] + torrent_file_path: String, + + #[arg(short, long)] + download_path: String, +} /// The root function #[tokio::main] async fn main() { + let args = Args::parse(); + // Creates a log file to handle large amounts of data - let log_path = "/Users/arlo/code/RustyTorrent/log/rustytorrent.log"; - simple_logging::log_to_file(log_path, LevelFilter::Info).unwrap(); - - let torrent_file_path = match std::env::args().nth(1) { - None => { - error!("Please Provide A Torrent File"); - eprintln!("Critical error, please read log for more info: {}", log_path); - return - } - Some(path) => { path } - }; - - let download_path = match std::env::args().nth(2) { - None => { - error!("Please Provide A Download Path"); - eprintln!("Critical error, please read log for more info: {}", log_path); - return - } - Some(path) => { path } - }; + let log_path = args.log_file_path.unwrap_or(String::from("./log/rustytorrent.log")); + simple_logging::log_to_file(&log_path, LevelFilter::Info).unwrap(); // Read the Torrent File - let torrent = Torrent::from_torrent_file(&torrent_file_path).await; + let torrent = Torrent::from_torrent_file(&args.torrent_file_path).await; info!("Sucessfully read torrent file"); torrent.log_useful_information(); // Create the files that will be written to let mut files = Files::new(); - files.create_files(&torrent, &download_path).await; + files.create_files(&torrent, &args.download_path).await; // Gets peers from the given tracker let (_remote_hostname, _remote_port) = torrent.get_tracker(); @@ -93,7 +92,7 @@ async fn main() { info!("Found Peers"); // Creates an assumed peer connection to the `SocketAddr` given - let mut peer = match Peer::create_connection(&format!("{}:{}", announce_message_response.ips[4], announce_message_response.ports[4])).await { + let mut peer = match Peer::create_connection(&format!("{}:{}", announce_message_response.ips[0], announce_message_response.ports[0])).await { None => { return }, Some(peer) => peer };