Skip to content

Commit

Permalink
[CommandLine] Add subcommand groups (#75678)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igorkudrin committed Dec 19, 2023
1 parent c77c366 commit 1e91f32
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 20 additions & 3 deletions llvm/include/llvm/Support/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ extern ManagedStatic<SubCommand> TopLevelSubCommand;
// A special subcommand that can be used to put an option into all subcommands.
extern ManagedStatic<SubCommand> AllSubCommands;

class SubCommandGroup {
SmallVector<SubCommand *, 4> Subs;

public:
SubCommandGroup(std::initializer_list<SubCommand *> IL) : Subs(IL) {}

ArrayRef<SubCommand *> getSubCommands() const { return Subs; }
};

//===----------------------------------------------------------------------===//
//
class Option {
Expand Down Expand Up @@ -473,11 +482,19 @@ struct cat {

// Specify the subcommand that this option belongs to.
struct sub {
SubCommand &Sub;
SubCommand *Sub = nullptr;
SubCommandGroup *Group = nullptr;

sub(SubCommand &S) : Sub(S) {}
sub(SubCommand &S) : Sub(&S) {}
sub(SubCommandGroup &G) : Group(&G) {}

template <class Opt> void apply(Opt &O) const { O.addSubCommand(Sub); }
template <class Opt> void apply(Opt &O) const {
if (Sub)
O.addSubCommand(*Sub);
else if (Group)
for (SubCommand *SC : Group->getSubCommands())
O.addSubCommand(*SC);
}
};

// Specify a callback function to be called when an option is seen.
Expand Down
27 changes: 27 additions & 0 deletions llvm/unittests/Support/CommandLineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1e91f32

Please sign in to comment.