mirror of
https://github.com/systemd/systemd
synced 2024-07-22 10:44:58 +00:00
tree-wide: unify some code that looks for --help in the command line
This commit is contained in:
parent
9959d78280
commit
542bb9be7c
|
@ -14,10 +14,11 @@
|
||||||
#include "mkdir.h"
|
#include "mkdir.h"
|
||||||
#include "parse-util.h"
|
#include "parse-util.h"
|
||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
#include "terminal-util.h"
|
#include "process-util.h"
|
||||||
#include "reboot-util.h"
|
#include "reboot-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
|
#include "terminal-util.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int help(void) {
|
static int help(void) {
|
||||||
|
@ -368,7 +369,7 @@ static int run(int argc, char *argv[]) {
|
||||||
|
|
||||||
log_setup();
|
log_setup();
|
||||||
|
|
||||||
if (strv_contains(strv_skip(argv, 1), "--help"))
|
if (argv_looks_like_help(argc, argv))
|
||||||
return help();
|
return help();
|
||||||
|
|
||||||
if (argc != 3)
|
if (argc != 3)
|
||||||
|
|
|
@ -1615,6 +1615,30 @@ _noreturn_ void freeze(void) {
|
||||||
pause();
|
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[] = {
|
static const char *const sigchld_code_table[] = {
|
||||||
[CLD_EXITED] = "exited",
|
[CLD_EXITED] = "exited",
|
||||||
|
|
|
@ -191,3 +191,5 @@ int setpriority_closest(int priority);
|
||||||
bool invoked_as(char *argv[], const char *token);
|
bool invoked_as(char *argv[], const char *token);
|
||||||
|
|
||||||
_noreturn_ void freeze(void);
|
_noreturn_ void freeze(void);
|
||||||
|
|
||||||
|
bool argv_looks_like_help(int argc, char **argv);
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include "path-util.h"
|
#include "path-util.h"
|
||||||
#include "pkcs11-util.h"
|
#include "pkcs11-util.h"
|
||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
|
#include "process-util.h"
|
||||||
#include "random-util.h"
|
#include "random-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
|
@ -1719,7 +1720,7 @@ static int run(int argc, char *argv[]) {
|
||||||
const char *verb;
|
const char *verb;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (argc <= 1)
|
if (argv_looks_like_help(argc, argv))
|
||||||
return help();
|
return help();
|
||||||
|
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
|
|
|
@ -12,9 +12,10 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "main-func.h"
|
#include "main-func.h"
|
||||||
#include "memory-util.h"
|
#include "memory-util.h"
|
||||||
#include "path-util.h"
|
|
||||||
#include "parse-util.h"
|
#include "parse-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
|
#include "process-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "terminal-util.h"
|
#include "terminal-util.h"
|
||||||
|
|
||||||
|
@ -90,10 +91,7 @@ static int run(int argc, char *argv[]) {
|
||||||
int r;
|
int r;
|
||||||
char *action, *volume;
|
char *action, *volume;
|
||||||
|
|
||||||
if (argc <= 1 ||
|
if (argv_looks_like_help(argc, argv))
|
||||||
strv_contains(strv_skip(argv, 1), "--help") ||
|
|
||||||
strv_contains(strv_skip(argv, 1), "-h") ||
|
|
||||||
streq(argv[1], "help"))
|
|
||||||
return help();
|
return help();
|
||||||
|
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "main-func.h"
|
#include "main-func.h"
|
||||||
#include "path-util.h"
|
#include "path-util.h"
|
||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
|
#include "process-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "terminal-util.h"
|
#include "terminal-util.h"
|
||||||
|
|
||||||
|
@ -114,10 +115,7 @@ static int run(int argc, char *argv[]) {
|
||||||
const char *verb;
|
const char *verb;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (argc <= 1 ||
|
if (argv_looks_like_help(argc, argv))
|
||||||
strv_contains(strv_skip(argv, 1), "--help") ||
|
|
||||||
strv_contains(strv_skip(argv, 1), "-h") ||
|
|
||||||
streq(argv[1], "help"))
|
|
||||||
return help();
|
return help();
|
||||||
|
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
|
|
Loading…
Reference in a new issue