[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:31 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"}) {
+ const char *args[] = {"prog", "sc", "input-file",
+ literalArg, "-enable", "--str=csv"};
+
+ // cl::ParseCommandLineOptions returns true on success. it returns false
+ // and prints errors to caller provided error stream (&OS in this setting).
+ EXPECT_TRUE(cl::ParseCommandLineOptions(6, args, StringRef(), &OS));
----------------
snehasish wrote:
nit: sizeof(args) instead of 6
https://github.com/llvm/llvm-project/pull/71776
More information about the llvm-commits
mailing list