From bd9285cc82fbedca35ad6408a3ccb63ecc190a81 Mon Sep 17 00:00:00 2001 From: "spinualexandru@outlook.com" Date: Fri, 20 Jun 2025 14:00:22 +0300 Subject: [PATCH 1/3] reject invalid args --- src/bin/edit/main.rs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/bin/edit/main.rs b/src/bin/edit/main.rs index dabef35847a..d2547f2f2b4 100644 --- a/src/bin/edit/main.rs +++ b/src/bin/edit/main.rs @@ -231,19 +231,41 @@ fn handle_args(state: &mut State) -> apperr::Result { let scratch = scratch_arena(None); let mut paths: Vec = Vec::new_in(&*scratch); let mut cwd = env::current_dir()?; + let mut args = env::args_os().skip(1); + let mut allow_positional = false; // The best CLI argument parser in the world. - for arg in env::args_os().skip(1) { - if arg == "-h" || arg == "--help" || (cfg!(windows) && arg == "/?") { - print_help(); - return Ok(true); - } else if arg == "-v" || arg == "--version" { - print_version(); - return Ok(true); - } else if arg == "-" { - paths.clear(); - break; + while let Some(arg) = args.next() { + if !allow_positional { + match arg.to_str() { + Some("-h") | Some("--help") => { + print_help(); + return Ok(true); + } + Some("/?") if cfg!(windows) => { + print_help(); + return Ok(true); + } + Some("-v") | Some("--version") => { + print_version(); + return Ok(true); + } + Some("-") | Some("--") => { + allow_positional = true; + if arg == "-" { + paths.clear(); + } + continue; + } + Some(s) if s.starts_with('-') => { + sys::write_stdout(&format!("Invalid option: {}\r\n", s)); + print_help(); + return Ok(true); + } + _ => {} + } } + let p = cwd.join(Path::new(&arg)); let p = path::normalize(&p); if !p.is_dir() { From e14b5214ad395cea916d0b4c5a573cef88033f42 Mon Sep 17 00:00:00 2001 From: "spinualexandru@outlook.com" Date: Fri, 20 Jun 2025 14:12:51 +0300 Subject: [PATCH 2/3] fix double dash not being skipped --- src/bin/edit/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/edit/main.rs b/src/bin/edit/main.rs index d2547f2f2b4..c796c4d843c 100644 --- a/src/bin/edit/main.rs +++ b/src/bin/edit/main.rs @@ -250,11 +250,13 @@ fn handle_args(state: &mut State) -> apperr::Result { print_version(); return Ok(true); } - Some("-") | Some("--") => { + Some("--") => { allow_positional = true; - if arg == "-" { - paths.clear(); - } + continue; + } + Some("-") => { + allow_positional = true; + paths.clear(); continue; } Some(s) if s.starts_with('-') => { From 39b8dc4db06c9ac2e109ab4aeed09800edb0870f Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Tue, 12 Aug 2025 14:16:08 +0200 Subject: [PATCH 3/3] Reduce diff size --- src/bin/edit/main.rs | 50 ++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/bin/edit/main.rs b/src/bin/edit/main.rs index 81f27dc3d54..85e8c5624b4 100644 --- a/src/bin/edit/main.rs +++ b/src/bin/edit/main.rs @@ -226,40 +226,26 @@ fn handle_args(state: &mut State) -> apperr::Result { let scratch = scratch_arena(None); let mut paths: Vec = Vec::new_in(&*scratch); let mut cwd = env::current_dir()?; - let mut args = env::args_os().skip(1); - let mut allow_positional = false; + let mut parse_args = true; // The best CLI argument parser in the world. - while let Some(arg) = args.next() { - if !allow_positional { - match arg.to_str() { - Some("-h") | Some("--help") => { - print_help(); - return Ok(true); - } - Some("/?") if cfg!(windows) => { - print_help(); - return Ok(true); - } - Some("-v") | Some("--version") => { - print_version(); - return Ok(true); - } - Some("--") => { - allow_positional = true; - continue; - } - Some("-") => { - allow_positional = true; - paths.clear(); - continue; - } - Some(s) if s.starts_with('-') => { - sys::write_stdout(&format!("Invalid option: {}\r\n", s)); - print_help(); - return Ok(true); - } - _ => {} + for arg in env::args_os().skip(1) { + if parse_args { + if arg == "--" { + parse_args = false; + continue; + } + if arg == "-" { + paths.clear(); + break; + } + if arg == "-h" || arg == "--help" || (cfg!(windows) && arg == "/?") { + print_help(); + return Ok(true); + } + if arg == "-v" || arg == "--version" { + print_version(); + return Ok(true); } }