Skip to content

Commit 1e91f32

Browse files
authored
[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.
1 parent c77c366 commit 1e91f32

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

llvm/include/llvm/Support/CommandLine.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ extern ManagedStatic<SubCommand> TopLevelSubCommand;
243243
// A special subcommand that can be used to put an option into all subcommands.
244244
extern ManagedStatic<SubCommand> AllSubCommands;
245245

246+
class SubCommandGroup {
247+
SmallVector<SubCommand *, 4> Subs;
248+
249+
public:
250+
SubCommandGroup(std::initializer_list<SubCommand *> IL) : Subs(IL) {}
251+
252+
ArrayRef<SubCommand *> getSubCommands() const { return Subs; }
253+
};
254+
246255
//===----------------------------------------------------------------------===//
247256
//
248257
class Option {
@@ -473,11 +482,19 @@ struct cat {
473482

474483
// Specify the subcommand that this option belongs to.
475484
struct sub {
476-
SubCommand &Sub;
485+
SubCommand *Sub = nullptr;
486+
SubCommandGroup *Group = nullptr;
477487

478-
sub(SubCommand &S) : Sub(S) {}
488+
sub(SubCommand &S) : Sub(&S) {}
489+
sub(SubCommandGroup &G) : Group(&G) {}
479490

480-
template <class Opt> void apply(Opt &O) const { O.addSubCommand(Sub); }
491+
template <class Opt> void apply(Opt &O) const {
492+
if (Sub)
493+
O.addSubCommand(*Sub);
494+
else if (Group)
495+
for (SubCommand *SC : Group->getSubCommands())
496+
O.addSubCommand(*SC);
497+
}
481498
};
482499

483500
// Specify a callback function to be called when an option is seen.

llvm/unittests/Support/CommandLineTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,4 +2274,31 @@ TEST(CommandLineTest, UnknownCommands) {
22742274
EXPECT_EQ(Errs, "prog: Unknown subcommand 'faz'. Try: 'prog --help'\n");
22752275
}
22762276

2277+
TEST(CommandLineTest, SubCommandGroups) {
2278+
// Check that options in subcommand groups are associated with expected
2279+
// subcommands.
2280+
2281+
cl::ResetCommandLineParser();
2282+
2283+
StackSubCommand SC1("sc1", "SC1 subcommand");
2284+
StackSubCommand SC2("sc2", "SC2 subcommand");
2285+
StackSubCommand SC3("sc3", "SC3 subcommand");
2286+
cl::SubCommandGroup Group12 = {&SC1, &SC2};
2287+
2288+
StackOption<bool> Opt12("opt12", cl::sub(Group12), cl::init(false));
2289+
StackOption<bool> Opt3("opt3", cl::sub(SC3), cl::init(false));
2290+
2291+
// The "--opt12" option is expected to be added to both subcommands in the
2292+
// group, but not to the top-level "no subcommand" pseudo-subcommand or the
2293+
// "sc3" subcommand.
2294+
EXPECT_EQ(1, SC1.OptionsMap.size());
2295+
EXPECT_TRUE(SC1.OptionsMap.contains("opt12"));
2296+
2297+
EXPECT_EQ(1, SC2.OptionsMap.size());
2298+
EXPECT_TRUE(SC2.OptionsMap.contains("opt12"));
2299+
2300+
EXPECT_FALSE(cl::SubCommand::getTopLevel().OptionsMap.contains("opt12"));
2301+
EXPECT_FALSE(SC3.OptionsMap.contains("opt12"));
2302+
}
2303+
22772304
} // anonymous namespace

0 commit comments

Comments
 (0)