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