aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/CommandLineTest.cpp
diff options
context:
space:
mode:
authorIgor Kudrin <ikudrin@accesssoftek.com>2023-12-14 08:29:29 +0700
committerGitHub <noreply@github.com>2023-12-14 08:29:29 +0700
commitde8ee03ed916feb37b602b93c07f0dade8fd0050 (patch)
treed462446acbfc79899c3542bf28f5ee78b17203bd /llvm/unittests/Support/CommandLineTest.cpp
parent67d7903262ce5c35bb23d599040dff29b9d7759e (diff)
downloadllvm-de8ee03ed916feb37b602b93c07f0dade8fd0050.zip
llvm-de8ee03ed916feb37b602b93c07f0dade8fd0050.tar.gz
llvm-de8ee03ed916feb37b602b93c07f0dade8fd0050.tar.bz2
[CommandLine] Better report unknown subcommands (#74811)
The patch improves the reporting for the first option in the command line when it looks like a subcommand name but does not match any defined. Before the patch: ``` > prog baz prog: Unknown command line argument 'baz'. Try: 'prog --help' ``` With the patch: ``` > prog baz prog: Unknown subcommand 'baz'. Try: 'prog --help' prog: Did you mean 'bar'? ```
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 762ac0e..ae80490 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -2244,4 +2244,34 @@ TEST(CommandLineTest, HelpWithSubcommands) {
cl::ResetCommandLineParser();
}
+TEST(CommandLineTest, UnknownCommands) {
+ cl::ResetCommandLineParser();
+
+ StackSubCommand SC1("foo", "Foo subcommand");
+ StackSubCommand SC2("bar", "Bar subcommand");
+ StackOption<bool> SC1Opt("put", cl::sub(SC1));
+ StackOption<bool> SC2Opt("get", cl::sub(SC2));
+ StackOption<bool> TopOpt1("peek");
+ StackOption<bool> TopOpt2("set");
+
+ std::string Errs;
+ raw_string_ostream OS(Errs);
+
+ const char *Args1[] = {"prog", "baz", "--get"};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(std::size(Args1), Args1, StringRef(), &OS));
+ EXPECT_EQ(Errs,
+ "prog: Unknown subcommand 'baz'. Try: 'prog --help'\n"
+ "prog: Did you mean 'bar'?\n"
+ "prog: Unknown command line argument '--get'. Try: 'prog --help'\n"
+ "prog: Did you mean '--set'?\n");
+
+ // Do not show a suggestion if the subcommand is not similar to any known.
+ Errs.clear();
+ const char *Args2[] = {"prog", "faz"};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(std::size(Args2), Args2, StringRef(), &OS));
+ EXPECT_EQ(Errs, "prog: Unknown subcommand 'faz'. Try: 'prog --help'\n");
+}
+
} // anonymous namespace