-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[Support]Look up in top-level subcommand as a fallback when looking options for a custom subcommand. #71776
[Support]Look up in top-level subcommand as a fallback when looking options for a custom subcommand. #71776
Changes from 3 commits
444a635
707dcab
5d90e44
d76b4d9
40840d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -525,6 +525,59 @@ TEST(CommandLineTest, LookupFailsInWrongSubCommand) { | |
EXPECT_FALSE(Errs.empty()); | ||
} | ||
|
||
TEST(CommandLineTest, SubcommandOptions) { | ||
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"); | ||
// The positional argument. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code self explains. I think the comments are unneeded. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete two one-line comments. |
||
StackOption<std::string> PositionalOpt( | ||
cl::Positional, cl::desc("positional argument test coverage"), | ||
cl::sub(SC)); | ||
// The literal argument. | ||
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> BoolOpt("enable", cl::sub(SC), cl::init(false)); | ||
|
||
const char *PositionalOptVal = "input-file"; | ||
const char *args[] = {"prog", "sc", PositionalOptVal, "-enable", "--str=csv"}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add another There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds reasonable. Make a slight change by adding an option of |
||
|
||
// 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(BoolOpt); | ||
// Tests that the value of `str` option is `csv` as specified. | ||
EXPECT_STREQ(TopLevelOpt.getValue().c_str(), "csv"); | ||
|
||
for (auto &[LiteralOptVal, WantLiteralOpt] : | ||
{std::pair{"--bar", bar}, std::pair{"--foo", foo}, | ||
std::pair{"--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(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other Subcommand tests. The name
SubcommandOptions
can be changed to reflect that the test intends to check, e.g.TopLevelOptInSubcommand