From 1237a5cab8e7b4236eeef7b5f05467f24cd881aa Mon Sep 17 00:00:00 2001
From: Blinningjr <nicke.l@telia.com>
Date: Mon, 4 Jan 2021 14:01:48 +0100
Subject: [PATCH] Fixed the cli

---
 srp_analysis/src/main.rs | 45 ++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/srp_analysis/src/main.rs b/srp_analysis/src/main.rs
index 651c9f5..1db7eff 100644
--- a/srp_analysis/src/main.rs
+++ b/srp_analysis/src/main.rs
@@ -18,13 +18,17 @@ struct Opt {
     #[structopt(short, long)]
     approx: bool,    
     
-    /// Input file, example input if not present
-    #[structopt(parse(from_os_str))]
-    input: Option<PathBuf>,
-
-    /// Output file, stdout if not present
-    #[structopt(parse(from_os_str))]
-    output: Option<PathBuf>,
+    /// Output in json format
+    #[structopt(short, long)]
+    json: bool,    
+     
+    /// Path to the input json file
+    #[structopt(short, long)]
+    in_file: Option<PathBuf>,
+    
+    /// Path to the output file
+    #[structopt(short, long)]
+    out_file: Option<PathBuf>,
 }
 
 
@@ -33,7 +37,7 @@ fn main() {
     let opt = Opt::from_args();
     //println!("{:?}", opt);
     
-    let tasks: Tasks = match opt.input {
+    let tasks: Tasks = match opt.in_file {
         Some(file) => load_tasks(file),
         None => create_tasks(),
     };
@@ -41,14 +45,29 @@ fn main() {
     let (ip, tr) = pre_analysis(&tasks);
     let analysis = analyse(&tasks, &ip, &tr, opt.approx);
 
-    match opt.output {
+    let output: String = match opt.json {
+        true => serde_json::to_string(&analysis).unwrap(),
+        false => format_analysis(&analysis, &tasks, &ip, &tr),
+    };
+
+    match opt.out_file {
         Some(file) => {
-            write_to_file(serde_json::to_string(&analysis).unwrap(), file);
-        }, // TODO: Implement so that the analysis can be saved to txt.
-        None => {
-            println!("{}", format_analysis(&analysis, &tasks, &ip, &tr)); 
+            match write_to_file(output, file) {
+                Ok(_) => (),
+                Err(err) => panic!(err),
+            };
         },
+        None => println!("{}", output),
     };
+
+//    match opt.output {
+//        Some(file) => {
+//            write_to_file(serde_json::to_string(&analysis).unwrap(), file);
+//        }, // TODO: Implement so that the analysis can be saved to txt.
+//        None => {
+//            println!("{}", format_analysis(&analysis, &tasks, &ip, &tr)); 
+//        },
+//    };
 }
 
 
-- 
GitLab