1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

tree-wide: unify some code that looks for --help in the command line

This commit is contained in:
Lennart Poettering 2022-03-31 11:09:48 +02:00
parent 9959d78280
commit 542bb9be7c
6 changed files with 36 additions and 12 deletions

View File

@ -14,10 +14,11 @@
#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)

View 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",

View 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);

View 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)

View File

@ -12,9 +12,10 @@
#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)

View 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)