add missing -yes flags to command definitions, use log crate instead of printlns in command parser, also accept double dash for flags

This commit is contained in:
dawn 2026-01-26 02:22:54 +03:00
parent 8431255930
commit 12655fb539
No known key found for this signature in database
16 changed files with 130 additions and 53 deletions

View file

@ -8,4 +8,5 @@ lazy_static = { workspace = true }
smol_str = "0.3.2"
ordermap = "0.5"
regex = "1"
strsim = "0.11"
strsim = "0.11"
log = "0.4"

View file

@ -18,6 +18,7 @@ use std::{collections::HashMap, usize};
use command::Command;
use flag::{Flag, FlagMatchError, FlagValueMatchError};
use log::debug;
use parameter::ParameterValue;
use smol_str::SmolStr;
use string::MatchedFlag;
@ -82,9 +83,9 @@ pub fn parse_command(
(_, Token::Parameter(_)) => std::cmp::Ordering::Less,
_ => std::cmp::Ordering::Equal,
});
println!("possible: {:?}", possible_tokens);
debug!("possible: {:?}", possible_tokens);
let next = next_token(possible_tokens.iter().cloned(), &input, current_pos);
println!("next: {:?}", next);
debug!("next: {:?}", next);
match &next {
Some((found_token, result, new_pos)) => {
match &result {
@ -156,7 +157,7 @@ pub fn parse_command(
.pop()
.and_then(|m| matches!(m.token, Token::Parameter(_)).then_some(m))
{
println!("redoing previous branch: {:?}", state.token);
debug!("redoing previous branch: {:?}", state.token);
local_tree = state.tree;
current_pos = state.start_pos; // reset position to previous branch's start
filtered_tokens = state.filtered_tokens; // reset filtered tokens to the previous branch's
@ -251,7 +252,7 @@ pub fn parse_command(
// match flags until there are none left
while let Some(matched_flag) = string::next_flag(&input, current_pos) {
current_pos = matched_flag.next_pos;
println!("flag matched {matched_flag:?}");
debug!("flag matched {matched_flag:?}");
raw_flags.push((current_token_idx, matched_flag));
}
// if we have a command, stop parsing and return it (only if there is no remaining input)
@ -355,7 +356,7 @@ pub fn parse_command(
flags.insert(name.to_string(), value.clone());
}
println!("{} {flags:?} {params:?}", full_cmd.cb);
debug!("{} {flags:?} {params:?}", full_cmd.cb);
return Ok(ParsedCommand {
command_def: full_cmd.clone(),
flags,
@ -371,7 +372,7 @@ fn match_flag<'a>(
) -> Option<Result<(SmolStr, Option<ParameterValue>), (&'a Flag, FlagMatchError)>> {
// check for all (possible) flags, see if token matches
for flag in possible_flags {
println!("matching flag {flag:?}");
debug!("matching flag {flag:?}");
match flag.try_match(matched_flag.name, matched_flag.value) {
Some(Ok(param)) => return Some(Ok((flag.get_name().into(), param))),
Some(Err(err)) => return Some(Err((flag, err))),
@ -397,7 +398,7 @@ fn next_token<'a>(
) -> Option<(Token, TokenMatchResult, usize)> {
// get next parameter, matching quotes
let matched = string::next_param(&input, current_pos);
println!("matched: {matched:?}\n---");
debug!("matched: {matched:?}\n---");
// iterate over tokens and run try_match
for token in possible_tokens {
@ -421,7 +422,7 @@ fn next_token<'a>(
};
match token.try_match(input_to_match) {
Some(result) => {
//println!("matched token: {}", token);
//debug!("matched token: {}", token);
return Some((token.clone(), result, next_pos));
}
None => {} // continue matching until we exhaust all tokens
@ -448,7 +449,7 @@ fn rank_possible_commands(
.map(move |(display, scoring, is_alias)| {
let similarity = strsim::jaro_winkler(&input, &scoring);
// if similarity > 0.7 {
// println!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
// debug!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
// }
(cmd, display, similarity, is_alias)
})

View file

@ -1,5 +1,6 @@
use std::collections::HashMap;
use log::debug;
use smol_str::{SmolStr, ToSmolStr};
lazy_static::lazy_static! {
@ -79,8 +80,8 @@ pub(super) fn next_param<'a>(input: &'a str, current_pos: usize) -> Option<Match
let leading_whitespace_count =
input[..current_pos].len() - input[..current_pos].trim_start().len();
let substr_to_match = &input[current_pos + leading_whitespace_count..];
println!("stuff: {input} {current_pos} {leading_whitespace_count}");
println!("to match: {substr_to_match}");
debug!("stuff: {input} {current_pos} {leading_whitespace_count}");
debug!("to match: {substr_to_match}");
if let Some(end_quote_pos) = find_quotes(substr_to_match) {
// return quoted string, without quotes
@ -132,20 +133,27 @@ pub(super) fn next_flag<'a>(input: &'a str, mut current_pos: usize) -> Option<Ma
return None;
}
println!("flag input {substr_to_match}");
debug!("flag input {substr_to_match}");
// strip the -
let Some(substr_to_match) = substr_to_match.strip_prefix('-') else {
let original_len = substr_to_match.len();
let substr_without_dashes = substr_to_match.trim_start_matches('-');
let dash_count = original_len - substr_without_dashes.len();
if dash_count == 0 || dash_count > 2 {
// if it doesn't have one, then it is not a flag
// or if it has more dashes than 2, assume its not a flag
return None;
};
current_pos += 1;
}
let substr_to_match = substr_without_dashes;
current_pos += dash_count;
// try finding = or whitespace
for (pos, char) in substr_to_match.char_indices() {
println!("flag find char {char} at {pos}");
debug!("flag find char {char} at {pos}");
if char == '=' {
let name = &substr_to_match[..pos];
println!("flag find {name}");
debug!("flag find {name}");
// try to get the value
let Some(param) = next_param(input, current_pos + pos + 1) else {
return Some(MatchedFlag {