options: only consume "--" immediately after an option that stops parsing

The behaviour that was implemented in systemd-dissect was that
both '--exec -- cmd' and '--exec cmd' result in 'cmd' as the command,
and '--' anywhere later is as a positional argument, so nesting is
possible, e.g.:
  --exec -- cmd --opt -- another-cmd --another-opt

This is not obvious, so add some tests for this and keep it as a separate
commit.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-03-22 16:52:22 +01:00
parent 4bf73e6980
commit e13b7872e0
2 changed files with 37 additions and 2 deletions
+4 -2
View File
@@ -112,8 +112,10 @@ int option_parse(
return 0;
}
if (!state->parsing_stopped &&
argv[state->optind][0] == '-' &&
if (state->parsing_stopped)
return 0;
if (argv[state->optind][0] == '-' &&
argv[state->optind][1] != '\0')
/* Looks like we found an option parameter */
break;
+33
View File
@@ -977,6 +977,21 @@ TEST(option_macros) {
NULL);
/* OPTION_STOPS_PARSING then "--": "--" is still consumed after exec */
test_macros_parse_one(STRV_MAKE("arg0",
"--help",
"--exec",
"--",
"--version",
"-h"),
(Entry[]) {
{ "help" },
{ "exec" },
{}
},
STRV_MAKE("--version",
"-h"));
/* OPTION_STOPS_PARSING then later "--": "--" is not consumed */
test_macros_parse_one(STRV_MAKE("arg0",
"--help",
"--exec",
@@ -989,6 +1004,24 @@ TEST(option_macros) {
{}
},
STRV_MAKE("--version",
"--",
"-h"));
/* OPTION_STOPS_PARSING then "--" twice: second "--" is not consumed */
test_macros_parse_one(STRV_MAKE("arg0",
"--help",
"--exec",
"--",
"--",
"--version",
"-h"),
(Entry[]) {
{ "help" },
{ "exec" },
{}
},
STRV_MAKE("--",
"--version",
"-h"));
}