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, QVariant::Type type, QVariant def,
25  QString help, QString longhelp);
26  CommandLineArg(const QString& name, QVariant::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  QVariant::Type m_type {QVariant::Invalid};
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  explicit MythCommandLineParser(QString appname);
133  virtual ~MythCommandLineParser();
134 
135  virtual void LoadArguments(void) {}
136  static void PrintVersion(void) ;
137  void PrintHelp(void) const;
138  QString GetHelpString(void) const;
139  virtual QString GetHelpHeader(void) const { return ""; }
140 
141  static const char* NamedOptType(Result type);
142  virtual bool Parse(int argc, const char * const * argv);
143 
144 // overloaded add constructors for single string options
145  // bool with default
146  CommandLineArg* add(const QString& arg, const QString& name, bool def,
147  QString help, QString longhelp);
148  // int
149  CommandLineArg* add(const QString& arg, const QString& name, int def,
150  QString help, QString longhelp);
151  // uint
152  CommandLineArg* add(const QString& arg, const QString& name, uint def,
153  QString help, QString longhelp);
154  // long long
155  CommandLineArg* add(const QString& arg, const QString& name, long long def,
156  QString help, QString longhelp);
157  // double
158  CommandLineArg* add(const QString& arg, const QString& name, double def,
159  QString help, QString longhelp);
160  // const char *
161  CommandLineArg* add(const QString& arg, const QString& name, const char *def,
162  QString help, QString longhelp);
163  // QString
164  CommandLineArg* add(const QString& arg, const QString& name, const QString& def,
165  QString help, QString longhelp);
166  // QSize
167  CommandLineArg* add(const QString& arg, const QString& name, QSize def,
168  QString help, QString longhelp);
169  // QDateTime
170  CommandLineArg* add(const QString& arg, const QString& name, const QDateTime& def,
171  QString help, QString longhelp);
172  // anything else
173  CommandLineArg* add(const QString& arg, const QString& name, QVariant::Type type,
174  QString help, QString longhelp);
175  // anything else with default
176  CommandLineArg* add(const QString& arg, const QString& name, QVariant::Type type,
177  QVariant def, QString help, QString longhelp);
178 
179 // overloaded add constructors for multi-string options
180  // bool with default
181  CommandLineArg* add(QStringList arglist, const QString& name, bool def,
182  QString help, QString longhelp);
183  // int
184  CommandLineArg* add(QStringList arglist, const QString& name, int def,
185  QString help, QString longhelp);
186  // uint
187  CommandLineArg* add(QStringList arglist, const QString& name, uint def,
188  QString help, QString longhelp);
189  // long long
190  CommandLineArg* add(QStringList arglist, const QString& name, long long def,
191  QString help, QString longhelp);
192  // float
193  CommandLineArg* add(QStringList arglist, const QString& name, double def,
194  QString help, QString longhelp);
195  // const char *
196  CommandLineArg* add(QStringList arglist, const QString& name, const char *def,
197  QString help, QString longhelp);
198  // QString
199  CommandLineArg* add(QStringList arglist, const QString& name, const QString& def,
200  QString help, QString longhelp);
201  // QSize
202  CommandLineArg* add(QStringList arglist, const QString& name, QSize def,
203  QString help, QString longhelp);
204  // QDateTime
205  CommandLineArg* add(QStringList arglist, const QString& name, const QDateTime& def,
206  QString help, QString longhelp);
207  // anything else
208  CommandLineArg* add(QStringList arglist, const QString& name, QVariant::Type type,
209  QString help, QString longhelp);
210  // anything else with default
211  CommandLineArg* add(QStringList arglist, const QString& name, QVariant::Type type,
212  QVariant def, QString help, QString longhelp);
213 
214  QVariant operator[](const QString &name);
215  QStringList GetArgs(void) const;
216  QMap<QString,QString> GetExtra(void) const;
217  QString GetPassthrough(void) const;
218  QMap<QString,QString> GetSettingsOverride(void);
219  QString GetLogFilePath(void);
220  int GetSyslogFacility(void) const;
221  LogLevel_t GetLogLevel(void) const;
222  QString GetAppName(void) const { return m_appname; }
223 
224  bool toBool(const QString& key) const;
225  int toInt(const QString& key) const;
226  uint toUInt(const QString& key) const;
227  long long toLongLong(const QString& key) const;
228  double toDouble(const QString& key) const;
229  QSize toSize(const QString& key) const;
230  QString toString(const QString& key) const;
231  QStringList toStringList(const QString& key, const QString& sep = "") const;
232  QMap<QString,QString> toMap(const QString& key) const;
233  QDateTime toDateTime(const QString& key) const;
234 
235  bool SetValue(const QString &key, const QVariant& value);
236  int ConfigureLogging(const QString& mask = "general",
237  bool progress = false);
238  void ApplySettingsOverride(void);
239  int Daemonize(void) const;
240 
241  protected:
242  void allowArgs(bool allow=true);
243  void allowExtras(bool allow=true);
244  void allowPassthrough(bool allow=true);
245 
246  void addHelp(void);
247  void addVersion(void);
248  void addWindowed(void);
249  void addMouse(void);
250  void addDaemon(void);
251  void addSettingsOverride(void);
252  void addRecording(void);
253  void addGeometry(void);
254  void addDisplay(void);
255  void addUPnP(void);
256  void addDVBv3(void);
257  void addLogging(const QString &defaultVerbosity = "general",
258  LogLevel_t defaultLogLevel = LOG_INFO);
259  void addPIDFile(void);
260  void addJob(void);
261  void addInFile(bool addOutFile = false);
262  void addPlatform(void);
263 
264  private:
265  Result getOpt(int argc, const char * const * argv, int &argpos,
266  QString &opt, QByteArray &val);
267  bool ReconcileLinks(void);
268 
269  QString m_appname;
270  QMap<QString,CommandLineArg*> m_optionedArgs;
271  QMap<QString,CommandLineArg*> m_namedArgs;
272  bool m_passthroughActive {false};
273  bool m_overridesImported {false};
274  bool m_verbose {false};
275 };
276 
278 
279 #endif
force
bool force
Definition: mythtv/programs/mythcommflag/main.cpp:72
build_compdb.args
args
Definition: build_compdb.py:11
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(GrabberScript *)
MythCommandLineParser::Result
Result
Definition: mythcommandlineparser.h:121
MythCommandLineParser::TestCommandLineParser
friend TestCommandLineParser
Definition: mythcommandlineparser.h:119
MythCommandLineParser::m_appname
QString m_appname
Definition: mythcommandlineparser.h:269
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
mythbaseexp.h
CommandLineArg::m_keywords
QStringList m_keywords
Definition: mythcommandlineparser.h:103
progress
bool progress
Definition: mythtv/programs/mythcommflag/main.cpp:71
CommandLineArg::m_removedversion
QString m_removedversion
Definition: mythcommandlineparser.h:98
arg
arg(title).arg(filename).arg(doDelete))
CommandLineArg::m_removed
QString m_removed
Definition: mythcommandlineparser.h:97
CommandLineArg::m_default
QVariant m_default
Definition: mythcommandlineparser.h:100
MythCommandLineParser::m_optionedArgs
QMap< QString, CommandLineArg * > m_optionedArgs
Definition: mythcommandlineparser.h:270
MythCommandLineParser::m_namedArgs
QMap< QString, CommandLineArg * > m_namedArgs
Definition: mythcommandlineparser.h:271
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
toString
QString toString(MarkTypes type)
Definition: programtypes.cpp:26
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:140
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:139
MythCommandLineParser::LoadArguments
virtual void LoadArguments(void)
Definition: mythcommandlineparser.h:135
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:222