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