Skip to content

Commit

Permalink
Fix ux::check_args. Close (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakivo committed Feb 22, 2025
1 parent ff8f3bf commit 347b7d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use flags::Flags;
use cr::CommandRunner;
use graph::build_dependency_graph;
use parser::{comp, Parser, read_file};
use ux::{/*check_args, */did_you_mean_compiled};
use ux::{check_args, did_you_mean_compiled};

use std::env;
use std::process::{exit, ExitCode};
Expand All @@ -29,14 +29,14 @@ use bumpalo::Bump;
use flager::Parser as FlagParser;

fn main() -> ExitCode {
// let args = env::args().collect::<Vec::<_>>();
// if let Some(undefined_flag) = check_args(&args) {
// eprintln!{
// "did you mean: {program} -t {undefined_flag} [..flags]?",
// program = args[0],
// };
// return ExitCode::FAILURE
// }
let args = env::args().collect::<Vec::<_>>();
if let Some(undefined_flag) = check_args(&args) {
eprintln!{
"did you mean: {program} -t {undefined_flag} [..flags]?",
program = args[0],
};
return ExitCode::FAILURE
}

let flag_parser = FlagParser::new();
let flags = Flags::new(&flag_parser);
Expand Down
14 changes: 10 additions & 4 deletions src/ux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::parser::comp::Job;
use crate::flags::{FLAG_STRS, MODE_STRS};
use crate::edit_distance::find_best_match_for_name;

#[allow(unused)]
pub fn check_args<'a>(args: &'a [String]) -> Option::<&'a String> {
let mut prev_is_value = false;

args.get(1)
.filter(|f| args.len() == 2 && !MODE_STRS.contains(&f.as_str()))
.inspect(|v| {
Expand All @@ -24,17 +25,22 @@ pub fn check_args<'a>(args: &'a [String]) -> Option::<&'a String> {
let v = w[1].as_str();

if v.as_bytes().first().map_or(false, |&b| b == b'-') &&
!FLAG_STRS.contains(&f)
!FLAG_STRS.contains(&f) &&
!prev_is_value
{
eprintln!("undefined flag: {v}");
if let Some(flag) = did_you_mean_flags(v) {
eprintln!("did you mean: {flag}?")
} exit(1)
}

if FLAG_STRS.contains(&f) {
prev_is_value = true
}

!FLAG_STRS.contains(&f) &&
!FLAG_STRS.contains(&v) &&
!MODE_STRS.contains(&v)
!FLAG_STRS.contains(&v) &&
!MODE_STRS.contains(&v)
}).map(|w| &w[1]))
}

Expand Down

0 comments on commit 347b7d5

Please sign in to comment.