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