cgls: convert to the new option parser
arg_names is changed to be a normal strv. In the new parser code, the argument is returned as a const char*. Dropping the const to stuff it into the array would be too ugly. The metavars for optional args are now shown in --help. Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+59
-83
@@ -1,6 +1,5 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sd-bus.h"
|
||||
@@ -10,8 +9,10 @@
|
||||
#include "bus-util.h"
|
||||
#include "cgroup-show.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "format-table.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "options.h"
|
||||
#include "output-mode.h"
|
||||
#include "pager.h"
|
||||
#include "parse-util.h"
|
||||
@@ -35,155 +36,130 @@ static char **arg_names = NULL;
|
||||
static int arg_full = -1;
|
||||
static const char* arg_machine = NULL;
|
||||
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_names, freep); /* don't free the strings */
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_names, strv_freep);
|
||||
|
||||
static int help(void) {
|
||||
_cleanup_free_ char *link = NULL;
|
||||
_cleanup_(table_unrefp) Table *options = NULL;
|
||||
int r;
|
||||
|
||||
r = terminal_urlify_man("systemd-cgls", "1", &link);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
printf("%s [OPTIONS...] [CGROUP...]\n\n"
|
||||
"Recursively show control group contents.\n\n"
|
||||
" -h --help Show this help\n"
|
||||
" --version Show package version\n"
|
||||
" --no-pager Do not pipe output into a pager\n"
|
||||
" -a --all Show all groups, including empty\n"
|
||||
" -u --unit Show the subtrees of specified system units\n"
|
||||
" --user-unit Show the subtrees of specified user units\n"
|
||||
" -x --xattr=BOOL Show cgroup extended attributes\n"
|
||||
" -c --cgroup-id=BOOL Show cgroup ID\n"
|
||||
" -l --full Do not ellipsize output\n"
|
||||
" -k Include kernel threads in output\n"
|
||||
" -M --machine=NAME Show container NAME\n"
|
||||
"\nSee the %s for details.\n",
|
||||
program_invocation_short_name,
|
||||
link);
|
||||
r = option_parser_get_help_table(&options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
printf("%s [OPTIONS...] [CGROUP...]\n\n"
|
||||
"%sRecursively show control group contents.%s\n\n",
|
||||
program_invocation_short_name,
|
||||
ansi_highlight(),
|
||||
ansi_normal());
|
||||
|
||||
r = table_print_or_warn(options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
printf("\nSee the %s for details.\n", link);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
|
||||
enum {
|
||||
ARG_NO_PAGER = 0x100,
|
||||
ARG_VERSION,
|
||||
ARG_USER_UNIT,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, ARG_VERSION },
|
||||
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
||||
{ "all", no_argument, NULL, 'a' },
|
||||
{ "full", no_argument, NULL, 'l' },
|
||||
{ "machine", required_argument, NULL, 'M' },
|
||||
{ "unit", optional_argument, NULL, 'u' },
|
||||
{ "user-unit", optional_argument, NULL, ARG_USER_UNIT },
|
||||
{ "xattr", required_argument, NULL, 'x' },
|
||||
{ "cgroup-id", required_argument, NULL, 'c' },
|
||||
{}
|
||||
};
|
||||
|
||||
int c, r;
|
||||
|
||||
assert(argc >= 1);
|
||||
assert(argv);
|
||||
|
||||
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
|
||||
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
|
||||
optind = 0;
|
||||
while ((c = getopt_long(argc, argv, "-hkalM:u::xc", options, NULL)) >= 0)
|
||||
OptionParser state = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS };
|
||||
const char *arg;
|
||||
int r;
|
||||
|
||||
FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
|
||||
switch (c) {
|
||||
|
||||
case 'h':
|
||||
OPTION_COMMON_HELP:
|
||||
return help();
|
||||
|
||||
case ARG_VERSION:
|
||||
OPTION_COMMON_VERSION:
|
||||
return version();
|
||||
|
||||
case ARG_NO_PAGER:
|
||||
OPTION_COMMON_NO_PAGER:
|
||||
arg_pager_flags |= PAGER_DISABLE;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
OPTION('a', "all", NULL, "Show all groups, including empty"):
|
||||
arg_output_flags |= OUTPUT_SHOW_ALL;
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
OPTION_FULL(OPTION_OPTIONAL_ARG, 'u', "unit", "UNIT",
|
||||
"Show the subtrees of specified system units"):
|
||||
if (arg_show_unit == SHOW_UNIT_USER)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Cannot combine --unit with --user-unit.");
|
||||
|
||||
arg_show_unit = SHOW_UNIT_SYSTEM;
|
||||
if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
|
||||
if (strv_extend(&arg_names, arg) < 0) /* push arg if not empty */
|
||||
return log_oom();
|
||||
break;
|
||||
|
||||
case ARG_USER_UNIT:
|
||||
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "user-unit", "UNIT",
|
||||
"Show the subtrees of specified user units"):
|
||||
if (arg_show_unit == SHOW_UNIT_SYSTEM)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Cannot combine --user-unit with --unit.");
|
||||
|
||||
arg_show_unit = SHOW_UNIT_USER;
|
||||
if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
|
||||
if (strv_extend(&arg_names, arg) < 0) /* push arg if not empty */
|
||||
return log_oom();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
/* positional argument */
|
||||
if (strv_push(&arg_names, optarg) < 0)
|
||||
return log_oom();
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
arg_full = true;
|
||||
break;
|
||||
|
||||
case 'k':
|
||||
arg_output_flags |= OUTPUT_KERNEL_THREADS;
|
||||
break;
|
||||
|
||||
case 'M':
|
||||
arg_machine = optarg;
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
if (optarg) {
|
||||
r = parse_boolean(optarg);
|
||||
OPTION_FULL(OPTION_OPTIONAL_ARG, 'x', "xattr", "BOOL",
|
||||
"Show cgroup extended attributes"):
|
||||
if (arg) {
|
||||
r = parse_boolean(arg);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse --xattr= value: %s", optarg);
|
||||
return log_error_errno(r, "Failed to parse --xattr= value: %s", arg);
|
||||
} else
|
||||
r = true;
|
||||
|
||||
SET_FLAG(arg_output_flags, OUTPUT_CGROUP_XATTRS, r);
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
if (optarg) {
|
||||
r = parse_boolean(optarg);
|
||||
OPTION_FULL(OPTION_OPTIONAL_ARG, 'c', "cgroup-id", "BOOL",
|
||||
"Show cgroup ID"):
|
||||
if (arg) {
|
||||
r = parse_boolean(arg);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse --cgroup-id= value: %s", optarg);
|
||||
return log_error_errno(r, "Failed to parse --cgroup-id= value: %s", arg);
|
||||
} else
|
||||
r = true;
|
||||
|
||||
SET_FLAG(arg_output_flags, OUTPUT_CGROUP_ID, r);
|
||||
break;
|
||||
|
||||
case '?':
|
||||
return -EINVAL;
|
||||
OPTION('l', "full", NULL, "Do not ellipsize output"):
|
||||
arg_full = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
OPTION_SHORT('k', NULL, "Include kernel threads in output"):
|
||||
arg_output_flags |= OUTPUT_KERNEL_THREADS;
|
||||
break;
|
||||
|
||||
OPTION_COMMON_MACHINE:
|
||||
arg_machine = arg;
|
||||
break;
|
||||
|
||||
OPTION_POSITIONAL:
|
||||
if (strv_extend(&arg_names, arg) < 0) /* push arg */
|
||||
return log_oom();
|
||||
break;
|
||||
}
|
||||
|
||||
if (arg_machine && arg_show_unit != SHOW_UNIT_NONE)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Cannot combine --unit or --user-unit with --machine=.");
|
||||
|
||||
assert(option_parser_get_n_args(&state) == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user