[llvm] [Support]Look up in top-level subcommand as a fallback when looking options for a custom subcommand. (PR #71776)
Snehasish Kumar via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 9 09:48:30 PST 2023
================
@@ -525,6 +525,74 @@ 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.
+ StackOption<std::string> PositionalOpt(
+ cl::Positional, cl::desc("positional argument test coverage"),
+ cl::sub(SC));
+ // Thel 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));
+
+ std::string Errs;
+ raw_string_ostream OS(Errs);
+
+ for (const char *literalArg : {"--bar", "--foo", "--baz"}) {
----------------
snehasish wrote:
If you structure this loop to iterate over a list of {input, want} pairs then you can avoid the switch case below and the conversion for checking.
```
for (auto& test : {std::pair{"--bar", bar}, {"--foo", foo}, {"--baz", baz}) {
...
EXPECT_EQ(LiteralOpt, test.second);
}
```
https://github.com/llvm/llvm-project/pull/71776
More information about the llvm-commits
mailing list