Skip to content

Commit

Permalink
Version 0.2.1
Browse files Browse the repository at this point in the history
- Changed args type to Vec<String> (was VecDeque<String>)
- Added help command support
- Added version command support
  • Loading branch information
AlexWanderman committed Oct 31, 2023
1 parent b744b49 commit ecc68b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[package]
name = "mkfile"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
18 changes: 10 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
39 changes: 31 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> = env::args().collect();
args.pop_front();
// Collect args
let args: Vec<String> = 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;
}

Expand All @@ -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() {
Expand All @@ -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()),
}
}
Expand Down Expand Up @@ -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"));
}

0 comments on commit ecc68b3

Please sign in to comment.