Skip to content

Commit

Permalink
Add long options --help, --version
Browse files Browse the repository at this point in the history
Also added a version string, which needs to be updated on every change. Compiling now requires non-POSIX getopt_long() function. (No effect on toybox awk.)
  • Loading branch information
raygard committed Oct 9, 2024
1 parent 932256d commit 6721762
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2024-10-08
- Fix bug assigning NF=0; add toybox test for it
- Fix bug assigning NF negative
- Add long options --help, --version

## 2024-10-05
- Mute clang warnings
Expand Down
22 changes: 18 additions & 4 deletions monosrc/mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include <errno.h>
#include <assert.h>

// for getopt():
// for isatty():
#include <unistd.h>
// for getopt_long():
#include <getopt.h>
#include <regex.h>
#if defined(__unix__) || defined(linux)
#include <langinfo.h>
Expand Down Expand Up @@ -4745,6 +4747,10 @@ static void run(int optind, int argc, char **argv, char *sepstring,
//// main
////////////////////

#ifndef FOR_TOYBOX
char *version = "24.10 20241008";

#endif // FOR_TOYBOX
static void progfiles_init(char *progstring, struct arg_list *prog_args)
{
TT.scs->p = progstring ? progstring : &(" "[2]); // Quiet clang warning on " " + 2;
Expand Down Expand Up @@ -4802,7 +4808,9 @@ int main(int argc, char **argv)
"awk [-F sepstring] -f progfile [-f progfile]...\n"
" [-v assignment]... [argument...]\n"
"also:\n"
"-V show version\n"
"-V or --version show version\n"
"-h or --help show this usage screen\n"

"-b use bytes, not characters\n"
"-c compile only, do not run\n"
};
Expand All @@ -4819,7 +4827,9 @@ int main(int argc, char **argv)
struct arg_list *prog_args = 0, **tail_prog_args = &prog_args;
struct arg_list *assign_args = 0, **tail_assign_args = &assign_args;

while ((opt = getopt(argc, argv, "F:f:v:Vbc")) != -1) {
struct option longopts[] = {{"version", 0, 0, 'V'}, {"help", 0, 0, 'h'}, {0}};

while ((opt = getopt_long(argc, argv, "F:f:v:Vbch", longopts, 0)) != -1) {
switch (opt) {
case 'F':
sepstring = escape_str(optarg, 0);
Expand All @@ -4834,12 +4844,16 @@ int main(int argc, char **argv)
tail_assign_args = new_arg(tail_assign_args, optarg);
break;
case 'V':
printf("<%s %s>\n", __DATE__, __TIME__);
printf("version %s, compiled %s %s\n", version, __DATE__, __TIME__);
awk_exit(0);
break;
case 'c':
opt_run_prog = 0;
break;
case 'h':
printf("%s", usage);
exit(0);
break;
break;
default:
error_exit("Option error.\n%s", usage);
Expand Down
4 changes: 3 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#include <errno.h>
#include <assert.h>

// for getopt():
// for isatty():
#include <unistd.h>
// for getopt_long():
#include <getopt.h>
#include <regex.h>
#if defined(__unix__) || defined(linux)
#include <langinfo.h>
Expand Down
18 changes: 15 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
//// main
////////////////////

#ifndef FOR_TOYBOX
char *version = "24.10 20241008";

#endif // FOR_TOYBOX
static void progfiles_init(char *progstring, struct arg_list *prog_args)
{
TT.scs->p = progstring ? progstring : &(" "[2]); // Quiet clang warning on " " + 2;
Expand Down Expand Up @@ -66,7 +70,9 @@ int main(int argc, char **argv)
"awk [-F sepstring] -f progfile [-f progfile]...\n"
" [-v assignment]... [argument...]\n"
"also:\n"
"-V show version\n"
"-V or --version show version\n"
"-h or --help show this usage screen\n"

"-b use bytes, not characters\n"
"-c compile only, do not run\n"
};
Expand All @@ -83,7 +89,9 @@ int main(int argc, char **argv)
struct arg_list *prog_args = 0, **tail_prog_args = &prog_args;
struct arg_list *assign_args = 0, **tail_assign_args = &assign_args;

while ((opt = getopt(argc, argv, "F:f:v:Vbc")) != -1) {
struct option longopts[] = {{"version", 0, 0, 'V'}, {"help", 0, 0, 'h'}, {0}};

while ((opt = getopt_long(argc, argv, "F:f:v:Vbch", longopts, 0)) != -1) {
switch (opt) {
case 'F':
sepstring = escape_str(optarg, 0);
Expand All @@ -98,12 +106,16 @@ int main(int argc, char **argv)
tail_assign_args = new_arg(tail_assign_args, optarg);
break;
case 'V':
printf("<%s %s>\n", __DATE__, __TIME__);
printf("version %s, compiled %s %s\n", version, __DATE__, __TIME__);
awk_exit(0);
break;
case 'c':
opt_run_prog = 0;
break;
case 'h':
printf("%s", usage);
exit(0);
break;
break;
default:
error_exit("Option error.\n%s", usage);
Expand Down

0 comments on commit 6721762

Please sign in to comment.