added flag processing

This commit is contained in:
Jonas_Jones 2022-09-20 01:12:36 +02:00
parent 28ae6a79e7
commit 674333b030

View file

@ -1,22 +1,57 @@
use std::env;
mod cliargs;
mod errorhandler;
fn main() {
fn main()
{
let args: Vec<String> = env::args().collect();
for i in args.iter()
let flags: Vec<&str> = ["-h", "--help", "-v", "--version", "-i", "--install"];
flagcheck(args, flags);
flagprocessing(args, flags);
}
// NEW CONCEPT CAUSE OLD ONE IS GARBAGE:
// - move args from old to new vector one by one when checked
// - spit out error when leftover elements in old vector
// - process elements of new vector
fn flagcheck(args: Vec<String>, flags: Vec<&str>)
{
let mut skip = 1; //first element in flags vector always skipped since it's the scripts directory and therefore garbage data
for c in args.iter()
{
match i.as_str()
if !flags.contains(&c)
{
"--help" => cliargs::help(),
_ => {
if !args.iter().position(|r| r.as_str() == i).unwrap().parse() == 0
{
println!("Error")
errorhandler::error(c.as_str());
std::process::exit(-1);
} else {
println!("{}", args.iter().position(|r| r.as_str() == i).unwrap());
match c.as_str()
{
"-i" => skip += 1
}
},
}
}
}
fn flagprocessing(args: Vec<String>, flags: Vec<&str>)
{
let mut skip = 1; //first element in flags vector always skipped since it's the scripts directory and therefore garbage data
for c in args.iter()
{
if skip == 0
{
match c.as_str()
{
"--help" => cliargs::help(),
_ => {
errorhandler::unexpectederror();
std::process::exit(-1);
},
}
} else {
skip -= 1;
}
}
}