Skip to content

Commit

Permalink
feat: Add no comments flag and help message
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelarie committed Oct 14, 2024
1 parent 4129d85 commit 1a8267c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TODO:
- [x] Write them to a file
- [x] Single time
- [ ] Use nushell env
- [x] Add no comments flag
- [ ] Add a command to source the file
- [ ] Handle multiple files in a directory

Expand All @@ -37,7 +38,8 @@ manually or use the Nushell environment. If using the environment method, the
file will regenerate at the start of each shell session.

**Unnecessary diagram:**
not that complex but I always wanted to try this out.
Not that complex but I always wanted to try this out.

```mermaid
---
config:
Expand All @@ -56,13 +58,10 @@ graph TD
D --> I[Comment Out Invalid Aliases]
I --> J[Include Parsing Error Information]
```
<!-- <div align="center"> -->
<!-- <img src="https://github.com/user-attachments/assets/4953aa13-8c6c-4e1a-b463-436971ee06b7" alt="Mermaid diagram" width="700"> -->
<!-- </div> -->

## Usage

current implementation:
Current implementation:

```bash
nu-alias-converter .bash_aliases # will generate a alias.nu file in the same directory
Expand Down
60 changes: 49 additions & 11 deletions src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ pub struct CliArgs {
pub debug_mode: bool,
}

pub struct GatheredArgs {
struct GatheredArgs {
file_path: Option<String>,
no_comments: bool,
debug_mode: bool,
#[allow(unused)]
arguments: Vec<String>,
#[allow(unused)]
remaining_args: Vec<String>,
Expand Down Expand Up @@ -41,7 +40,24 @@ impl CliArgs {
debug_mode = true;
Some(arg.to_string())
}
_ => None,
"--help" | "-h" => Some(arg.to_string()),
_ => {
let chars = arg.chars().collect::<Vec<char>>();
for (index, c) in chars.iter().enumerate() {
if c == &'-' {
println!("Invalid flag: {}", arg);
println!("Use -h for help");
std::process::exit(1);
}
let flag = format!("-{}", c);
let value =
arg.chars().skip(index + 1).collect::<String>();
arguments.push(flag);
arguments.push(value);
break;
}
None
}
};

if let Some(flag_value) = flag_value {
Expand All @@ -58,16 +74,38 @@ impl CliArgs {
}
}

fn print_help() {
println!(
"Usage: {} [options] <script>",
std::env::args()
.next()
.unwrap_or_else(|| "nu-alias-converter".to_string())
);
println!();
println!("Options:");
println!(" --no-comments, -nc Do not print comments");
println!(" --debug, -d Print debug information");
println!(" --help, -h Print this help message");
}

pub fn new() -> Result<Self, &'static str> {
let arguments_config = Self::gather();
let gathered = Self::gather();
let is_help_request = gathered
.arguments
.iter()
.any(|arg| arg == "--help" || arg == "-h");

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"),
if is_help_request {
Self::print_help();
std::process::exit(0);
}

let file_path = gathered.file_path.ok_or("No script name provided")?;

Ok(Self {
file_path,
no_comments: gathered.no_comments,
debug_mode: gathered.debug_mode,
})
}
}
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use command::arguments::CliArgs;
use std::{
fs::{self, File},
io::{BufWriter, Write},
path::Path,
};
use syntax_tree::find_aliases;
use tree_sitter::Parser;
Expand All @@ -14,9 +15,12 @@ fn main() {
eprintln!("Error: {}", e);
std::process::exit(1);
});
let file_exists = Path::new(&args.file_path).exists();

println!("Reading file: {}", args.file_path);
println!("No comments: {}", args.no_comments);
if !file_exists {
eprintln!("Error: File path {} does not exist", args.file_path);
std::process::exit(1);
}

let code = fs::read_to_string(args.file_path).expect("Error reading file");

Expand Down

0 comments on commit 1a8267c

Please sign in to comment.