diff --git a/src/cli.rs b/src/cli.rs index 08b4500..678a49e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -45,11 +45,12 @@ pub struct Args { short = 'd', long, num_args = 0..=1, + require_equals = true, default_missing_value = "./awkvars.out", help = "[default value, if empty: `./awkvars.out`]" )] pub dump_variables: Option, - #[arg(short = 'D', long, num_args = 0..=1, default_missing_value = "", value_parser = parse_debug_mode)] + #[arg(short = 'D', long, num_args = 0..=1, require_equals = true, default_missing_value = "", value_parser = parse_debug_mode)] pub debug: Option, #[arg(short = 'E', long)] pub exec: Option, @@ -61,7 +62,7 @@ pub struct Args { pub trace: bool, #[arg(short = 'l', long)] pub load: Vec, - #[arg(short = 'L', long, num_args = 0..=1, default_missing_value = "basic", value_enum)] + #[arg(short = 'L', long, num_args = 0..=1, require_equals = true, default_missing_value = "basic", value_enum)] pub lint: Vec, #[arg(short = 'M', long)] pub bignum: bool, @@ -72,6 +73,7 @@ pub struct Args { #[arg(short = 'o', long, num_args = 0..=1, + require_equals = true, default_missing_value = "./awkprof.out", help = "[default value, if empty: `./awkprof.out`]" )] @@ -83,6 +85,7 @@ pub struct Args { #[arg(short = 'p', long, num_args = 0..=1, + require_equals = true, default_missing_value = "./awkprof.out", help = "[default value, if empty: `./awkprof.out`]" )] @@ -180,6 +183,40 @@ impl clap::Args for ProgramSources { } } +/// The short options `-d`, `-D`, `-L`, `-o` and `-p` take an optional value, which gawk only +/// reads when it is attached (`-Lfatal`). clap needs the `=` for that (`-L=fatal`), so add it. +pub fn attach_short_values(args: impl IntoIterator) -> Vec { + // Short options that take their value from the next argument. + const TAKES_VALUE: [&[u8]; 7] = [b"-f", b"-F", b"-v", b"-e", b"-E", b"-i", b"-l"]; + + let mut args = args.into_iter(); + let mut out: Vec = args.next().into_iter().collect(); // program name + while let Some(arg) = args.next() { + match arg.as_encoded_bytes() { + [b'-', b'd' | b'D' | b'L' | b'o' | b'p', first, ..] if *first != b'=' => { + let mut bytes = arg.as_encoded_bytes().to_vec(); + bytes.insert(2, b'='); + // SAFETY: only an ASCII byte was inserted between two ASCII bytes. + out.push(unsafe { OsString::from_encoded_bytes_unchecked(bytes) }); + } + bytes @ [b'-', ..] if bytes != b"-" && bytes != b"--" => { + let takes_value = TAKES_VALUE.contains(&bytes); + out.push(arg); + if takes_value { + out.extend(args.next()); + } + } + // The program text, an operand or `--`: the rest is not an option. + _ => { + out.push(arg); + break; + } + } + } + out.extend(args); + out +} + #[derive(Clone, Debug)] pub enum ArgQueueItem { File(PathBuf), diff --git a/src/main.rs b/src/main.rs index 97d4f4a..59d4309 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ use interpreter::{CodeGen, ExecMode, Interpreter}; use parser::{FileCache, IncludeError, Parser}; use crate::{ - cli::{Args, KeyValue}, + cli::{Args, KeyValue, attach_short_values}, event::AwkRt, utils::{AWK_FATAL_CODE, EXIT_FAILURE, ensure_consistent_panic, exit_err}, }; @@ -34,7 +34,7 @@ fn main() { } fn uu_main() -> Result<(), Box> { - let args = match Args::try_parse_args(args_os()) { + let args = match Args::try_parse_args(attach_short_values(args_os())) { Ok(args) => args, Err(msg) => { msg.print()?; diff --git a/tests/by-util/test_awk.rs b/tests/by-util/test_awk.rs index 9ebb394..1f00ac2 100644 --- a/tests/by-util/test_awk.rs +++ b/tests/by-util/test_awk.rs @@ -1608,3 +1608,50 @@ fn fatal_error_skips_end() { .fails_with_code(2) .no_stdout(); } + +#[test] +fn optional_option_values_must_be_attached() { + for opt in ["--lint", "--profile", "--dump-variables", "-L", "-p", "-d"] { + ucmd() + .args(&[opt, r#"BEGIN { print "prog" }"#]) + .succeeds() + .stdout_only("prog\n"); + } +} + +#[test] +fn optional_option_values_can_be_attached() { + for opt in [ + "--lint=fatal", + "-Lfatal", + "-L=fatal", + "--profile=prof.out", + "-pprof.out", + "--dump-variables=vars.out", + "-dvars.out", + ] { + ucmd() + .args(&[opt, r#"BEGIN { print "prog" }"#]) + .succeeds() + .stdout_only("prog\n"); + } +} + +#[test] +fn attached_optional_value_with_a_program_file() { + let ts = uutests::util::TestScenario::new("awk"); + ts.fixtures + .write("prog.awk", r#"BEGIN { print "from file" }"#); + ts.cmd(crate::TESTS_BINARY) + .args(&["-Lfatal", "-f", "prog.awk"]) + .succeeds() + .stdout_only("from file\n"); +} + +#[test] +fn invalid_lint_value_is_rejected() { + ucmd() + .args(&["--lint=bogus", "BEGIN { }"]) + .fails_with_code(1) + .stderr_contains("invalid value 'bogus'"); +}