-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
717 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
mod parse; | ||
|
||
use crate::Error; | ||
use crate::OptionError; | ||
|
||
fn parse_args<'a, F1, F2, F3>( | ||
args: &[&'a str], | ||
mut collect_args: F1, | ||
mut collect_opts: F2, | ||
take_args: F3, | ||
) -> Result<(), Error<'a>> | ||
where | ||
F1: FnMut(&'a str), | ||
F2: FnMut(&'a str, Option<&'a str>) -> Result<(), Error<'a>>, | ||
F3: Fn(&str) -> bool, | ||
{ | ||
let mut is_non_opt = false; | ||
let mut prev_opt_taking_args = ""; | ||
let mut first_err: Option<Error> = None; | ||
|
||
'L0: for (i_arg, arg) in args.iter().enumerate() { | ||
if is_non_opt { | ||
collect_args(arg); | ||
} else if !prev_opt_taking_args.is_empty() { | ||
match collect_opts(prev_opt_taking_args, Some(arg)) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
continue 'L0; | ||
} | ||
Ok(_) => {} | ||
} | ||
prev_opt_taking_args = ""; | ||
} else if arg.starts_with("--") { | ||
if arg.len() == 2 { | ||
is_non_opt = true; | ||
continue 'L0; | ||
} | ||
|
||
let arg = &arg[2..]; | ||
let mut i = 0; | ||
|
||
for ch in arg.chars() { | ||
if i > 0 { | ||
if ch == '=' { | ||
match collect_opts(&arg[0..i], Some(&arg[i + 1..])) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
continue 'L0; | ||
} | ||
Ok(_) => {} | ||
} | ||
break; | ||
} | ||
if !is_allowed_character(ch) { | ||
if first_err == None { | ||
first_err = Some(Error::InvalidOption( | ||
OptionError::OptionContainsInvalidChar { option: arg }, | ||
)); | ||
} | ||
continue 'L0; | ||
} | ||
} else { | ||
if !is_allowed_first_character(ch) { | ||
if first_err == None { | ||
first_err = Some(Error::InvalidOption( | ||
OptionError::OptionContainsInvalidChar { option: arg }, | ||
)); | ||
} | ||
continue 'L0; | ||
} | ||
} | ||
i += 1; | ||
} | ||
|
||
if i == arg.len() { | ||
if take_args(arg) && i_arg < args.len() - 1 {} | ||
match collect_opts(arg, None) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
continue 'L0; | ||
} | ||
Ok(_) => {} | ||
} | ||
} | ||
} else if arg.starts_with("-") { | ||
if arg.len() == 1 { | ||
collect_args(arg); | ||
continue 'L0; | ||
} | ||
|
||
let arg = &arg[1..]; | ||
let mut name: &str = ""; | ||
let mut i = 0; | ||
|
||
for ch in arg.chars() { | ||
if i > 0 { | ||
if ch == '=' { | ||
if !name.is_empty() { | ||
match collect_opts(name, Some(&arg[i + 1..])) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
} | ||
Ok(_) => {} | ||
} | ||
} | ||
continue 'L0; | ||
} | ||
if !name.is_empty() { | ||
match collect_opts(name, None) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
} | ||
Ok(_) => {} | ||
} | ||
} | ||
} | ||
if !is_allowed_first_character(ch) { | ||
if first_err == None { | ||
first_err = Some(Error::InvalidOption( | ||
OptionError::OptionContainsInvalidChar { | ||
option: &arg[i..i + 1], | ||
}, | ||
)); | ||
} | ||
name = ""; | ||
} else { | ||
name = &arg[i..i + 1]; | ||
} | ||
i += 1; | ||
} | ||
|
||
if i == arg.len() && !name.is_empty() { | ||
if take_args(name) && i_arg < args.len() - 1 { | ||
prev_opt_taking_args = name; | ||
} else { | ||
match collect_opts(name, None) { | ||
Err(err) => { | ||
if first_err == None { | ||
first_err = Some(err); | ||
} | ||
continue 'L0; | ||
} | ||
Ok(_) => {} | ||
} | ||
} | ||
} | ||
} else { | ||
collect_args(arg); | ||
} | ||
} | ||
|
||
match first_err { | ||
Some(err) => Err(err), | ||
None => Ok(()), | ||
} | ||
} | ||
|
||
fn is_allowed_character(ch: char) -> bool { | ||
ch == '-' || ch.is_ascii_alphabetic() || ch.is_ascii_digit() | ||
} | ||
|
||
fn is_allowed_first_character(ch: char) -> bool { | ||
ch.is_ascii_alphabetic() | ||
} |
Oops, something went wrong.