Skip to content

Commit

Permalink
cli parser works
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed May 5, 2022
1 parent 9b24311 commit 0f20a10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(dead_code)]

use std::cmp::PartialEq;
use std::env::Args;
use std::fs;
use std::process;

Expand Down Expand Up @@ -45,7 +44,7 @@ impl CommandLineArgs {
}

/// Enumerations of valid command line options used for finite state automata.
enum CommandLineOption {
pub enum CommandLineOption {
Directory,
Depth,
Patterns,
Expand All @@ -66,8 +65,8 @@ impl PartialEq<str> for CommandLineOption {
impl CommandLineOption {
/// Parses Args for valid command line options and returns a CommandLineArgs struct
/// containing provided options. Writes to stderr and exits if malformed cl-args.
pub fn parse_args(mut args: Args) -> CommandLineArgs {
if let Some(_) = args.find(|i| i == "-h" ) {
pub fn parse_args(args: &[String]) -> CommandLineArgs {
if let Some(_) = args.iter().find(|i| i == &"-h" ) {
println!("{}", HELP);
process::exit(0);
}
Expand All @@ -89,17 +88,20 @@ impl CommandLineOption {
Self::validate_arg(&arg);
let directory = Self::get_directory_from_arg(&arg);
cli_arguments.set_directory(directory.to_string());
current_state = CommandLineOption::None;
},

CommandLineOption::Depth => {
Self::validate_arg(&arg);
let depth = Self::get_depth_from_arg(&arg);
cli_arguments.set_depth(depth);
current_state = CommandLineOption::None;
},

CommandLineOption::Patterns => {
Self::validate_arg(&arg);
cli_arguments.set_prefixes(arg.clone());
current_state = CommandLineOption::None;
}
}
}
Expand Down Expand Up @@ -154,10 +156,10 @@ impl CommandLineOption {

#[cfg(test)]
mod test {
#[test]
fn test_command_line_option() {
use super::CommandLineOption;
use super::CommandLineOption;

#[test]
fn test_command_line_args() {
assert!(&CommandLineOption::Directory == "-d");
assert!(&CommandLineOption::Directory != "-b");
assert!(&CommandLineOption::Depth == "-l");
Expand Down
34 changes: 32 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@ mod cli;
mod file_tree;
mod utils;

use cli::{CommandLineArgs, CommandLineOption};
use file_tree::FileTree;

fn main() {
//let args = env::args();
let args = env::args();

FileTree::default().display()
if args.len() <= 1 {
FileTree::default().display()
} else {
let clargs = args.collect::<Vec<String>>();
let (_, args) = clargs.split_first().unwrap();

let CommandLineArgs {
directory,
depth,
prefixes
} = CommandLineOption::parse_args(args);

let dir = match directory {
Some(d) => d,
None => ".".to_string()
};

let pre = match prefixes {
Some(d) => d,
None => "".to_string()
};

let maybe_pre = if pre == "".to_string() {
None
} else {
Some(pre.as_str())
};

FileTree::new(&dir, maybe_pre, depth).unwrap().display();
}
}

0 comments on commit 0f20a10

Please sign in to comment.