From patchwork Wed Mar 27 00:21:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 819 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 493E6600F9 for ; Wed, 27 Mar 2019 01:21:23 +0100 (CET) X-Halon-ID: 389555c3-5026-11e9-8144-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id 389555c3-5026-11e9-8144-0050569116f7; Wed, 27 Mar 2019 01:21:10 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Wed, 27 Mar 2019 01:21:07 +0100 Message-Id: <20190327002107.24487-4-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190327002107.24487-1-niklas.soderlund@ragnatech.se> References: <20190327002107.24487-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/3] cam: options: Add support for repeatable options 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: Wed, 27 Mar 2019 00:21:24 -0000 Add a flag to indicate if an option can be repeatable. If an option is repeatable it must be accessed thru the array interface, even if it's only specified once by the user. Also update the usage generator to indicate which options are repeatable. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/cam/options.cpp | 21 +++++++++++++++------ src/cam/options.h | 5 +++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 556e7623c54b0658..92b6b8ef2d7d9671 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -97,7 +97,11 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, break; } - values_[opt] = value; + if (option.isArray) + values_[opt].addValue(value); + else + values_[opt] = value; + return true; } @@ -129,7 +133,7 @@ bool KeyValueParser::addOption(const char *name, OptionType type, return false; optionsMap_[name] = Option({ 0, type, name, argument, nullptr, - help, nullptr }); + help, nullptr, false }); return true; } @@ -339,7 +343,7 @@ std::vector OptionValue::toArray() const bool OptionsParser::addOption(int opt, OptionType type, const char *help, const char *name, OptionArgument argument, - const char *argumentName) + const char *argumentName, bool array) { /* * Options must have at least a short or long name, and a text message. @@ -357,16 +361,16 @@ bool OptionsParser::addOption(int opt, OptionType type, const char *help, return false; options_.push_back(Option({ opt, type, name, argument, argumentName, - help, nullptr })); + help, nullptr, array })); optionsMap_[opt] = &options_.back(); return true; } bool OptionsParser::addOption(int opt, KeyValueParser *parser, const char *help, - const char *name) + const char *name, bool array) { if (!addOption(opt, OptionKeyValue, help, name, ArgumentRequired, - "key=value[,key=value,...]")) + "key=value[,key=value,...]", array)) return false; options_.back().keyValueParser = parser; @@ -464,6 +468,8 @@ void OptionsParser::usage() length += 1 + strlen(option.argumentName); if (option.argument == ArgumentOptional) length += 2; + if (option.isArray) + length += 4; if (length > indent) indent = length; @@ -497,6 +503,9 @@ void OptionsParser::usage() argument += "]"; } + if (option.isArray) + argument += " ..."; + std::cerr << std::setw(indent) << std::left << argument; for (const char *help = option.help, *end = help; end; ) { diff --git a/src/cam/options.h b/src/cam/options.h index e0ff50af2fa74986..6e3ef62e869974b9 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -36,6 +36,7 @@ struct Option { const char *argumentName; const char *help; KeyValueParser *keyValueParser; + bool isArray; bool hasShortOption() const { return isalnum(opt); } bool hasLongOption() const { return name != nullptr; } @@ -126,9 +127,9 @@ public: bool addOption(int opt, OptionType type, const char *help, const char *name = nullptr, OptionArgument argument = ArgumentNone, - const char *argumentName = nullptr); + const char *argumentName = nullptr, bool array = false); bool addOption(int opt, KeyValueParser *parser, const char *help, - const char *name = nullptr); + const char *name = nullptr, bool array = false); Options parse(int argc, char *argv[]); void usage();