Langmuir
Simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clparser.h
Go to the documentation of this file.
1 #ifndef CLPARSER_H
2 #define CLPARSER_H
3 
4 #include <QStringList>
5 #include <QString>
6 #include <QObject>
7 #include <QDebug>
8 #include <QMap>
9 
10 namespace LangmuirCore {
11 
15 class CommandLineParser : public QObject
16 {
17  Q_OBJECT
18 public:
23  explicit CommandLineParser(QObject *parent = 0);
24 
29  void setDescription(const QString& desc);
30 
37  void addBool(QString flag, QString dest, QString help);
38 
44  void addPositional(QString dest, QString help);
45 
52  void add(QString flag, QString dest, QString help);
53 
59  template <typename T> T get(const QString& dest, T default_value);
60 
65  void parse(QStringList &args);
66 
70  QString help();
71 
72 protected:
74  QMap<QString,bool> m_isBool;
75 
77  QMap<QString,int> m_isPositional;
78 
80  QMap<QString,QString> m_flags;
81 
83  QMap<QString,QString> m_helps;
84 
86  QMap<QString,QString> m_values;
87 
89  QString m_description;
90 
92  QStringList m_args;
93 
95  unsigned int m_numPositional;
96 
98  unsigned int m_numArguments;
99 
104  template <typename T> T convert(const QString& value);
105 };
106 
107 template <typename T> T CommandLineParser::get(const QString &dest, T default_value)
108 {
109  if (!m_values.contains(dest)) {
110  qDebug() << "langmuir: invalid parser key..." << qPrintable(dest);
111  exit(-1);
112  }
113  if (m_values[dest].isEmpty()) {
114  return default_value;
115  }
116  return convert<T>(m_values[dest]);
117 }
118 
119 }
120 #endif // CLPARSER_H
A class to parse command line arguments.
Definition: clparser.h:15
void setDescription(const QString &desc)
set the program description
QString help()
get the help string
void addBool(QString flag, QString dest, QString help)
add a flag that has no argument
T convert(const QString &value)
convert value to type
QMap< QString, QString > m_flags
map dest->flag
Definition: clparser.h:80
QMap< QString, QString > m_helps
map dest->help
Definition: clparser.h:83
CommandLineParser(QObject *parent=0)
create the CommandLineParser
QStringList m_args
list of remaining command line arguments
Definition: clparser.h:92
unsigned int m_numArguments
total number of arguments
Definition: clparser.h:98
QMap< QString, QString > m_values
map dest->value
Definition: clparser.h:86
QMap< QString, int > m_isPositional
map dest->boolean
Definition: clparser.h:77
Definition: agent.h:12
T get(const QString &dest, T default_value)
get value by key and convert to type
Definition: clparser.h:107
void parse(QStringList &args)
parse the command line arguments
QMap< QString, bool > m_isBool
map dest->boolean
Definition: clparser.h:74
unsigned int m_numPositional
total number of positional arguments
Definition: clparser.h:95
void addPositional(QString dest, QString help)
add an argument that has no flag
QString m_description
description string
Definition: clparser.h:89
void add(QString flag, QString dest, QString help)
add a flag that has an argument