Skip to content

eBPF-based security platform

options: answer --list-cmds for a command without subcommands

Daniel Borkmann65c030fba656
diff --git a/Documentation/reference/cli/shirudo.rst b/Documentation/reference/cli/shirudo.rst
index 6bd106a6..6cdbda67 100644
--- a/Documentation/reference/cli/shirudo.rst
+++ b/Documentation/reference/cli/shirudo.rst
@@ -134,9 +134,10 @@ option, an option given twice where it does not repeat, a value missing
or empty, and an argument the command does not take are usage errors:
the command prints one line, ``<command>: <what is wrong>``, with the
closest option names after an unknown one, and exits with 2. ``--``
-ends the options, ``-h`` and ``--help`` print the command's help, and
+ends the options, ``-h`` and ``--help`` print the command's help,
``--list-opts`` prints its options one a line, a value's placeholder
-after a tab, which the bash completion reads. ``upatch map
+after a tab, and ``--list-cmds`` its subcommands, none where it has
+none. The bash completion reads both. ``upatch map
--json=<path>`` takes its path after the equals sign only, as ``--json``
alone prints the JSON. ``image build`` hands its arguments to
``shirudo-gen-verity``, which takes each value as the next argument
diff --git a/testing/proofs/cbmc/options-next.c b/testing/proofs/cbmc/options-next.c
index ce9d1552..2f1f180c 100644
--- a/testing/proofs/cbmc/options-next.c
+++ b/testing/proofs/cbmc/options-next.c
@@ -58,6 +58,12 @@ void print_help(const struct help *h)
{
}
+/* As util/help.c has it. */
+int is_list_cmds(const char *arg)
+{
+ return arg && strcmp(arg, "--list-cmds") == 0;
+}
+
void help_suggest(const char *bad, const char *const *names, size_t n_names)
{
__CPROVER_assert(n_names <= OPT_MAX, "the names fit");
diff --git a/testing/unit/options.c b/testing/unit/options.c
index c9e2ab73..9ed9ad73 100644
--- a/testing/unit/options.c
+++ b/testing/unit/options.c
@@ -150,6 +150,7 @@ static void help_and_the_option_list_end_parsing(void)
{
static const char *const help[] = { "--cert", "a", "-h", "--bogus" };
static const char *const list[] = { "--list-opts" };
+ static const char *const cmds[] = { "--list-cmds" };
struct opt_parser p;
FILE *saved = stdout;
@@ -160,6 +161,8 @@ static void help_and_the_option_list_end_parsing(void)
ASSERT_TRUE(stdout);
parse(&p, list, 1);
EXPECT_EQ(opt_next(&p), OPT_DONE);
+ parse(&p, cmds, 1);
+ EXPECT_EQ(opt_next(&p), OPT_DONE);
EXPECT_EQ(fclose(stdout), 0);
stdout = saved;
}
diff --git a/util/options.c b/util/options.c
index 23dfec46..eb117eb6 100644
--- a/util/options.c
+++ b/util/options.c
@@ -195,6 +195,9 @@ int opt_next(struct opt_parser *p)
}
if (!strcmp(arg, "--list-opts"))
return opt_list(p);
+ /* A command whose options this parser reads has no subcommands. */
+ if (is_list_cmds(arg))
+ return OPT_DONE;
opt = spec_find(p, arg, &eq);
if (opt < 0)
return opt_unknown(p, arg);
diff --git a/util/options.h b/util/options.h
index feae7f3d..8a5026fb 100644
--- a/util/options.h
+++ b/util/options.h
@@ -81,7 +81,8 @@ void opt_init(struct opt_parser *p, const char *command,
* for an OPT_OPTIONAL one given without; otherwise one of the OPT_ codes
* above. An unknown option, a value missing, empty or given to a flag,
* and an option given twice are usage errors. --list-opts prints the
- * options, one a line, a value's metavar after a tab, for completion.
+ * options, one a line, a value's metavar after a tab, for completion,
+ * and --list-cmds the subcommands, which are none.
*/
int opt_next(struct opt_parser *p);