Skip to content

awk: set ARGV and ARGC from the command line - #203

Open
VanshajPoonia wants to merge 7 commits into
uutils:mainfrom
VanshajPoonia:awk-argv-argc-172
Open

VanshajPoonia wants to merge 7 commits into
uutils:mainfrom
VanshajPoonia:awk-argv-argc-172

Conversation

@VanshajPoonia

Copy link
Copy Markdown
Contributor

Closes #172

ARGC was always 0 and ARGV was empty, so a program couldn't see its own operands. The symbol table already had set_argc_argv, but nothing called it.

Args::try_parse_args now keeps the operands as typed and builds the read queue from them and they fill ARGV before BEGIN runs. Options and the program text stay out, while assignments and - stay in. ARGV[0] is the name awk was run as, without its directory, like gawk.

Four new tests cover operands, no operands, options and program text left out and -f with operands. All four fail on main.

Not in this PR: changing ARGV or ARGC in BEGIN doesn't change which files get read yet.

Comment thread tests/by-util/test_awk.rs
"-F",
":",
"--",
"BEGIN { print ARGC, ARGV[1], ARGV[2], ARGV[3] }",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about awk 'BEGIN { print ARGV[1] }' -- x?
gawk stops options at the program text, so it keeps -- in ARGV, i think clap eats it here.
could you please add a test for it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for checking the -- case and clap was dropping it. It also read a -v after the program text as an option so awk 'BEGIN { print ARGV[1] }' -v x=1 hit the unimplemented -v path instead of printing -v.

4538426 puts a -- before the first operand (the program text, or the first input with -f), so clap stops there and everything after it stays in ARGV as typed, like gawk. 2cc00ce adds tests for -- x and -v x=1 after the program text for -f args.awk data -- -v, and for a -- before the first operand which is dropped by gawk.

I checked these against gawk 5.4.2 and they now match: prog -- x, prog a -- b, prog -- -- x, prog -v x=1, -- prog -- x, -f p a -- b, -f p -- a, -f p a -v b.

#201 also walks the options before clap. Whichever of the two lands second, I'll merge the two loops into one.

Comment thread src/cli.rs Outdated

/// gawk reads options only up to the first operand (the program text, or the first input
/// with -f) and keeps the rest as typed. A `--` there stops clap parsing them.
fn end_options_at_first_operand(mut args: Vec<OsString>) -> Vec<OsString> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

45 lines mirroring how clap parses options, could we avoid it?
if code and operands were one positional with trailing_var_arg, clap would stop at the first operand itself, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion, it works. In 26be876 the program text and the operands are one trailing positional, so clap stops at the first operand and keeps everything after it as typed, -- included. The 45 lines are gone and the gawk cases from my last reply still match.

Comment thread tests/by-util/test_awk.rs

#[test]
fn argv_keeps_a_double_dash_after_the_program_text() {
ucmd()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a test with attached values too, like -F: or --field-separator=: or a cluster -bF: before the program text?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, added in 9794dcb for -F:, --field-separator=: and -bF: before the program text. The test checks ARGC and ARGV only, because -F isn't applied to FS yet (awk -F: 'BEGIN { print FS }' prints an empty line, on main too). I can send that fix as a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased on main. Now that #206 applies -F, the attached-values test also checks FS in 7ef3ca4, and it matches gawk for -F:, --field-separator=: and -bF:.

ARGC was 0 and ARGV was empty, so a program could not see its own
operands. The operands are now kept as typed and fill ARGV before BEGIN
runs. Args::try_parse_args builds the read queue from them.

Options and the program text stay out, while assignments and "-" stay
in. ARGV[0] is the name awk was run as without its directory, like gawk.
gawk keeps everything after the program text, or after the first input with -f, as typed. clap dropped a `--` there and read a later -v as an option, so ARGV lost them. A `--` is now put before the first operand.
The program text (or the first input with -f or --source) and everything after it are one trailing positional, so clap stops reading options there and keeps the rest as typed, -- included. This replaces the walk that inserted a -- before the first operand.
-F:, --field-separator=: and the cluster -bF: each take their value and leave the program text and ARGV as gawk does.
Now that -F sets FS, the attached-value cases also check FS, as gawk prints it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

awk: ARGV and ARGC are never set

2 participants