10#include <QStringBuilder>
15using namespace swift::misc::physical_quantities;
19 QList<CSimpleCommandParser::CommandHtmlHelp> CSimpleCommandParser::s_commands;
20 QSet<QString> CSimpleCommandParser::s_registered;
24 this->setCheckedCommandList(knownCommands);
29 m_knownCommand =
false;
30 m_originalLine = commandLine;
32 if (!m_cleanedLine.isEmpty())
34 m_splitParts = m_cleanedLine.split(
' ');
35 if (!m_splitParts.isEmpty())
37 const QString &first = m_splitParts.constFirst();
38 const QString formatted = formatCommand(first);
41 m_commandPart = formatted;
42 m_knownCommand = m_knownCommands.
contains(formatted);
51 if (index < 0 || index >= m_splitParts.size())
return empty;
52 return m_splitParts.
at(index);
68 return m_originalLine.mid(fi).trimmed();
78 return c > 0 ? c - 1 : 0;
113 int i = p.
toInt(&ok);
163 if (help.command == command.
command)
168 s_commands.append(command);
173 if (s_registered.contains(helpContext))
177 s_registered.insert(helpContext);
183 if (s_commands.isEmpty())
188 static const QString html(
"<table style=\"font-size: 8pt; white-space: nowrap;\">\n%1\n</table>");
189 static const QString row(
"<td>%1</td><td>%2</td>");
190 static const QString rowHeader(
"<th>%1</th><th>%2</th><th>%3</th><th>%4</th>");
198 rows += u
"<tr>" % rowHeader.
arg(
"command",
"synopsis",
"command",
"synopsis") % u
"</tr>\n" % u
"<tr>" %
199 rowHeader.
arg(cmdCol, textCol, cmdCol, textCol) % u
"</tr>\n";
201 for (
int i = 0; i < cmds.
size(); i++)
213 rows += row.
arg(
"",
"");
217 return html.
arg(rows);
220 QString CSimpleCommandParser::removeLeadingDot(
const QString &candidate)
229 QString CSimpleCommandParser::formatCommand(
const QString &command)
234 bool CSimpleCommandParser::isCommand(
const QString &candidate) {
return candidate.
startsWith(
'.'); }
238 if (m_commandPart.isEmpty())
242 if (!checkCommand.
isEmpty() && formatCommand(checkCommand) == m_commandPart)
246 if (!alias1.
isEmpty() && formatCommand(alias1) == m_commandPart)
250 if (!alias2.
isEmpty() && formatCommand(alias2) == m_commandPart)
259 if (m_commandPart.isEmpty())
263 return m_commandPart.startsWith(formatCommand(startPattern));
268 if (m_commandPart.isEmpty())
272 return m_commandPart.endsWith(endPattern);
275 void CSimpleCommandParser::setCheckedCommandList(
const QStringList &commands)
277 for (
const QString &c : commands)
279 m_knownCommands.
append(formatCommand(c));
static void registerCommand(const CommandHtmlHelp &command)
Register a command.
static QString commandsHtmlHelp()
HTML commands HELP.
int countPartsWithoutCommand() const
Count parts, command excluded.
int toInt(int index, int def=-1) const
Part as integer.
bool isInt(int index) const
Is part an integer?
int countParts() const
Count parts.
bool toBool(int index, bool def=false) const
Part as bool.
bool commandStartsWith(const QString &startPattern) const
Command starting with pattern?
CSimpleCommandParser(const QStringList &knownCommands)
Constructor.
bool matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Matches given part.
bool isDouble(int index) const
Is part a double?
void parse(const QString &commandLine)
Parse.
QString partAndRemainingStringAfter(int index) const
Part and remaing string after.
double toDouble(int index, double def=-1.0) const
Part as double.
bool matchesCommand(const QString &checkCommand, const QString &alias1="", const QString &alias2="")
Is given command current command of command line.
const QString & part(int index) const
Get part, 0 is command itself.
bool hasPart(int index) const
Existing part.
static bool registered(const QString &helpContext)
Help already registered.
bool commandEndsWith(const QString &endPattern) const
Command ending with pattern?
@ SeparatorBestGuess
try to figure out
static double parseNumber(const QString &number, bool &success, SeparatorMode mode=SeparatorBestGuess)
Locale aware parsing.
Free functions in swift::misc.
SWIFT_MISC_EXPORT qsizetype nthIndexOf(const QString &string, QChar ch, int nth=1, Qt::CaseSensitivity cs=Qt::CaseInsensitive)
nth index of ch
SWIFT_MISC_EXPORT bool stringToBool(const QString &boolString)
Convert string to bool.
void append(QList< T > &&value)
qsizetype size() const const
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString right(qsizetype n) &&
QString simplified() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString toLower() const const
QString trimmed() const const
static bool less(const CommandHtmlHelp &a, const CommandHtmlHelp &b)
Compare by command.