diff options
author | Don Hinton <hintonda@gmail.com> | 2019-05-11 20:27:01 +0000 |
---|---|---|
committer | Don Hinton <hintonda@gmail.com> | 2019-05-11 20:27:01 +0000 |
commit | 0303e8a3fd88e0da8732144f34033123d3ed83b4 (patch) | |
tree | 6d9afa53229393aca2c5f78b6e229095b089cbe4 /llvm/unittests/Support/CommandLineTest.cpp | |
parent | 73e8b6743820ff83bb07f565f7f22a575e1e38fe (diff) | |
download | llvm-0303e8a3fd88e0da8732144f34033123d3ed83b4.zip llvm-0303e8a3fd88e0da8732144f34033123d3ed83b4.tar.gz llvm-0303e8a3fd88e0da8732144f34033123d3ed83b4.tar.bz2 |
[CommandLine] Add long option flag for cl::ParseCommandLineOptions . Part 5 of 5
Summary:
If passed, the long option flag makes the CommandLine parser
mimic the behavior or GNU getopt_long. Short options are a single
character prefixed by a single dash, and long options are multiple
characters prefixed by a double dash.
This patch was motivated by the discussion in the following thread:
http://lists.llvm.org/pipermail/llvm-dev/2019-April/131786.html
Reviewed By: MaskRay
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61294
llvm-svn: 360532
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r-- | llvm/unittests/Support/CommandLineTest.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp index 88696743..2bf07e3 100644 --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -1527,4 +1527,77 @@ TEST(CommandLineTest, GroupingAndPrefix) { cl::ResetAllOptionOccurrences(); } +TEST(CommandLineTest, LongOptions) { + cl::ResetCommandLineParser(); + + StackOption<bool> OptA("a", cl::desc("Some flag")); + StackOption<bool> OptBLong("long-flag", cl::desc("Some long flag")); + StackOption<bool, cl::alias> OptB("b", cl::desc("Alias to --long-flag"), + cl::aliasopt(OptBLong)); + StackOption<std::string> OptAB("ab", cl::desc("Another long option")); + + std::string Errs; + raw_string_ostream OS(Errs); + + const char *args1[] = {"prog", "-a", "-ab", "val1"}; + const char *args2[] = {"prog", "-a", "--ab", "val1"}; + const char *args3[] = {"prog", "-ab", "--ab", "val1"}; + + // + // The following tests treat `-` and `--` the same, and always match the + // longest string. + // + + EXPECT_TRUE( + cl::ParseCommandLineOptions(4, args1, StringRef(), &OS)); OS.flush(); + EXPECT_TRUE(OptA); + EXPECT_FALSE(OptBLong); + EXPECT_STREQ("val1", OptAB.c_str()); + EXPECT_TRUE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); + + EXPECT_TRUE( + cl::ParseCommandLineOptions(4, args2, StringRef(), &OS)); OS.flush(); + EXPECT_TRUE(OptA); + EXPECT_FALSE(OptBLong); + EXPECT_STREQ("val1", OptAB.c_str()); + EXPECT_TRUE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); + + // Fails because `-ab` and `--ab` are treated the same and appear more than + // once. Also, `val1` is unexpected. + EXPECT_FALSE( + cl::ParseCommandLineOptions(4, args3, StringRef(), &OS)); OS.flush(); + outs()<< Errs << "\n"; + EXPECT_FALSE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); + + // + // The following tests treat `-` and `--` differently, with `-` for short, and + // `--` for long options. + // + + // Fails because `-ab` is treated as `-a -b`, so `-a` is seen twice, and + // `val1` is unexpected. + EXPECT_FALSE(cl::ParseCommandLineOptions(4, args1, StringRef(), + &OS, nullptr, true)); OS.flush(); + EXPECT_FALSE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); + + // Works because `-a` is treated differently than `--ab`. + EXPECT_TRUE(cl::ParseCommandLineOptions(4, args2, StringRef(), + &OS, nullptr, true)); OS.flush(); + EXPECT_TRUE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); + + // Works because `-ab` is treated as `-a -b`, and `--ab` is a long option. + EXPECT_TRUE(cl::ParseCommandLineOptions(4, args3, StringRef(), + &OS, nullptr, true)); + EXPECT_TRUE(OptA); + EXPECT_TRUE(OptBLong); + EXPECT_STREQ("val1", OptAB.c_str()); + OS.flush(); + EXPECT_TRUE(Errs.empty()); Errs.clear(); + cl::ResetAllOptionOccurrences(); +} } // anonymous namespace |