diff --git a/Cargo.lock b/Cargo.lock index 664ce8c..35eb881 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "mkfile" -version = "0.2.0" +version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 665fa60..e85bf78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [package] name = "mkfile" -version = "0.2.0" +version = "0.2.1" edition = "2021" diff --git a/readme.md b/readme.md index ea731c6..3d35a64 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# About the project +# Overview Minimal Rust CLI app with no external dependencies. Creates text files. May create parent directories recursively, override existing files and output @@ -9,9 +9,11 @@ verbosely. mkfile \[OPTION\]... PATH... Options: -- -v --verbose - print a message for each file. -- -p --parents - create parent directories recursively. -- -o --override - override already existing files. +- -v --verbose - print a message for each file; +- -p --parents - create parent directories recursively; +- -o --override - override already existing files; +- --help - display help message and exit; +- --version - display version message and exit. # Usage example @@ -36,7 +38,7 @@ $ mkfile -v ~/file.txt # TODO -Add parameters: -- -T --text=string - include this text in all new files. -- --help -- --version +- Create binary files +- Default text parameter for all files +- Default binary parameter for all files in binary mode +- chmod parameter diff --git a/src/main.rs b/src/main.rs index 758473d..5550633 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,15 @@ -use std::collections::VecDeque; use std::env; use std::fs; use std::fs::File; use std::path::Path; fn main() { - // Collect args and remove the executable path - let mut args: VecDeque = env::args().collect(); - args.pop_front(); + // Collect args + let args: Vec = env::args().collect(); - // Return if no args - if args.is_empty() { + // Return if no args and print help message + if args.len() <= 1 { + print_help_msg(); return; } @@ -23,10 +22,11 @@ fn main() { let mut create_parents = false; let mut do_override = false; - for arg in &args { + // [1..] since the first arg is the executable path + for arg in &args[1..] { let a = arg.chars().nth(0); let b = arg.chars().nth(1); - let c = arg.chars().nth(3); + let c = arg.chars().nth(2); // Long option --option if a.eq(&Some('-')) && b.eq(&Some('-')) && c.is_some() { @@ -53,6 +53,14 @@ fn main() { "v" | "verbose" => is_verbose = true, "p" | "parents" => create_parents = true, "o" | "override" => do_override = true, + "help" => { + print_help_msg(); + return; + } + "version" => { + print_version_msg(); + return; + } _ => options_wrong.push(opt.to_string()), } } @@ -99,3 +107,18 @@ fn main() { }; } } + +fn print_help_msg() { + println!("Usage: mkfile [OPTION]... PATH..."); + println!("Create file(s), if they do not already exist.\n"); + println!("Options:"); + println!("-v --verbose print a message for each file"); + println!("-p --parents create parent directories recursively"); + println!("-o --override override already existing files"); + println!(" --help display this help and exit"); + println!(" --version output version information and exit (todo!)"); +} + +fn print_version_msg() { + println!("mkfile v{}", env!("CARGO_PKG_VERSION")); +}