Skip to content
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

Revert "[Support]Look up in top-level subcommand as a fallback when looking options for a custom subcommand #71975

Merged
merged 1 commit into from
Nov 10, 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
7 changes: 0 additions & 7 deletions llvm/lib/Support/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,6 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value,
LongOptionsUseDoubleDash, HaveDoubleDash);

// If Handler is not found in a specialized subcommand, look up handler
// in the top-level subcommand.
// cl::opt without cl::sub belongs to top-level subcommand.
if (!Handler && ChosenSubCommand != &SubCommand::getTopLevel())
Handler = LookupLongOption(SubCommand::getTopLevel(), ArgName, Value,
LongOptionsUseDoubleDash, HaveDoubleDash);

// Check to see if this "option" is really a prefixed or grouped argument.
if (!Handler && !(LongOptionsUseDoubleDash && HaveDoubleDash))
Handler = HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing,
Expand Down
53 changes: 0 additions & 53 deletions llvm/unittests/Support/CommandLineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,59 +525,6 @@ TEST(CommandLineTest, LookupFailsInWrongSubCommand) {
EXPECT_FALSE(Errs.empty());
}

TEST(CommandLineTest, TopLevelOptInSubcommand) {
enum LiteralOptionEnum {
foo,
bar,
baz,
};

cl::ResetCommandLineParser();

// This is a top-level option and not associated with a subcommand.
// A command line using subcommand should parse both subcommand options and
// top-level options. A valid use case is that users of llvm command line
// tools should be able to specify top-level options defined in any library.
cl::opt<std::string> TopLevelOpt("str", cl::init("txt"),
cl::desc("A top-level option."));

StackSubCommand SC("sc", "Subcommand");
StackOption<std::string> PositionalOpt(
cl::Positional, cl::desc("positional argument test coverage"),
cl::sub(SC));
StackOption<LiteralOptionEnum> LiteralOpt(
cl::desc("literal argument test coverage"), cl::sub(SC), cl::init(bar),
cl::values(clEnumVal(foo, "foo"), clEnumVal(bar, "bar"),
clEnumVal(baz, "baz")));
StackOption<bool> EnableOpt("enable", cl::sub(SC), cl::init(false));
StackOption<int> ThresholdOpt("threshold", cl::sub(SC), cl::init(1));

const char *PositionalOptVal = "input-file";
const char *args[] = {"prog", "sc", PositionalOptVal,
"-enable", "--str=csv", "--threshold=2"};

// cl::ParseCommandLineOptions returns true on success. Otherwise, it will
// print the error message to stderr and exit in this setting (`Errs` ostream
// is not set).
ASSERT_TRUE(cl::ParseCommandLineOptions(sizeof(args) / sizeof(args[0]), args,
StringRef()));
EXPECT_STREQ(PositionalOpt.getValue().c_str(), PositionalOptVal);
EXPECT_TRUE(EnableOpt);
// Tests that the value of `str` option is `csv` as specified.
EXPECT_STREQ(TopLevelOpt.getValue().c_str(), "csv");
EXPECT_EQ(ThresholdOpt, 2);

for (auto &[LiteralOptVal, WantLiteralOpt] :
{std::pair{"--bar", bar}, {"--foo", foo}, {"--baz", baz}}) {
const char *args[] = {"prog", "sc", LiteralOptVal};
ASSERT_TRUE(cl::ParseCommandLineOptions(sizeof(args) / sizeof(args[0]),
args, StringRef()));

// Tests that literal options are parsed correctly.
EXPECT_EQ(LiteralOpt, WantLiteralOpt);
}
}

TEST(CommandLineTest, AddToAllSubCommands) {
cl::ResetCommandLineParser();

Expand Down