summaryrefslogtreecommitdiffstats
path: root/akonadish/syntax_modules/core_syntax.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'akonadish/syntax_modules/core_syntax.cpp')
-rw-r--r--akonadish/syntax_modules/core_syntax.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp
new file mode 100644
index 0000000..393a0a5
--- /dev/null
+++ b/akonadish/syntax_modules/core_syntax.cpp
@@ -0,0 +1,139 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "core_syntax.h"
21
22#include <QDebug>
23#include <QObject> // tr()
24#include <QSet>
25#include <QTextStream>
26
27namespace CoreSyntax
28{
29
30Syntax::List syntax()
31{
32 Syntax::List syntax;
33 syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit);
34
35 Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp);
36 help.completer = &CoreSyntax::showHelpCompleter;
37 syntax << help;
38
39 Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session"));
40 set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel);
41 syntax << set;
42
43 Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session"));
44 get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel);
45 syntax << get;
46
47 return syntax;
48}
49
50bool exit(const QStringList &, State &)
51{
52 ::exit(0);
53 return true;
54}
55
56bool showHelp(const QStringList &commands, State &state)
57{
58 SyntaxTree::Command command = SyntaxTree::self()->match(commands);
59 if (commands.isEmpty()) {
60 state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!"));
61 state.printLine(QObject::tr("Top-level commands:"));
62
63 QSet<QString> sorted;
64 for (auto syntax: SyntaxTree::self()->syntax()) {
65 sorted.insert(syntax.keyword);
66 }
67
68 for (auto keyword: sorted) {
69 state.printLine(keyword, 1);
70 }
71 } else if (const Syntax *syntax = command.first) {
72 //TODO: get parent!
73 state.print(QObject::tr("Command `%1`").arg(syntax->keyword));
74
75 if (!syntax->help.isEmpty()) {
76 state.print(": " + syntax->help);
77 }
78 state.printLine();
79
80 if (!syntax->children.isEmpty()) {
81 state.printLine("Sub-commands:", 1);
82 QSet<QString> sorted;
83 for (auto childSyntax: syntax->children) {
84 sorted.insert(childSyntax.keyword);
85 }
86
87 for (auto keyword: sorted) {
88 state.printLine(keyword, 1);
89 }
90 }
91 } else {
92 state.printError("Unknown command: " + commands.join(" "));
93 }
94
95 return true;
96}
97
98QStringList showHelpCompleter(const QStringList &commands, const QString &fragment)
99{
100 QStringList items;
101
102 for (auto syntax: SyntaxTree::self()->syntax()) {
103 if (syntax.keyword != QObject::tr("help") &&
104 (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) {
105 items << syntax.keyword;
106 }
107 }
108
109 qSort(items);
110 return items;
111}
112
113bool setDebugLevel(const QStringList &commands, State &state)
114{
115 if (commands.count() != 1) {
116 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
117 return false;
118 }
119
120 bool ok = false;
121 int level = commands[0].toUInt(&ok);
122
123 if (!ok) {
124 state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0]));
125 return false;
126 }
127
128 state.setDebugLevel(level);
129 return true;
130}
131
132bool printDebugLevel(const QStringList &commands, State &state)
133{
134 state.printLine(QString::number(state.debugLevel()));
135 return true;
136}
137
138} // namespace CoreSyntax
139