MythTV  master
mythcommandlineparser.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #include <QStringList>
4 #include <QDateTime>
5 #include <QSize>
6 #include <QMap>
7 #include <QString>
8 #include <QVariant>
9 
10 #include <cstdint> // for uint64_t
11 
12 #include "mythbaseexp.h"
13 #include "mythlogging.h"
14 #include "referencecounter.h"
15 
17 
19 {
20  public:
21  CommandLineArg(const QString& name, QVariant::Type type, QVariant def,
22  QString help, QString longhelp);
23  CommandLineArg(const QString& name, QVariant::Type type, QVariant def);
24  explicit CommandLineArg(const QString& name);
25  ~CommandLineArg() = default;
26 
27  CommandLineArg* SetGroup(const QString &group) { m_group = group;
28  return this; }
29  void AddKeyword(const QString &keyword) { m_keywords << keyword; }
30 
31  QString GetName(void) const { return m_name; }
32  QString GetUsedKeyword(void) const { return m_usedKeyword; }
33  int GetKeywordLength(void) const;
34  QString GetHelpString(int off, const QString& group = "",
35  bool force = false) const;
36  QString GetLongHelpString(QString keyword) const;
37 
38  bool Set(const QString& opt);
39  bool Set(const QString& opt, const QByteArray& val);
40  void Set(QVariant val) { m_stored = val;
41  m_given = true; }
42 
43  CommandLineArg* SetParent(QString opt);
44  CommandLineArg* SetParent(QStringList opts);
45  CommandLineArg* SetParentOf(QString opt);
46  CommandLineArg* SetParentOf(QStringList opts);
47 
48  CommandLineArg* SetChild(QString opt);
49  CommandLineArg* SetChild(QStringList opt);
50  CommandLineArg* SetChildOf(QString opt);
51  CommandLineArg* SetChildOf(QStringList opts);
52 
53  CommandLineArg* SetRequiredChild(const QString& opt);
54  CommandLineArg* SetRequiredChild(QStringList opt);
55  CommandLineArg* SetRequiredChildOf(const QString& opt);
56  CommandLineArg* SetRequiredChildOf(QStringList opt);
57 
58  CommandLineArg* SetRequires(QString opt);
59  CommandLineArg* SetRequires(QStringList opts);
60  CommandLineArg* SetBlocks(QString opt);
61  CommandLineArg* SetBlocks(QStringList opts);
62 
63  CommandLineArg* SetDeprecated(QString depstr = "");
64  CommandLineArg* SetRemoved(QString remstr = "", QString remver = "");
65 
66  static void AllowOneOf(QList<CommandLineArg*> args);
67 
68  void PrintVerbose(void) const;
69 
70  friend class MythCommandLineParser;
71 
72  private:
73  QString GetKeywordString(void) const;
74 
75  void SetParentOf(CommandLineArg *other, bool forward = true);
76  void SetChildOf(CommandLineArg *other, bool forward = true);
77  void SetRequires(CommandLineArg *other, bool forward = true);
78  void SetBlocks(CommandLineArg *other, bool forward = true);
79 
80  void Convert(void);
81 
82  QString GetPreferredKeyword(void) const;
83  bool TestLinks(void) const;
84  void CleanupLinks(void);
85 
86  void PrintRemovedWarning(QString &keyword) const;
87  void PrintDeprecatedWarning(QString &keyword) const;
88 
89  bool m_given {false};
90  bool m_converted {false};
91  QString m_name;
92  QString m_group;
93  QString m_deprecated;
94  QString m_removed;
96  QVariant::Type m_type {QVariant::Invalid};
97  QVariant m_default;
98  QVariant m_stored;
99 
100  QStringList m_keywords;
101  QString m_usedKeyword;
102 
103  QList<CommandLineArg*> m_parents;
104  QList<CommandLineArg*> m_children;
105  QList<CommandLineArg*> m_requires;
106  QList<CommandLineArg*> m_requiredby;
107  QList<CommandLineArg*> m_blocks;
108 
109  QString m_help;
110  QString m_longhelp;
111 };
112 
114 {
115  public:
116  explicit MythCommandLineParser(QString);
118 
119  virtual void LoadArguments(void) {};
120  void PrintVersion(void) const;
121  void PrintHelp(void) const;
122  QString GetHelpString(void) const;
123  virtual QString GetHelpHeader(void) const { return ""; }
124 
125  virtual bool Parse(int argc, const char * const * argv);
126 
127 // overloaded add constructors for single string options
128  // bool with default
129  CommandLineArg* add(QString arg, QString name, bool def,
130  QString help, QString longhelp)
131  { return add(QStringList(arg), name, QVariant::Bool,
132  QVariant(def), help, longhelp); }
133  // int
134  CommandLineArg* add(QString arg, QString name, int def,
135  QString help, QString longhelp)
136  { return add(QStringList(arg), name, QVariant::Int,
137  QVariant(def), help, longhelp); }
138  // uint
139  CommandLineArg* add(QString arg, QString name, uint def,
140  QString help, QString longhelp)
141  { return add(QStringList(arg), name, QVariant::UInt,
142  QVariant(def), help, longhelp); }
143  // long long
144  CommandLineArg* add(QString arg, QString name, long long def,
145  QString help, QString longhelp)
146  { return add(QStringList(arg), name, QVariant::LongLong,
147  QVariant(def), help, longhelp); }
148  // double
149  CommandLineArg* add(QString arg, QString name, double def,
150  QString help, QString longhelp)
151  { return add(QStringList(arg), name, QVariant::Double,
152  QVariant(def), help, longhelp); }
153  // const char *
154  CommandLineArg* add(QString arg, QString name, const char *def,
155  QString help, QString longhelp)
156  { return add(QStringList(arg), name, QVariant::String,
157  QVariant(def), help, longhelp); }
158  // QString
159  CommandLineArg* add(QString arg, QString name, QString def,
160  QString help, QString longhelp)
161  { return add(QStringList(arg), name, QVariant::String,
162  QVariant(def), help, longhelp); }
163  // QSize
164  CommandLineArg* add(QString arg, QString name, QSize def,
165  QString help, QString longhelp)
166  { return add(QStringList(arg), name, QVariant::Size,
167  QVariant(def), help, longhelp); }
168  // QDateTime
169  CommandLineArg* add(QString arg, QString name, QDateTime def,
170  QString help, QString longhelp)
171  { return add(QStringList(arg), name, QVariant::DateTime,
172  QVariant(def), help, longhelp); }
173  // anything else
174  CommandLineArg* add(QString arg, QString name, QVariant::Type type,
175  QString help, QString longhelp)
176  { return add(QStringList(arg), name, type,
177  QVariant(type), help, longhelp); }
178  // anything else with default
179  CommandLineArg* add(QString arg, QString name, QVariant::Type type,
180  QVariant def, QString help, QString longhelp)
181  { return add(QStringList(arg), name, type,
182  def, help, longhelp); }
183 
184 // overloaded add constructors for multi-string options
185  // bool with default
186  CommandLineArg* add(QStringList arglist, QString name, bool def,
187  QString help, QString longhelp)
188  { return add(arglist, name, QVariant::Bool,
189  QVariant(def), help, longhelp); }
190  // int
191  CommandLineArg* add(QStringList arglist, QString name, int def,
192  QString help, QString longhelp)
193  { return add(arglist, name, QVariant::Int,
194  QVariant(def), help, longhelp); }
195  // uint
196  CommandLineArg* add(QStringList arglist, QString name, uint def,
197  QString help, QString longhelp)
198  { return add(arglist, name, QVariant::UInt,
199  QVariant(def), help, longhelp); }
200  // long long
201  CommandLineArg* add(QStringList arglist, QString name, long long def,
202  QString help, QString longhelp)
203  { return add(arglist, name, QVariant::LongLong,
204  QVariant(def), help, longhelp); }
205  // float
206  CommandLineArg* add(QStringList arglist, QString name, double def,
207  QString help, QString longhelp)
208  { return add(arglist, name, QVariant::Double,
209  QVariant(def), help, longhelp); }
210  // const char *
211  CommandLineArg* add(QStringList arglist, QString name, const char *def,
212  QString help, QString longhelp)
213  { return add(arglist, name, QVariant::String,
214  QVariant(def), help, longhelp); }
215  // QString
216  CommandLineArg* add(QStringList arglist, QString name, QString def,
217  QString help, QString longhelp)
218  { return add(arglist, name, QVariant::String,
219  QVariant(def), help, longhelp); }
220  // QSize
221  CommandLineArg* add(QStringList arglist, QString name, QSize def,
222  QString help, QString longhelp)
223  { return add(arglist, name, QVariant::Size,
224  QVariant(def), help, longhelp); }
225  // QDateTime
226  CommandLineArg* add(QStringList arglist, QString name, QDateTime def,
227  QString help, QString longhelp)
228  { return add(arglist, name, QVariant::DateTime,
229  QVariant(def), help, longhelp); }
230  // anything else
231  CommandLineArg* add(QStringList arglist, QString name, QVariant::Type type,
232  QString help, QString longhelp)
233  { return add(arglist, name, type,
234  QVariant(type), help, longhelp); }
235  // anything else with default
236  CommandLineArg* add(QStringList arglist, const QString& name, QVariant::Type type,
237  QVariant def, QString help, QString longhelp);
238 
239  QVariant operator[](const QString &name);
240  QStringList GetArgs(void) const;
241  QMap<QString,QString> GetExtra(void) const;
242  QString GetPassthrough(void) const;
243  QMap<QString,QString> GetSettingsOverride(void);
244  QString GetLogFilePath(void);
245  int GetSyslogFacility(void);
246  LogLevel_t GetLogLevel(void);
247  QString GetAppName(void) const { return m_appname; }
248 
249  bool toBool(const QString& key) const;
250  int toInt(const QString& key) const;
251  uint toUInt(const QString& key) const;
252  long long toLongLong(const QString& key) const;
253  double toDouble(const QString& key) const;
254  QSize toSize(const QString& key) const;
255  QString toString(const QString& key) const;
256  QStringList toStringList(const QString& key, const QString& sep = "") const;
257  QMap<QString,QString> toMap(const QString& key) const;
258  QDateTime toDateTime(const QString& key) const;
259 
260  bool SetValue(const QString &key, const QVariant& value);
261  int ConfigureLogging(QString mask = "general",
262  unsigned int progress = 0);
263  void ApplySettingsOverride(void);
264  int Daemonize(void);
265 
266  protected:
267  void allowArgs(bool allow=true);
268  void allowExtras(bool allow=true);
269  void allowPassthrough(bool allow=true);
270 
271  void addHelp(void);
272  void addVersion(void);
273  void addWindowed(void);
274  void addMouse(void);
275  void addDaemon(void);
276  void addSettingsOverride(void);
277  void addRecording(void);
278  void addGeometry(void);
279  void addDisplay(void);
280  void addUPnP(void);
281  void addLogging(const QString &defaultVerbosity = "general",
282  LogLevel_t defaultLogLevel = LOG_INFO);
283  void addPIDFile(void);
284  void addJob(void);
285  void addInFile(bool addOutFile = false);
286 
287  private:
288  int getOpt(int argc, const char * const * argv, int &argpos,
289  QString &opt, QByteArray &val);
290  bool ReconcileLinks(void);
291 
292  QString m_appname;
293  QMap<QString,CommandLineArg*> m_optionedArgs;
294  QMap<QString,CommandLineArg*> m_namedArgs;
295  bool m_passthroughActive {false};
296  bool m_overridesImported {false};
297  bool m_verbose {false};
298 };
CommandLineArg * add(QString arg, QString name, const char *def, QString help, QString longhelp)
CommandLineArg * add(QString arg, QString name, QSize def, QString help, QString longhelp)
virtual void LoadArguments(void)
CommandLineArg * add(QString arg, QString name, bool def, QString help, QString longhelp)
CommandLineArg * add(QString arg, QString name, double def, QString help, QString longhelp)
virtual QString GetHelpHeader(void) const
CommandLineArg * add(QStringList arglist, QString name, QDateTime def, QString help, QString longhelp)
QList< CommandLineArg * > m_parents
QList< CommandLineArg * > m_requires
CommandLineArg * add(QStringList arglist, QString name, bool def, QString help, QString longhelp)
QString toString(MarkTypes type)
QMap< QString, CommandLineArg * > m_optionedArgs
General purpose reference counter.
CommandLineArg * add(QStringList arglist, QString name, long long def, QString help, QString longhelp)
Parent class for defining application command line parsers.
unsigned int uint
Definition: compat.h:140
Definition for a single command line option.
CommandLineArg * add(QStringList arglist, QString name, const char *def, QString help, QString longhelp)
void Set(QVariant val)
CommandLineArg * add(QString arg, QString name, uint def, QString help, QString longhelp)
CommandLineArg * add(QString arg, QString name, QVariant::Type type, QString help, QString longhelp)
CommandLineArg * add(QStringList arglist, QString name, QSize def, QString help, QString longhelp)
CommandLineArg * add(QString arg, QString name, long long def, QString help, QString longhelp)
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
CommandLineArg * add(QString arg, QString name, QVariant::Type type, QVariant def, QString help, QString longhelp)
QMap< QString, CommandLineArg * > m_namedArgs
void AddKeyword(const QString &keyword)
CommandLineArg * add(QStringList arglist, QString name, double def, QString help, QString longhelp)
CommandLineArg * add(QString arg, QString name, QDateTime def, QString help, QString longhelp)
CommandLineArg * add(QStringList arglist, QString name, QVariant::Type type, QString help, QString longhelp)
CommandLineArg * add(QStringList arglist, QString name, int def, QString help, QString longhelp)
QString GetName(void) const
CommandLineArg * add(QString arg, QString name, int def, QString help, QString longhelp)
const char * name
Definition: ParseText.cpp:328
QList< CommandLineArg * > m_requiredby
CommandLineArg * add(QString arg, QString name, QString def, QString help, QString longhelp)
CommandLineArg * SetGroup(const QString &group)
QList< CommandLineArg * > m_blocks
QString GetAppName(void) const
QList< CommandLineArg * > m_children
CommandLineArg * add(QStringList arglist, QString name, uint def, QString help, QString longhelp)
QString GetUsedKeyword(void) const
CommandLineArg * add(QStringList arglist, QString name, QString def, QString help, QString longhelp)