diff options
author | Mikhail Glushenkov <foldr@codedgers.com> | 2009-01-16 22:54:19 +0000 |
---|---|---|
committer | Mikhail Glushenkov <foldr@codedgers.com> | 2009-01-16 22:54:19 +0000 |
commit | 7059d47a6e1a378232dce3e47b51434dec0ea608 (patch) | |
tree | 85bc2cbde9cdf86e45d9b58c9921c5049e774514 /lib/Support/CommandLine.cpp | |
parent | 01bbc3e3346e58be4924363b3127ea4254919dbb (diff) | |
download | external_llvm-7059d47a6e1a378232dce3e47b51434dec0ea608.zip external_llvm-7059d47a6e1a378232dce3e47b51434dec0ea608.tar.gz external_llvm-7059d47a6e1a378232dce3e47b51434dec0ea608.tar.bz2 |
Support for multi-valued options in CommandLine
Makes possible to specify options that take multiple arguments (a-la
-sectalign on Darwin). See documentation for details.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@62372 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/CommandLine.cpp')
-rw-r--r-- | lib/Support/CommandLine.cpp | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 20c2b8e..87dfc5a 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -172,6 +172,9 @@ static Option *LookupOption(const char *&Arg, const char *&Value, static inline bool ProvideOption(Option *Handler, const char *ArgName, const char *Value, int argc, char **argv, int &i) { + // Is this a multi-argument option? + unsigned NumAdditionalVals = Handler->getNumAdditionalVals(); + // Enforce value requirements switch (Handler->getValueExpectedFlag()) { case ValueRequired: @@ -184,6 +187,10 @@ static inline bool ProvideOption(Option *Handler, const char *ArgName, } break; case ValueDisallowed: + if (NumAdditionalVals > 0) + return Handler->error(": multi-valued option specified" + " with ValueDisallowed modifier!"); + if (Value) return Handler->error(" does not allow a value! '" + std::string(Value) + "' specified."); @@ -198,8 +205,35 @@ static inline bool ProvideOption(Option *Handler, const char *ArgName, break; } - // Run the handler now! - return Handler->addOccurrence(i, ArgName, Value ? Value : ""); + // If this isn't a multi-arg option, just run the handler. + if (NumAdditionalVals == 0) { + return Handler->addOccurrence(i, ArgName, Value ? Value : ""); + } + // If it is, run the handle several times. + else { + bool MultiArg = false; + + if (Value) { + if (Handler->addOccurrence(i, ArgName, Value, MultiArg)) + return true; + --NumAdditionalVals; + MultiArg = true; + } + + while (NumAdditionalVals > 0) { + + if (i+1 < argc) { + Value = argv[++i]; + } else { + return Handler->error(": not enough values!"); + } + if (Handler->addOccurrence(i, ArgName, Value, MultiArg)) + return true; + MultiArg = true; + --NumAdditionalVals; + } + return false; + } } static bool ProvidePositionalOption(Option *Handler, const std::string &Arg, @@ -738,8 +772,10 @@ bool Option::error(std::string Message, const char *ArgName) { } bool Option::addOccurrence(unsigned pos, const char *ArgName, - const std::string &Value) { - NumOccurrences++; // Increment the number of times we have been seen + const std::string &Value, + bool MultiArg) { + if (!MultiArg) + NumOccurrences++; // Increment the number of times we have been seen switch (getNumOccurrencesFlag()) { case Optional: |