aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests
diff options
context:
space:
mode:
authorIgor Kudrin <ikudrin@accesssoftek.com>2023-12-20 02:45:29 +0700
committerGitHub <noreply@github.com>2023-12-20 02:45:29 +0700
commit1e91f32ef777eb2a868acac99d25cba6d54feb02 (patch)
tree0cdde9e9fa3437231900978000d1d9ca1768b26a /llvm/unittests
parentc77c3663dbea07264ba760bc17b913cebd7986ba (diff)
downloadllvm-1e91f32ef777eb2a868acac99d25cba6d54feb02.zip
llvm-1e91f32ef777eb2a868acac99d25cba6d54feb02.tar.gz
llvm-1e91f32ef777eb2a868acac99d25cba6d54feb02.tar.bz2
[CommandLine] Add subcommand groups (#75678)
The patch introduces a `SubCommandGroup` class which represents a list of subcommands. An option can be added to all these subcommands using one `cl::sub(group)` command. This simplifies the declaration of options that are shared across multiple subcommands of a tool.
Diffstat (limited to 'llvm/unittests')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index ae80490..328674a 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -2274,4 +2274,31 @@ TEST(CommandLineTest, UnknownCommands) {
EXPECT_EQ(Errs, "prog: Unknown subcommand 'faz'. Try: 'prog --help'\n");
}
+TEST(CommandLineTest, SubCommandGroups) {
+ // Check that options in subcommand groups are associated with expected
+ // subcommands.
+
+ cl::ResetCommandLineParser();
+
+ StackSubCommand SC1("sc1", "SC1 subcommand");
+ StackSubCommand SC2("sc2", "SC2 subcommand");
+ StackSubCommand SC3("sc3", "SC3 subcommand");
+ cl::SubCommandGroup Group12 = {&SC1, &SC2};
+
+ StackOption<bool> Opt12("opt12", cl::sub(Group12), cl::init(false));
+ StackOption<bool> Opt3("opt3", cl::sub(SC3), cl::init(false));
+
+ // The "--opt12" option is expected to be added to both subcommands in the
+ // group, but not to the top-level "no subcommand" pseudo-subcommand or the
+ // "sc3" subcommand.
+ EXPECT_EQ(1, SC1.OptionsMap.size());
+ EXPECT_TRUE(SC1.OptionsMap.contains("opt12"));
+
+ EXPECT_EQ(1, SC2.OptionsMap.size());
+ EXPECT_TRUE(SC2.OptionsMap.contains("opt12"));
+
+ EXPECT_FALSE(cl::SubCommand::getTopLevel().OptionsMap.contains("opt12"));
+ EXPECT_FALSE(SC3.OptionsMap.contains("opt12"));
+}
+
} // anonymous namespace