Skip to content

[CommandLine] Add subcommand groups #75678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -477,11 +486,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