From f8386836dfda04c626e63ba9d64c1a4f63867dfa Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 23 Mar 2019 06:05:46 +0200 Subject: cam: Separate options valid() and empty() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An empty option list is not necessarily an error. Add a new empty() function to test the option list for emptiness, and modify the valid() function to only notify parsing errors. As a side effect this allows accessing partially parsed options, which may be useful in the future. Signed-off-by: Laurent Pinchart Acked-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- src/cam/options.cpp | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src/cam/options.cpp') diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 92b6b8ef..172d40f7 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -40,10 +40,16 @@ const char *Option::typeName() const * OptionBase */ +template +bool OptionsBase::empty() const +{ + return values_.empty(); +} + template bool OptionsBase::valid() const { - return !values_.empty(); + return valid_; } template @@ -105,12 +111,6 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, return true; } -template -void OptionsBase::clear() -{ - values_.clear(); -} - template class OptionsBase; template class OptionsBase; @@ -170,21 +170,18 @@ KeyValueParser::Options KeyValueParser::parse(const char *arguments) if (optionsMap_.find(key) == optionsMap_.end()) { std::cerr << "Invalid option " << key << std::endl; - options.clear(); - break; + return options; } OptionArgument arg = optionsMap_[key].argument; if (value.empty() && arg == ArgumentRequired) { std::cerr << "Option " << key << " requires an argument" << std::endl; - options.clear(); - break; + return options; } else if (!value.empty() && arg == ArgumentNone) { std::cerr << "Option " << key << " takes no argument" << std::endl; - options.clear(); - break; + return options; } const Option &option = optionsMap_[key]; @@ -192,11 +189,11 @@ KeyValueParser::Options KeyValueParser::parse(const char *arguments) std::cerr << "Failed to parse '" << value << "' as " << option.typeName() << " for option " << key << std::endl; - options.clear(); - break; + return options; } } + options.valid_ = true; return options; } @@ -438,19 +435,18 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv) std::cerr << argv[optind - 1] << std::endl; usage(); - options.clear(); - break; + return options; } const Option &option = *optionsMap_[c]; if (!options.parseValue(c, option, optarg)) { parseValueError(option); usage(); - options.clear(); - break; + return options; } } + options.valid_ = true; return options; } -- cgit v1.2.1