From 85a553080a960ac369d808db81fd2b01fe6b89de Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Wed, 24 Apr 2024 08:14:36 -0700 Subject: [PATCH] arg parsing --- src/cmd.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 8 +++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/cmd.rs diff --git a/src/cmd.rs b/src/cmd.rs new file mode 100644 index 0000000..2330c91 --- /dev/null +++ b/src/cmd.rs @@ -0,0 +1,35 @@ +use std::{collections::HashMap, process::exit}; + +pub fn help() { + println!( + "usage: +airport-paths + finds the shortest path between the first and second airport-paths +airport-paths -h + shows this help" + ); +} + +pub fn handle_args(args: Vec, _code_to_id: HashMap) -> (usize, usize) { + let (origin, destination) = match args.len() { + 3 => { + let exit_handler = || { + println!("unable to find airport"); + exit(2); + }; + let origin = _code_to_id.get(&args[1]).unwrap_or_else(exit_handler); + let destination = _code_to_id.get(&args[2]).unwrap_or_else(exit_handler); + + (origin, destination) + } + _ => { + help(); + match args.get(1).map(String::as_str) { + Some("-h") => exit(0), + _ => exit(2), + } + } + }; + + (*origin, *destination) +} diff --git a/src/main.rs b/src/main.rs index d185085..90c49cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ #![allow(dead_code)] mod airport; +mod cmd; mod distance; mod parse; use std::{ cell::RefCell, collections::{hash_map::Entry, HashMap}, + env, rc::Rc, }; @@ -128,5 +130,9 @@ fn main() { }; } - println!("{graph:#?}") + // Using command line arguments as input + let args: Vec = env::args().collect(); + let (origin_id, destination_id) = cmd::handle_args(args, _code_to_id); + + println!("{:#?} {:#?}", graph[&origin_id], graph[&destination_id]); }