llvm.org GIT mirror llvm / f66fc53
Support: Fix option handling when using cl::Required with aliasopt Until now, attempting to create an alias of a required option would complain if the user supplied the alias, because the required option didn't have a value. Similarly, if you said the alias was required, then using the base option would complain that the alias wasn't supplied. Lastly, if you put required on both, *neither* option would work. By changning alias to overload addOccurrence and setting cl::Required on the original option, we can get this to behave in a more useful way. I've also added a test and updated a user that was getting this wrong. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212986 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Bogner 6 years ago
3 changed file(s) with 27 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
269269
270270 // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
271271 //
272 bool addOccurrence(unsigned pos, StringRef ArgName,
273 StringRef Value, bool MultiArg = false);
272 virtual bool addOccurrence(unsigned pos, StringRef ArgName,
273 StringRef Value, bool MultiArg = false);
274274
275275 // Prints option name followed by message. Always returns true.
276276 bool error(const Twine &Message, StringRef ArgName = StringRef());
16471647 bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
16481648 StringRef Arg) override {
16491649 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1650 }
1651 bool addOccurrence(unsigned pos, StringRef /*ArgName*/,
1652 StringRef Value, bool MultiArg = false) override {
1653 return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
16501654 }
16511655 // Handle printing stuff...
16521656 size_t getOptionWidth() const override;
3737 cl::desc(""));
3838
3939 cl::opt OutputFilename("output", cl::value_desc("output"),
40 cl::init("-"),
40 cl::init("-"), cl::Required,
4141 cl::desc("Output file"));
42 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), cl::Required,
42 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
4343 cl::aliasopt(OutputFilename));
4444
4545 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
211211 }
212212 }
213213
214 void testAliasRequired(int argc, const char *const *argv) {
215 StackOption Option("option", cl::Required);
216 cl::alias Alias("o", llvm::cl::aliasopt(Option));
217
218 cl::ParseCommandLineOptions(argc, argv);
219 EXPECT_EQ("x", Option);
220 EXPECT_EQ(1, Option.getNumOccurrences());
221
222 Alias.removeArgument();
223 }
224
225 TEST(CommandLineTest, AliasRequired) {
226 const char *opts1[] = { "-tool", "-option=x" };
227 const char *opts2[] = { "-tool", "-o", "x" };
228 testAliasRequired(array_lengthof(opts1), opts1);
229 testAliasRequired(array_lengthof(opts2), opts2);
230 }
231
232
214233 } // anonymous namespace