From 0b2f843db9e53d54bff642436c88fe5db8b08e0a Mon Sep 17 00:00:00 2001 From: Evan Typanski Date: Tue, 7 Jan 2025 11:39:38 -0500 Subject: [PATCH] Try to be more careful with `--help` printing Namely, this will: - print --help to stdout rather than stderr - not print full usage when errors happen - add a prompt to use --help for more info on error This should make it more greppable, clearer, and less spammy. --- NEWS | 5 + src/Options.cc | 287 ++++++++++++++++++++++++---------------------- src/Options.h | 5 +- src/zeek-setup.cc | 6 +- 4 files changed, 164 insertions(+), 139 deletions(-) diff --git a/NEWS b/NEWS index aa267bf22d..ad99446a80 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,11 @@ New Functionality Changed Functionality --------------------- +- Command line options processing will no longer print usage whenever there + is an error. Instead, issues in command line processing will print an error, + then prompt to use --help. The --help usage will now print to standard output + rather than standard error. + Removed Functionality ------------------------ diff --git a/src/Options.cc b/src/Options.cc index fc7a639e61..9a21eae1d1 100644 --- a/src/Options.cc +++ b/src/Options.cc @@ -72,145 +72,144 @@ bool fake_dns() { return getenv("ZEEK_DNS_FAKE"); } extern const char* zeek_version(); -void usage(const char* prog, int code) { - fprintf(stderr, "zeek version %s\n", zeek_version()); +void prompt_for_help(const char* prog) { fprintf(stderr, "Try '%s --help' for more information.\n", prog); } - fprintf(stderr, "usage: %s [options] [file ...]\n", prog); - fprintf(stderr, "usage: %s --test [doctest-options] -- [options] [file ...]\n", prog); - fprintf(stderr, " | Zeek script file, or read stdin\n"); - fprintf(stderr, " -a|--parse-only | exit immediately after parsing scripts\n"); - fprintf(stderr, " -b|--bare-mode | don't load scripts from the base/ directory\n"); - fprintf(stderr, " -c|--capture-unprocessed | write unprocessed packets to a tcpdump file\n"); - fprintf(stderr, " -d|--debug-script | activate Zeek script debugging\n"); - fprintf(stderr, " -e|--exec | augment loaded scripts by given code\n"); - fprintf(stderr, " -f|--filter | tcpdump filter\n"); - fprintf(stderr, " -h|--help | command line help\n"); - fprintf(stderr, " -i|--iface | read from given interface (only one allowed)\n"); - fprintf(stderr, " -p|--prefix | add given prefix to Zeek script file resolution\n"); - fprintf(stderr, - " -r|--readfile | read from given tcpdump file (only one " - "allowed, pass '-' as the filename to read from stdin)\n"); - fprintf(stderr, " -s|--rulefile | read rules from given file\n"); - fprintf(stderr, " -t|--tracefile | activate execution tracing\n"); - fprintf(stderr, " -u|--usage-issues | find variable usage issues and exit\n"); - fprintf(stderr, - " --no-unused-warnings | suppress warnings of unused " - "functions/hooks/events\n"); - fprintf(stderr, " -v|--version | print version and exit\n"); - fprintf(stderr, " -V|--build-info | print build information and exit\n"); - fprintf(stderr, " -w|--writefile | write to given tcpdump file\n"); +void usage(const char* prog) { + printf("zeek version %s\n", zeek_version()); + + printf("usage: %s [options] [file ...]\n", prog); + printf("usage: %s --test [doctest-options] -- [options] [file ...]\n", prog); + printf(" | Zeek script file, or read stdin\n"); + printf(" -a|--parse-only | exit immediately after parsing scripts\n"); + printf(" -b|--bare-mode | don't load scripts from the base/ directory\n"); + printf(" -c|--capture-unprocessed | write unprocessed packets to a tcpdump file\n"); + printf(" -d|--debug-script | activate Zeek script debugging\n"); + printf(" -e|--exec | augment loaded scripts by given code\n"); + printf(" -f|--filter | tcpdump filter\n"); + printf(" -h|--help | command line help\n"); + printf(" -i|--iface | read from given interface (only one allowed)\n"); + printf(" -p|--prefix | add given prefix to Zeek script file resolution\n"); + printf( + " -r|--readfile | read from given tcpdump file (only one " + "allowed, pass '-' as the filename to read from stdin)\n"); + printf(" -s|--rulefile | read rules from given file\n"); + printf(" -t|--tracefile | activate execution tracing\n"); + printf(" -u|--usage-issues | find variable usage issues and exit\n"); + printf( + " --no-unused-warnings | suppress warnings of unused " + "functions/hooks/events\n"); + printf(" -v|--version | print version and exit\n"); + printf(" -V|--build-info | print build information and exit\n"); + printf(" -w|--writefile | write to given tcpdump file\n"); #ifdef DEBUG - fprintf(stderr, - " -B|--debug | Enable debugging output for selected " - "streams ('-B help' for help)\n"); + printf( + " -B|--debug | Enable debugging output for selected " + "streams ('-B help' for help)\n"); #endif - fprintf(stderr, " -C|--no-checksums | ignore checksums\n"); - fprintf(stderr, " -D|--deterministic | initialize random seeds to zero\n"); - fprintf(stderr, - " -E|--event-trace | generate a replayable event trace to " - "the given file\n"); - fprintf(stderr, " -F|--force-dns | force DNS\n"); - fprintf(stderr, " -G|--load-seeds | load seeds from given file\n"); - fprintf(stderr, " -H|--save-seeds | save seeds to given file\n"); - fprintf(stderr, " -I|--print-id | print out given ID\n"); - fprintf(stderr, - " -N|--print-plugins | print available plugins and exit (-NN " - "for verbose)\n"); - fprintf(stderr, - " -O|--optimize