]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: unify some code that looks for --help in the command line
authorLennart Poettering <lennart@poettering.net>
Thu, 31 Mar 2022 09:09:48 +0000 (11:09 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 31 Mar 2022 09:44:46 +0000 (11:44 +0200)
src/backlight/backlight.c
src/basic/process-util.c
src/basic/process-util.h
src/cryptsetup/cryptsetup.c
src/integritysetup/integritysetup.c
src/veritysetup/veritysetup.c

index 5a3095cbbab60d8deb08c909684ba88933bf9bec..a4e5d77f6c8074c98136d506d440c6dd80d04d01 100644 (file)
 #include "mkdir.h"
 #include "parse-util.h"
 #include "pretty-print.h"
-#include "terminal-util.h"
+#include "process-util.h"
 #include "reboot-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "terminal-util.h"
 #include "util.h"
 
 static int help(void) {
@@ -368,7 +369,7 @@ static int run(int argc, char *argv[]) {
 
         log_setup();
 
-        if (strv_contains(strv_skip(argv, 1), "--help"))
+        if (argv_looks_like_help(argc, argv))
                 return help();
 
         if (argc != 3)
index 72807d039c348825dc54a2f387c01ec3ecfbec97..10aec10bfa721faa26e1fc1a363de434e32ffe75 100644 (file)
@@ -1615,6 +1615,30 @@ _noreturn_ void freeze(void) {
                 pause();
 }
 
+bool argv_looks_like_help(int argc, char **argv) {
+        char **l;
+
+        /* Scans the command line for indications the user asks for help. This is supposed to be called by
+         * tools that do not implement getopt() style command line parsing because they are not primarily
+         * user-facing. Detects four ways of asking for help:
+         *
+         * 1. Passing zero arguments
+         * 2. Passing "help" as first argument
+         * 3. Passing --help as any argument
+         * 4. Passing -h as any argument
+         */
+
+        if (argc <= 1)
+                return true;
+
+        if (streq_ptr(argv[1], "help"))
+                return true;
+
+        l = strv_skip(argv, 1);
+
+        return strv_contains(l, "--help") ||
+                strv_contains(l, "-h");
+}
 
 static const char *const sigchld_code_table[] = {
         [CLD_EXITED] = "exited",
index f22ff76ee82833f1e06b3efe954afc666fe71496..c07575e5802a4453fc4832dbe97a849a236ab55e 100644 (file)
@@ -191,3 +191,5 @@ int setpriority_closest(int priority);
 bool invoked_as(char *argv[], const char *token);
 
 _noreturn_ void freeze(void);
+
+bool argv_looks_like_help(int argc, char **argv);
index 21430d425623664046a58b0a6f1bacad34058bb5..408d7511bfd0f8e34a380ad451882f7361e37fc1 100644 (file)
@@ -34,6 +34,7 @@
 #include "path-util.h"
 #include "pkcs11-util.h"
 #include "pretty-print.h"
+#include "process-util.h"
 #include "random-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -1719,7 +1720,7 @@ static int run(int argc, char *argv[]) {
         const char *verb;
         int r;
 
-        if (argc <= 1)
+        if (argv_looks_like_help(argc, argv))
                 return help();
 
         if (argc < 3)
index d8cfd12e9522307626005578c6de40326c9c101d..7eff0ce5dc962c774e9631605d5945bae2bc702d 100644 (file)
 #include "log.h"
 #include "main-func.h"
 #include "memory-util.h"
-#include "path-util.h"
 #include "parse-util.h"
+#include "path-util.h"
 #include "pretty-print.h"
+#include "process-util.h"
 #include "string-util.h"
 #include "terminal-util.h"
 
@@ -90,10 +91,7 @@ static int run(int argc, char *argv[]) {
         int r;
         char *action, *volume;
 
-        if (argc <= 1 ||
-            strv_contains(strv_skip(argv, 1), "--help") ||
-            strv_contains(strv_skip(argv, 1), "-h") ||
-            streq(argv[1], "help"))
+        if (argv_looks_like_help(argc, argv))
                 return help();
 
         if (argc < 3)
index a81e93cb756529ac317856a4e903ed405c7231d3..1d1baaa59db2cbb80d6497d05e8ebef6eba2877a 100644 (file)
@@ -12,6 +12,7 @@
 #include "main-func.h"
 #include "path-util.h"
 #include "pretty-print.h"
+#include "process-util.h"
 #include "string-util.h"
 #include "terminal-util.h"
 
@@ -114,10 +115,7 @@ static int run(int argc, char *argv[]) {
         const char *verb;
         int r;
 
-        if (argc <= 1 ||
-            strv_contains(strv_skip(argv, 1), "--help") ||
-            strv_contains(strv_skip(argv, 1), "-h") ||
-            streq(argv[1], "help"))
+        if (argv_looks_like_help(argc, argv))
                 return help();
 
         if (argc < 3)