From patchwork Sat Mar 23 07:31:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 789 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6D34F600FD for ; Sat, 23 Mar 2019 08:31:43 +0100 (CET) Received: from pendragon.ideasonboard.com (30.net042126252.t-com.ne.jp [42.126.252.30]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 459B02D0 for ; Sat, 23 Mar 2019 08:31:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1553326303; bh=521FQ7hf2R2MS0Ig9U0g5GyMfngmpuazRUr/TRcyoEI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fQ0CaT/ccFcIvRxfdE0aROSpNAbSjRrc5Jv3Z2H82l2+NbSvStvZALtj1+NS0XuKm NjotLa5LbGKWtDN3g7JIhyRr9J4M1SP/4nIaz9humPJwfnS8u0nKDtXboiF0ig+vfc OzfBLxKxQK0rJnwXkY15YFng4dcOmaWrMmGXLrZ8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 23 Mar 2019 09:31:24 +0200 Message-Id: <20190323073125.25497-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190323073125.25497-1-laurent.pinchart@ideasonboard.com> References: <20190323073125.25497-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] cam: Separate options valid() and empty() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Mar 2019 07:31:43 -0000 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 Reviewed-by: Niklas Söderlund --- src/cam/main.cpp | 7 +++++-- src/cam/options.cpp | 34 +++++++++++++++------------------- src/cam/options.h | 5 ++++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index 1ca7862bf237..e7490c32f99a 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -67,9 +67,12 @@ static int parseOptions(int argc, char *argv[]) parser.addOption(OptList, OptionNone, "List all cameras", "list"); options = parser.parse(argc, argv); - if (!options.valid() || options.isSet(OptHelp)) { + if (!options.valid()) + return -EINVAL; + + if (options.empty() || options.isSet(OptHelp)) { parser.usage(); - return !options.valid() ? -EINVAL : -EINTR; + return options.empty() ? -EINVAL : -EINTR; } return 0; diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 655aa36bb9c9..f053a31d6ea1 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -39,10 +39,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 @@ -100,12 +106,6 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, return true; } -template -void OptionsBase::clear() -{ - values_.clear(); -} - template class OptionsBase; template class OptionsBase; @@ -165,21 +165,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]; @@ -187,11 +184,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; } @@ -412,19 +409,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; } diff --git a/src/cam/options.h b/src/cam/options.h index 745f4a4a3a43..0b0444c2db42 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -45,6 +45,9 @@ template class OptionsBase { public: + OptionsBase() : valid_(false) {} + + bool empty() const; bool valid() const; bool isSet(const T &opt) const; const OptionValue &operator[](const T &opt) const; @@ -54,9 +57,9 @@ private: friend class OptionsParser; bool parseValue(const T &opt, const Option &option, const char *value); - void clear(); std::map values_; + bool valid_; }; class KeyValueParser