Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
#[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<DebugMode>,
#[arg(short = 'E', long)]
pub exec: Option<PathBuf>,
Expand All @@ -61,7 +62,7 @@ pub struct Args {
pub trace: bool,
#[arg(short = 'l', long)]
pub load: Vec<OsString>,
#[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<LintMode>,
#[arg(short = 'M', long)]
pub bignum: bool,
Expand All @@ -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`]"
)]
Expand All @@ -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`]"
)]
Expand Down Expand Up @@ -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<Item = OsString>) -> Vec<OsString> {
// 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<OsString> = 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),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -34,7 +34,7 @@ fn main() {
}

fn uu_main() -> Result<(), Box<dyn Error>> {
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()?;
Expand Down
47 changes: 47 additions & 0 deletions tests/by-util/test_awk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}
Loading