From ebba3951aa13d2668bc684680f557b8cbbda8b20 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sat, 30 May 2020 15:04:23 +0530 Subject: [PATCH] Added command line argument option to run as a single command Added command line option to run the program as a single command instead of interactive mode --- src/main.rs | 94 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index bab1e52..2154ba5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::io::Write; use regex::Regex; use ansi_term::Colour; use ansi_term::Style; +use std::env; const ILLEGAL_EXP : &'static str = "Illegal Expression"; const ERR_PARSING_NOT_MATCH : &'static str = "Error parsing expression! \ @@ -134,38 +135,26 @@ fn evaluate (expr : &str, match_num : ®ex::Regex) -> Result { } fn main() { - // get a line from input and evaluate it - let mut expr = String::new (); + + let args : Vec = env::args().collect (); // regular expression to match a number let match_num = Regex::new (r"^\d+?\.*?\d*?$").unwrap (); - // loop until a blank line is received - loop { - expr.clear (); - print!("{}", Style::new().bold().paint("evpf>")); - io::stdout().flush ().expect (ERR_FLUSHING); - // read a line of text - io::stdin().read_line (&mut expr).expect (ERR_READING_LINE); - // trim the text - let expr = expr.trim (); - - if expr == "q" || expr == "Q" { - // quit if the expression is q or Qs - break; - } else if expr == "?" || expr == "h" || expr == "H" { - // display help text - for text in HELP_TEXT.iter() { - println! ("{}", Colour::Cyan.paint(*text)); + + if args.len () > 1 { + // if arguments are provided run in command line mode - i.e. print the + // result and exit + let mut expr = String::new (); + let mut i = 0; + // create the expression string to evaluate + for arg in args.iter() { + if i > 0 { + expr.push_str (&arg); + expr.push_str (" "); } - - continue; - } else if expr == "" { - // continue without proceeding - continue; + i += 1; } - - // Evaluate result + // evaluate the result let res = evaluate (&expr, &match_num); - // if Result is OK then print the result in green if res.is_ok () { let restxt = format! ("{}: {}", RESULT, @@ -176,7 +165,56 @@ fn main() { let errtxt = format! ("{}: {}", ERROR, res.unwrap_err()); eprintln! ("{}", Colour::Purple.paint (errtxt)); - eprintln! ("{}", Colour::Purple.paint (ERROR_HELP)); + } + + } else { + // if arguments are not provided run in interactive mode - + // display a prompt and get the expression + // repeat until the user quits + + // get a line from input and evaluate it + let mut expr = String::new (); + + // loop until a blank line is received + loop { + expr.clear (); + print!("{}", Style::new().bold().paint("evpf>")); + io::stdout().flush ().expect (ERR_FLUSHING); + // read a line of text + io::stdin().read_line (&mut expr).expect (ERR_READING_LINE); + // trim the text + let expr = expr.trim (); + + if expr == "q" || expr == "Q" { + // quit if the expression is q or Qs + break; + } else if expr == "?" || expr == "h" || expr == "H" { + // display help text + for text in HELP_TEXT.iter() { + println! ("{}", Colour::Cyan.paint(*text)); + } + + continue; + } else if expr == "" { + // continue without proceeding + continue; + } + + // Evaluate result + let res = evaluate (&expr, &match_num); + + // if Result is OK then print the result in green + if res.is_ok () { + let restxt = format! ("{}: {}", RESULT, + res.unwrap()); + println! ("{}", Colour::Green.paint (restxt)); + } else { + // print the error in purple + let errtxt = format! ("{}: {}", ERROR, + res.unwrap_err()); + eprintln! ("{}", Colour::Purple.paint (errtxt)); + eprintln! ("{}", Colour::Purple.paint (ERROR_HELP)); + } } } } -- 2.20.1