Skip to content

Commit 542bb9b

Browse files
committed
tree-wide: unify some code that looks for --help in the command line
1 parent 9959d78 commit 542bb9b

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

src/backlight/backlight.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
#include "mkdir.h"
1515
#include "parse-util.h"
1616
#include "pretty-print.h"
17-
#include "terminal-util.h"
17+
#include "process-util.h"
1818
#include "reboot-util.h"
1919
#include "string-util.h"
2020
#include "strv.h"
21+
#include "terminal-util.h"
2122
#include "util.h"
2223

2324
static int help(void) {
@@ -368,7 +369,7 @@ static int run(int argc, char *argv[]) {
368369

369370
log_setup();
370371

371-
if (strv_contains(strv_skip(argv, 1), "--help"))
372+
if (argv_looks_like_help(argc, argv))
372373
return help();
373374

374375
if (argc != 3)

src/basic/process-util.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,30 @@ _noreturn_ void freeze(void) {
16151615
pause();
16161616
}
16171617

1618+
bool argv_looks_like_help(int argc, char **argv) {
1619+
char **l;
1620+
1621+
/* Scans the command line for indications the user asks for help. This is supposed to be called by
1622+
* tools that do not implement getopt() style command line parsing because they are not primarily
1623+
* user-facing. Detects four ways of asking for help:
1624+
*
1625+
* 1. Passing zero arguments
1626+
* 2. Passing "help" as first argument
1627+
* 3. Passing --help as any argument
1628+
* 4. Passing -h as any argument
1629+
*/
1630+
1631+
if (argc <= 1)
1632+
return true;
1633+
1634+
if (streq_ptr(argv[1], "help"))
1635+
return true;
1636+
1637+
l = strv_skip(argv, 1);
1638+
1639+
return strv_contains(l, "--help") ||
1640+
strv_contains(l, "-h");
1641+
}
16181642

16191643
static const char *const sigchld_code_table[] = {
16201644
[CLD_EXITED] = "exited",

src/basic/process-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,5 @@ int setpriority_closest(int priority);
191191
bool invoked_as(char *argv[], const char *token);
192192

193193
_noreturn_ void freeze(void);
194+
195+
bool argv_looks_like_help(int argc, char **argv);

src/cryptsetup/cryptsetup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "path-util.h"
3535
#include "pkcs11-util.h"
3636
#include "pretty-print.h"
37+
#include "process-util.h"
3738
#include "random-util.h"
3839
#include "string-util.h"
3940
#include "strv.h"
@@ -1719,7 +1720,7 @@ static int run(int argc, char *argv[]) {
17191720
const char *verb;
17201721
int r;
17211722

1722-
if (argc <= 1)
1723+
if (argv_looks_like_help(argc, argv))
17231724
return help();
17241725

17251726
if (argc < 3)

src/integritysetup/integritysetup.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#include "log.h"
1313
#include "main-func.h"
1414
#include "memory-util.h"
15-
#include "path-util.h"
1615
#include "parse-util.h"
16+
#include "path-util.h"
1717
#include "pretty-print.h"
18+
#include "process-util.h"
1819
#include "string-util.h"
1920
#include "terminal-util.h"
2021

@@ -90,10 +91,7 @@ static int run(int argc, char *argv[]) {
9091
int r;
9192
char *action, *volume;
9293

93-
if (argc <= 1 ||
94-
strv_contains(strv_skip(argv, 1), "--help") ||
95-
strv_contains(strv_skip(argv, 1), "-h") ||
96-
streq(argv[1], "help"))
94+
if (argv_looks_like_help(argc, argv))
9795
return help();
9896

9997
if (argc < 3)

src/veritysetup/veritysetup.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "main-func.h"
1313
#include "path-util.h"
1414
#include "pretty-print.h"
15+
#include "process-util.h"
1516
#include "string-util.h"
1617
#include "terminal-util.h"
1718

@@ -114,10 +115,7 @@ static int run(int argc, char *argv[]) {
114115
const char *verb;
115116
int r;
116117

117-
if (argc <= 1 ||
118-
strv_contains(strv_skip(argv, 1), "--help") ||
119-
strv_contains(strv_skip(argv, 1), "-h") ||
120-
streq(argv[1], "help"))
118+
if (argv_looks_like_help(argc, argv))
121119
return help();
122120

123121
if (argc < 3)

0 commit comments

Comments
 (0)