Repository navigation
awk: set ARGV and ARGC from the command line - #203
VanshajPoonia wants to merge 7 commits into
Conversation
| "-F", | ||
| ":", | ||
| "--", | ||
| "BEGIN { print ARGC, ARGV[1], ARGV[2], ARGV[3] }", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
|
||
| /// 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> { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
|
||
| #[test] | ||
| fn argv_keeps_a_double_dash_after_the_program_text() { | ||
| ucmd() |
There was a problem hiding this comment.
could you please add a test with attached values too, like -F: or --field-separator=: or a cluster -bF: before the program text?
There was a problem hiding this comment.
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.
9794dcb to
76356c0
Compare
76356c0 to
7ef3ca4
Compare
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.
7ef3ca4 to
71449dc
Compare
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_argsnow 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
-fwith operands. All four fail on main.Not in this PR: changing ARGV or ARGC in BEGIN doesn't change which files get read yet.