TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkcommandline.cpp
Go to the documentation of this file.
1 #include "ttkcommandline.h"
2 
3 #include <QStringList>
4 
5 void TTKCommandLineParser::setDescription(const QString &description) noexcept
6 {
7  m_description = description;
8 }
9 
11 {
12  if(option.first().isEmpty())
13  {
14  TTK_ERROR_STREAM("Command line option first key can't be empty");
15  return false;
16  }
17 
18  for(const TTKCommandLineOption &opt : qAsConst(m_options))
19  {
20  if(option == opt)
21  {
22  TTK_ERROR_STREAM("Command line option already having an option named");
23  return false;
24  }
25  }
26 
27  m_options << option;
28  return true;
29 }
30 
31 bool TTKCommandLineParser::addOptions(const QList<TTKCommandLineOption> &options)
32 {
33  bool result = true;
34  for(const TTKCommandLineOption &opt : qAsConst(options))
35  {
36  result &= addOption(opt);
37  }
38  return result;
39 }
40 
42 {
43  process(QCoreApplication::arguments().mid(1));
44 }
45 
46 void TTKCommandLineParser::process(const QStringList &arguments)
47 {
48  QString lastCommand;
49  for(const QString &arg : qAsConst(arguments))
50  {
51  const QString &cmd = arg.trimmed();
52  if(cmd.startsWith("-") || cmd.startsWith("--"))
53  {
54  m_commands.insert(cmd, {});
55  lastCommand = cmd;
56  }
57  else if(!m_commands.isEmpty() && !lastCommand.isEmpty())
58  {
59  m_commands[lastCommand] = arg;
60  }
61  }
62 }
63 
65 {
66  const QList<QString> &keys = m_commands.keys();
67  return keys.contains(option.first()) || keys.contains(option.second());
68 }
69 
71 {
72  const QString &first = m_commands.value(option.first());
73  const QString &second = m_commands.value(option.second());
74  return second.isEmpty() ? first : second;
75 }
76 
78 {
79  QString text = "TTK Command Line Module Options:\n";
80  if(!m_description.isEmpty())
81  {
82  text = m_description + text;
83  }
84 
85  for(const TTKCommandLineOption &option : qAsConst(m_options))
86  {
87  const QString &first = option.first();
88  const QString &second = option.second();
89  const QString &line = !second.isEmpty() ? (first + ", " + second) : first;
90  text += line.leftJustified(20) + option.description() + "\n";
91  }
92  TTK_LOG_STREAM(text);
93 }
bool addOptions(const QList< TTKCommandLineOption > &options)
QString value(const TTKCommandLineOption &option) const
void setDescription(const QString &description) noexcept
The class of the command line option.
QList< TTKCommandLineOption > m_options
#define qAsConst
Definition: ttkqtglobal.h:53
bool isSet(const TTKCommandLineOption &option) const
bool addOption(const TTKCommandLineOption &option)
QString second() const noexcept
#define TTK_LOG_STREAM(msg)
Definition: ttklogger.h:63
QString first() const noexcept
QHash< QString, QString > m_commands
void showHelp() const
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:69
const char * description
Definition: http_parser.c:459