summaryrefslogtreecommitdiff
path: root/src/cam/options.h
diff options
context:
space:
mode:
authorNiklas Söderlund <niklas.soderlund@ragnatech.se>2019-01-28 01:02:18 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-02-01 11:42:03 +0200
commit270eb0acc79346ac0b641f405c40bf3b959a4c6f (patch)
treefca6218b4fb3e0765e2c9e6fb3f366caea03e32e /src/cam/options.h
parentc8c546fe99a343c13c7c0b8f2e5e180c19831b43 (diff)
cam: options: Add a key=value parser
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 <niklas.soderlund@ragnatech.se> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/cam/options.h')
-rw-r--r--src/cam/options.h30
1 files changed, 28 insertions, 2 deletions
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 <list>
#include <map>
+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 <typename T>
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<T, OptionValue> values_;
};
+class KeyValueParser
+{
+public:
+ class Options : public OptionsBase<std::string>
+ {
+ };
+
+ 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<std::string, Option> 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();