From 4129d85de0f4acdf0040ff8b7791c29f700ecd7c Mon Sep 17 00:00:00 2001 From: marcelarie Date: Tue, 15 Oct 2024 00:25:46 +0200 Subject: [PATCH] feat: Add no-comments and debug flags --- src/command/arguments.rs | 34 +++++++++++++++++++++++++++------- src/main.rs | 5 ++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/command/arguments.rs b/src/command/arguments.rs index 09c0307..7e937d4 100644 --- a/src/command/arguments.rs +++ b/src/command/arguments.rs @@ -1,11 +1,15 @@ pub struct CliArgs { pub file_path: String, + pub no_comments: bool, + pub debug_mode: bool, } pub struct GatheredArgs { + file_path: Option, + no_comments: bool, + debug_mode: bool, #[allow(unused)] arguments: Vec, - file_path: Option, #[allow(unused)] remaining_args: Vec, } @@ -15,18 +19,28 @@ impl CliArgs { let mut arguments: Vec = Vec::new(); let mut script_name = None; let mut args = std::env::args(); + let mut no_comments = false; + let mut debug_mode = false; // Skip the program name args.next(); while let Some(arg) = args.next() { - if !arg.starts_with('-') { + if !arg.starts_with('-') && script_name.is_none() { script_name = Some(arg); - break; + // TODO: Check if this should be continue or break + continue; }; let flag_value = match arg.as_ref() { - "--test-flag" => args.next().map(|x| x.to_string()), + "--no-comments" | "-nc" => { + no_comments = true; + Some(arg.to_string()) + } + "--debug" | "-d" => { + debug_mode = true; + Some(arg.to_string()) + } _ => None, }; @@ -38,15 +52,21 @@ impl CliArgs { GatheredArgs { arguments, file_path: script_name, + no_comments, + debug_mode, remaining_args: args.collect(), } } pub fn new() -> Result { - let gathered = Self::gather(); + let arguments_config = Self::gather(); - match gathered.file_path { - Some(file_path) => Ok(Self { file_path }), + match arguments_config.file_path { + Some(file_path) => Ok(Self { + file_path, + no_comments: arguments_config.no_comments, + debug_mode: arguments_config.debug_mode, + }), None => Err("No script name provided"), } } diff --git a/src/main.rs b/src/main.rs index d39daea..2803a3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,9 @@ fn main() { std::process::exit(1); }); + println!("Reading file: {}", args.file_path); + println!("No comments: {}", args.no_comments); + let code = fs::read_to_string(args.file_path).expect("Error reading file"); let mut parser = Parser::new(); @@ -42,7 +45,7 @@ fn main() { if alias.is_valid_nushell { writeln!(writer, "alias {} = {}", alias.name, alias.content) .expect("Error writing to file"); - } else { + } else if !args.no_comments { writeln!( writer, "# alias {} = {} # Errors: {}",