feat: 🎸 My own command line parser

This commit is contained in:
Atsukoro1 2022-12-04 22:59:19 +01:00
parent 23502e6c53
commit f9b29bc154
7 changed files with 126 additions and 24 deletions

View file

@ -2,6 +2,3 @@
name = "ponyfetch"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.0.29", features = ["derive"] }

View file

@ -1,15 +0,0 @@
#[derive(clap::Parser)]
#[clap(
version = "1.0",
author = "Atsukoro1",
about = "Just a simple cross-platform neofetch for all the bronies out there."
)]
pub struct Args {
#[clap(short, long, required = false, default_value = "rainbowdash")]
/// Defines what ASCII pony to print.
pub pony: String,
#[clap(short, long, required = false, default_value = "green")]
/// Defines what color to print the output it
pub color: String
}

106
src/helpers/arguments.rs Normal file
View file

@ -0,0 +1,106 @@
use crate::helpers::colors::COLORS;
#[derive(Debug)]
pub struct Arguments {
pub help: bool,
pub color: String,
pub pony: String
}
impl Arguments {
fn validate_color(color: String) -> String {
for c in COLORS.iter() {
if c.0 == color {
return color.to_string();
}
};
Self::print_err("Invalid color provided.");
std::process::exit(1);
}
fn validate_pony(pony: String) -> String {
let ponies = crate::helpers::paths::get_ponies();
for p in ponies.iter() {
if p == &pony {
return pony.to_string();
}
};
Self::print_err("Invalid pony provided.");
std::process::exit(1);
}
fn print_help() {
println!("Usage: ponyfetch [OPTION]...");
println!("Prints a pony with system information.");
println!("
-h, --help Display this help and exit
-c, --color Set the color of the pony
-p, --pony Set the pony to display
");
std::process::exit(0);
}
fn print_err(err: &str) {
println!("Error: {}", err);
println!("Usage: ponyfetch [OPTION]...");
println!("Try 'ponyfetch --help' for more information.");
std::process::exit(1);
}
fn get_args(arg: String) -> String {
let args = arg.split("=").collect::<Vec<&str>>();
if args.len() < 2 {
Self::print_err("Invalid argument provided.");
};
args[1].to_string()
}
pub fn parse() -> Arguments {
let mut args = Arguments {
help: false,
color: String::from(""),
pony: String::from("")
};
let args_vec: Vec<String> = std::env::args().collect();
args_vec.into_iter().for_each(|arg| {
match arg {
arg if arg == "--help" || arg == "-h" => args.help = true,
arg if arg.contains("--color") || arg.contains("-c") => {
args.color = Self::validate_color(
Self::get_args(arg)
);
},
arg if arg.contains("--pony") || arg.contains("-p") => {
args.pony = Self::validate_pony(
Self::get_args(arg)
);
},
_ => ()
}
});
if args.color == "" {
args.color = String::from("blue");
}
if args.pony == "" {
args.pony = String::from("rainbowdash");
}
if args.help {
Self::print_help();
}
args
}
}

View file

@ -1,4 +1,4 @@
const COLORS : [(&str, &str); 34] = [
pub const COLORS : [(&str, &str); 34] = [
("black", "\u{001b}[30m"),
("red", "\u{001b}[31m"),
("green", "\u{001b}[32m"),

View file

@ -1,3 +1,4 @@
pub mod arguments;
pub mod file;
pub mod colors;
pub mod print;

View file

@ -1,5 +1,19 @@
use std::path::PathBuf;
pub fn get_ponies() -> Vec<String> {
let mut ponies: Vec<String> = Vec::new();
let pony_files = std::fs::read_dir(get_pony_path()).unwrap();
pony_files.for_each(|file| {
let file = file.unwrap();
let file_name = file.file_name().into_string().unwrap();
let file_name = file_name.split(".").collect::<Vec<&str>>()[0].to_string();
ponies.push(file_name);
});
ponies
}
#[cfg(target_os = "linux")]
pub fn get_pony_path() -> String {
if std::env::current_exe().unwrap().to_str().unwrap().contains("target") {

View file

@ -1,9 +1,7 @@
use clap::Parser;
use args::Args;
use helpers::arguments::Arguments;
mod helpers;
mod system;
mod args;
#[derive(Clone, Copy, Debug)]
pub enum ActionType {
@ -110,7 +108,8 @@ const ACTIONS: [Action; 13] = [
];
fn main() {
let args: Args = Args::parse();
let args = Arguments::parse();
println!("{:?}", args);
let line_count = helpers::file::get_file_linecount(
&format!("{}{}.txt", helpers::paths::get_pony_path(), &args.pony)