From 270eb0acc79346ac0b641f405c40bf3b959a4c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Mon, 28 Jan 2019 01:02:18 +0100 Subject: cam: options: Add a key=value parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some options passed to the cam utility need to be complex and specify a list of key=value pairs. Add a new parser to deal with these options, usable on its own to parse key=value pairs from any string. Integrate the KeyValueParser into the existing OptionsParser. The cam application can fully describe all its options in one location and perform full parsing of all arguments in one go. The KeyValueParser also integrates itself with the usage() printing of the OptionsParser. Signed-off-by: Niklas Söderlund Signed-off-by: Laurent Pinchart --- src/cam/options.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/cam/options.h') diff --git a/src/cam/options.h b/src/cam/options.h index 8b611d37..e1fd62ec 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -11,6 +11,9 @@ #include #include +class KeyValueParser; +class OptionValue; + enum OptionArgument { ArgumentNone, ArgumentRequired, @@ -21,6 +24,7 @@ enum OptionType { OptionNone, OptionInteger, OptionString, + OptionKeyValue, }; struct Option { @@ -30,14 +34,13 @@ struct Option { OptionArgument argument; const char *argumentName; const char *help; + KeyValueParser *keyValueParser; bool hasShortOption() const { return isalnum(opt); } bool hasLongOption() const { return name != nullptr; } const char *typeName() const; }; -class OptionValue; - template class OptionsBase { @@ -47,6 +50,7 @@ public: const OptionValue &operator[](const T &opt) const; private: + friend class KeyValueParser; friend class OptionsParser; bool parseValue(const T &opt, const Option &option, const char *value); @@ -55,6 +59,23 @@ private: std::map values_; }; +class KeyValueParser +{ +public: + class Options : public OptionsBase + { + }; + + bool addOption(const char *name, OptionType type, const char *help, + OptionArgument argument = ArgumentNone); + + Options parse(const char *arguments); + void usage(int indent); + +private: + std::map optionsMap_; +}; + class OptionValue { public: @@ -62,16 +83,19 @@ public: OptionValue(int value); OptionValue(const char *value); OptionValue(const std::string &value); + OptionValue(const KeyValueParser::Options &value); OptionType type() const { return type_; } operator int() const; operator std::string() const; + operator KeyValueParser::Options() const; private: OptionType type_; int integer_; std::string string_; + KeyValueParser::Options keyValues_; }; class OptionsParser @@ -85,6 +109,8 @@ public: const char *name = nullptr, OptionArgument argument = ArgumentNone, const char *argumentName = nullptr); + bool addOption(int opt, KeyValueParser *parser, const char *help, + const char *name = nullptr); Options parse(int argc, char *argv[]); void usage(); -- cgit v1.2.1