arg parsing

main
Zynh0722 2024-04-24 08:14:36 -07:00
parent 3f8a061404
commit 85a553080a
2 changed files with 42 additions and 1 deletions

35
src/cmd.rs Normal file
View File

@ -0,0 +1,35 @@
use std::{collections::HashMap, process::exit};
pub fn help() {
println!(
"usage:
airport-paths <aiport code> <airport code>
finds the shortest path between the first and second airport-paths
airport-paths -h
shows this help"
);
}
pub fn handle_args(args: Vec<String>, _code_to_id: HashMap<String, usize>) -> (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)
}

View File

@ -1,12 +1,14 @@
#![allow(dead_code)] #![allow(dead_code)]
mod airport; mod airport;
mod cmd;
mod distance; mod distance;
mod parse; mod parse;
use std::{ use std::{
cell::RefCell, cell::RefCell,
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
env,
rc::Rc, rc::Rc,
}; };
@ -128,5 +130,9 @@ fn main() {
}; };
} }
println!("{graph:#?}") // Using command line arguments as input
let args: Vec<String> = env::args().collect();
let (origin_id, destination_id) = cmd::handle_args(args, _code_to_id);
println!("{:#?} {:#?}", graph[&origin_id], graph[&destination_id]);
} }