Skip to content

Commit

Permalink
feat: Add no-comments and debug flags
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelarie committed Oct 14, 2024
1 parent c789414 commit 4129d85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/command/arguments.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
no_comments: bool,
debug_mode: bool,
#[allow(unused)]
arguments: Vec<String>,
file_path: Option<String>,
#[allow(unused)]
remaining_args: Vec<String>,
}
Expand All @@ -15,18 +19,28 @@ impl CliArgs {
let mut arguments: Vec<String> = 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,
};

Expand All @@ -38,15 +52,21 @@ impl CliArgs {
GatheredArgs {
arguments,
file_path: script_name,
no_comments,
debug_mode,
remaining_args: args.collect(),
}
}

pub fn new() -> Result<Self, &'static str> {
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"),
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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: {}",
Expand Down

0 comments on commit 4129d85

Please sign in to comment.