diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-01-31 23:36:04 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-02-01 11:42:02 +0200 |
commit | c8c546fe99a343c13c7c0b8f2e5e180c19831b43 (patch) | |
tree | 631d116f09aa60d39fe4205507c9ac5ab667ef20 /src/cam/options.h | |
parent | 6f3503981a4f20ef3e939a9b91c29ef12d95efd2 (diff) |
cam: options: Add option type handling to options parser
Extend the options parser with support for option types. All options
must now specify the type of their argument, and the parser
automatically parses the argument and handles errors internally.
Available types are none, integer or string.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/cam/options.h')
-rw-r--r-- | src/cam/options.h | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/cam/options.h b/src/cam/options.h index b9b7bd25..8b611d37 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -17,8 +17,15 @@ enum OptionArgument { ArgumentOptional, }; +enum OptionType { + OptionNone, + OptionInteger, + OptionString, +}; + struct Option { int opt; + OptionType type; const char *name; OptionArgument argument; const char *argumentName; @@ -26,20 +33,45 @@ struct Option { bool hasShortOption() const { return isalnum(opt); } bool hasLongOption() const { return name != nullptr; } + const char *typeName() const; }; +class OptionValue; + template <typename T> class OptionsBase { public: bool valid() const; bool isSet(const T &opt) const; - const std::string &operator[](const T &opt) const; + const OptionValue &operator[](const T &opt) const; private: friend class OptionsParser; - std::map<T, std::string> values_; + + bool parseValue(const T &opt, const Option &option, const char *value); void clear(); + + std::map<T, OptionValue> values_; +}; + +class OptionValue +{ +public: + OptionValue(); + OptionValue(int value); + OptionValue(const char *value); + OptionValue(const std::string &value); + + OptionType type() const { return type_; } + + operator int() const; + operator std::string() const; + +private: + OptionType type_; + int integer_; + std::string string_; }; class OptionsParser @@ -49,7 +81,8 @@ public: { }; - bool addOption(int opt, const char *help, const char *name = nullptr, + bool addOption(int opt, OptionType type, const char *help, + const char *name = nullptr, OptionArgument argument = ArgumentNone, const char *argumentName = nullptr); @@ -57,6 +90,8 @@ public: void usage(); private: + void parseValueError(const Option &option); + std::list<Option> options_; std::map<unsigned int, Option *> optionsMap_; }; |