Skip to content

Commit

Permalink
new: added Cmd#parse
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed May 26, 2024
1 parent e78bc01 commit 4ed6be6
Show file tree
Hide file tree
Showing 3 changed files with 717 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the file LICENSE in this distribution for more details.

mod errors;
mod parse;

use std::collections::HashMap;
use std::env;
Expand Down
178 changes: 178 additions & 0 deletions src/parse/mod.rs
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()
}
Loading

0 comments on commit 4ed6be6

Please sign in to comment.