mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
refactor: rewrite handle_args() with iterator (codeberg #6)
Reviewed-on: https://codeberg.org/iff/pay-respects/pulls/6 Co-authored-by: Integral <integral@member.fsf.org> Co-committed-by: Integral <integral@member.fsf.org>
This commit is contained in:
parent
1227fc88ee
commit
7c74d13705
1 changed files with 17 additions and 25 deletions
|
|
@ -8,15 +8,18 @@ pub enum Status {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
|
pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
|
||||||
let args: Vec<_> = args.into_iter().collect();
|
let mut iter = args.into_iter().peekable();
|
||||||
if args.len() <= 1 {
|
let mut init = Init::new();
|
||||||
|
if let Some(binary_path) = iter.next() {
|
||||||
|
init.binary_path = binary_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
if iter.peek().is_none() {
|
||||||
return Status::Continue;
|
return Status::Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut init = Init::new();
|
while let Some(arg) = iter.next() {
|
||||||
let mut index = 1;
|
match arg.as_str() {
|
||||||
while index < args.len() {
|
|
||||||
match args[index].as_str() {
|
|
||||||
"-h" | "--help" => {
|
"-h" | "--help" => {
|
||||||
print_help();
|
print_help();
|
||||||
return Status::Exit;
|
return Status::Exit;
|
||||||
|
|
@ -26,27 +29,18 @@ pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
|
||||||
return Status::Exit;
|
return Status::Exit;
|
||||||
}
|
}
|
||||||
"-a" | "--alias" => {
|
"-a" | "--alias" => {
|
||||||
if args.len() > index + 1 {
|
match iter.peek() {
|
||||||
if args[index + 1].starts_with('-') {
|
Some(next_arg) if !next_arg.starts_with('-') => {
|
||||||
init.alias = String::from("f");
|
init.alias = next_arg.to_string();
|
||||||
} else {
|
iter.next();
|
||||||
init.alias = args[index + 1].clone();
|
|
||||||
index += 1;
|
|
||||||
}
|
}
|
||||||
} else {
|
_ => init.alias = String::from("f"),
|
||||||
init.alias = String::from("f");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init.auto_alias = true;
|
init.auto_alias = true;
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
"--nocnf" => {
|
|
||||||
init.cnf = false;
|
|
||||||
index += 1
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
init.shell = args[index].clone();
|
|
||||||
index += 1
|
|
||||||
}
|
}
|
||||||
|
"--nocnf" => init.cnf = false,
|
||||||
|
_ => init.shell = arg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,8 +49,6 @@ pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
|
||||||
return Status::Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
init.binary_path = args[0].clone();
|
|
||||||
|
|
||||||
initialization(&mut init);
|
initialization(&mut init);
|
||||||
Status::Exit
|
Status::Exit
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue