APPS: Print help also on -h and --h; print high-level help when no cmd given
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 7 Jan 2021 09:16:12 +0000 (10:16 +0100)
committerDr. David von Oheimb <dev@ddvo.net>
Mon, 11 Jan 2021 18:39:49 +0000 (19:39 +0100)
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/13799)

apps/lib/opt.c
apps/openssl.c
test/recipes/20-test_app.t

index 5ddf1c7a74d7384f686748ab8a6f51cfcefafc55..22d4138301448cb69eb00e147feb1e7b4a430238 100644 (file)
@@ -732,7 +732,8 @@ int opt_next(void)
         *arg++ = '\0';
     for (o = opts; o->name; ++o) {
         /* If not this option, move on to the next one. */
-        if (strcmp(p, o->name) != 0)
+        if (!(strcmp(p, "h") == 0 && strcmp(o->name, "help") == 0)
+                && strcmp(p, o->name) != 0)
             continue;
 
         /* If it doesn't take a value, make sure none was given. */
index 0c95a7585ecc0ca8b1f5bf0e57bf1f5ba58664db..b61ed5f81d1d48be0f824b24a7a944c3f0894a00 100644 (file)
@@ -237,6 +237,7 @@ int main(int argc, char *argv[])
     char *pname;
     const char *fname;
     ARGS arg;
+    int global_help = 0;
     int ret = 0;
 
     arg.argv = NULL;
@@ -277,18 +278,21 @@ int main(int argc, char *argv[])
     f.name = pname;
     fp = lh_FUNCTION_retrieve(prog, &f);
     if (fp == NULL) {
-        /* We assume we've been called as 'openssl cmd' */
+        /* We assume we've been called as 'openssl ...' */
+        global_help = argc > 1
+            && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0
+                || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0);
         argc--;
         argv++;
-        opt_appname(argv[0]);
+        opt_appname(argc == 1 || global_help ? "help" : argv[0]);
     } else {
         argv[0] = pname;
     }
 
     /* If there's a command, run with that, otherwise "help". */
-    ret = argc > 0
-        ? do_cmd(prog, argc, argv)
-        : do_cmd(prog, 1, help_argv);
+    ret = argc == 0 || global_help
+        ? do_cmd(prog, 1, help_argv)
+        : do_cmd(prog, argc, argv);
 
  end:
     OPENSSL_free(default_config_file);
index e7246565f2c369f3f4a485e88460c005906cc936..dfd0db25b851f1d02084bc5428ae82ea2078ab54 100644 (file)
@@ -13,7 +13,7 @@ use OpenSSL::Test;
 
 setup("test_app");
 
-plan tests => 3;
+plan tests => 5;
 
 ok(run(app(["openssl"])),
    "Run openssl app with no args");
@@ -21,5 +21,11 @@ ok(run(app(["openssl"])),
 ok(run(app(["openssl", "help"])),
    "Run openssl app with help");
 
-ok(!run(app(["openssl", "-help"])),
+ok(!run(app(["openssl", "-wrong"])),
    "Run openssl app with incorrect arg");
+
+ok(run(app(["openssl", "-help"])),
+   "Run openssl app with -help");
+
+ok(run(app(["openssl", "--help"])),
+   "Run openssl app with --help");