From ab8eea8c95a63d659783969b3e367f0726764af4 Mon Sep 17 00:00:00 2001 From: Xavier ASUS Date: Fri, 28 Dec 2018 17:48:29 +0100 Subject: Required parameters are now checked. Added new CmdLineArg instance inside const parameter table, used for baudrate configuration. Implemented some methods on CmdLineArg. --- src/main.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 8498b83..df51d84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,6 @@ use serial::prelude::*; use std::{string::String, env, io, collections::HashMap, time::Duration}; -const PARAMETER_TABLE : [&'static str; 2] = ["--port-name", "--disable-output"]; - /// Main function. fn main() { @@ -18,7 +16,7 @@ fn main() { } fn app(arg_hash: HashMap) { - + println!("App!"); } struct CmdLineArg { @@ -28,7 +26,31 @@ struct CmdLineArg { explanation : &'static str } -const CMD_LINE_ARGS : [CmdLineArg; 2] = +impl CmdLineArg { + fn get_explanation(&self) -> String { + self.explanation.to_string() + } + + fn get_string(&self) -> String { + self.arg_str.to_string() + } + + fn get_param(&self) -> Option { + match self.param_str { + None => None, + _ => Some(self.param_str.unwrap().to_string()) + } + } + + fn has_parameters(&self) -> bool { + match self.param_str { + None => false, + _ => true + } + } +} + +const CMD_LINE_ARGS : [CmdLineArg; 3] = [ CmdLineArg { arg_str : "--port-name", @@ -42,32 +64,41 @@ const CMD_LINE_ARGS : [CmdLineArg; 2] = param_str : None, is_required : false, explanation : "Disables incoming debug messages from the console" + }, + + CmdLineArg { + arg_str : "--baudrate", + param_str : Some("[BAUDRATE]"), + is_required : false, + explanation : "Sets serial port baudrate. Defaults to 4800bps" } ]; -fn process_arguments() -> Option> { +fn show_help() { + println!("rspsxserial command line arguments:"); - if env::args_os().count() <= 1 { - println!("rspsxserial command line arguments:"); + for arg in CMD_LINE_ARGS.iter() { + let mut line = String::new(); - for arg in CMD_LINE_ARGS.iter() { - let mut line = String::new(); + line.push_str(arg.arg_str); - line.push_str(arg.arg_str); + if arg.has_parameters() { + line.push(' '); + line.push_str(arg.param_str.unwrap()); + }; - match arg.param_str { - None => {}, - _ => { - line.push(' '); - line.push_str(arg.param_str.unwrap()); - } - }; + line.push('\t'); + line.push_str(arg.explanation); + line.push('.'); - line.push('\t'); - line.push_str(arg.explanation); + println!("{}", line); + } +} - println!("{}", line); - } +fn process_arguments() -> Option> { + + if env::args_os().count() <= 1 { + show_help(); return None; } @@ -128,7 +159,17 @@ fn process_arguments() -> Option> { } } - println!("Hash = {:?}", arg_hash); + // Check all needed parameters have been given + for arg in CMD_LINE_ARGS.iter() { + if arg.is_required { + let arg_string = arg.arg_str.to_string(); + + if !arg_hash.contains_key(&arg_string) { + println!("Missing required option {}", arg_string); + return None + } + } + } Some(arg_hash) } -- cgit v1.2.3