From 29e75d51d447c5c0aae9834f5988b79a945c7acc Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 1 Oct 2016 15:52:00 -0500 Subject: Move --version handling next to --help handling, so it applies to all commands. Say "toybox" before version string. Tweak sed to preserve lie-to-autoconf. --- Config.in | 5 +++-- main.c | 37 ++++++++++++++++++++----------------- toys/posix/sed.c | 5 ++++- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Config.in b/Config.in index 76b2ef1d..07c6d70c 100644 --- a/Config.in +++ b/Config.in @@ -82,12 +82,13 @@ config TOYBOX_HELP Include help text for each command. config TOYBOX_HELP_DASHDASH - bool "--help" + bool "--help and --version" default y depends on TOYBOX_HELP help Support --help argument in all commands, even ones with a NULL - optstring. Produces the same output as "help command". + optstring. (Use TOYFLAG_NOHELP to disable.) Produces the same output + as "help command". --version shows toybox version. config TOYBOX_I18N bool "Internationalization support" diff --git a/main.c b/main.c index c0c1b824..5a2a272d 100644 --- a/main.c +++ b/main.c @@ -67,6 +67,12 @@ static const int NEED_OPTIONS = #include "generated/newtoys.h" 0; // Ends the opts || opts || opts... +static void unknown(char *name) +{ + toys.exitval = 127; + error_exit("Unknown command %s", name); +} + // Setup toybox global state for this command. static void toy_singleinit(struct toy_list *which, char *argv[]) { @@ -75,13 +81,19 @@ static void toy_singleinit(struct toy_list *which, char *argv[]) if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE)); - if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) - && argv[1] && !strcmp(argv[1], "--help")) - { - if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) - if (!(toys.which = toy_find(toys.argv[2]))) return; - show_help(stdout); - xexit(); + // Parse --help and --version for (almost) all commands + if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) { + if (!strcmp(argv[1], "--help")) { + if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) + if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]); + show_help(stdout); + xexit(); + } + + if (!strcmp(argv[1], "--version")) { + xputs("toybox "TOYBOX_VERSION); + xexit(); + } } if (NEED_OPTIONS && which->options) get_optflags(); @@ -168,16 +180,7 @@ void toybox_main(void) // For early error reporting toys.which = toy_list; - if (toys.argv[1]) { - if (!strcmp("--version", toys.argv[1])) { - xputs(TOYBOX_VERSION); - xexit(); - } - if (toys.argv[1][0] != '-') { - toys.exitval = 127; - error_exit("Unknown command %s", toys.argv[1]); - } - } + if (toys.argv[1] && toys.argv[1][0] != '-') unknown(toys.argv[1]); // Output list of command. for (i=1; i