diff options
| author | Xavier ASUS <xavi92psx@gmail.com> | 2018-12-28 17:48:29 +0100 |
|---|---|---|
| committer | Xavier ASUS <xavi92psx@gmail.com> | 2018-12-28 17:48:29 +0100 |
| commit | ab8eea8c95a63d659783969b3e367f0726764af4 (patch) | |
| tree | 01ca0536a17e180faf29fedcb38450919028625d /src | |
| parent | dc203a882c9ca5848abb2bf5991ed2ea4fa1b29a (diff) | |
Required parameters are now checked.
Added new CmdLineArg instance inside const parameter table, used for baudrate configuration.
Implemented some methods on CmdLineArg.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 85 |
1 files changed, 63 insertions, 22 deletions
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<String, String>) { - + 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<String> { + 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<HashMap<String, String>> { +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<HashMap<String, String>> { + + if env::args_os().count() <= 1 { + show_help(); return None; } @@ -128,7 +159,17 @@ fn process_arguments() -> Option<HashMap<String, String>> { } } - 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) } |
