summaryrefslogtreecommitdiffstats
path: root/akonadi2_cli/syntax_modules/core_syntax.cpp
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2015-12-23 16:43:45 +0100
committerAaron Seigo <aseigo@kde.org>2015-12-23 16:43:45 +0100
commit7e96145d27329ca3870f23d5b161785d10c9faf5 (patch)
tree46c5c673b9410341c817cc7fe6f93672718ec85d /akonadi2_cli/syntax_modules/core_syntax.cpp
parent8c78033ca7e59c44eb2886a3f8fe89c5b22ad114 (diff)
downloadsink-7e96145d27329ca3870f23d5b161785d10c9faf5.tar.gz
sink-7e96145d27329ca3870f23d5b161785d10c9faf5.zip
Module -> SyntaxTree
Diffstat (limited to 'akonadi2_cli/syntax_modules/core_syntax.cpp')
-rw-r--r--akonadi2_cli/syntax_modules/core_syntax.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp
new file mode 100644
index 0000000..f71c1d6
--- /dev/null
+++ b/akonadi2_cli/syntax_modules/core_syntax.cpp
@@ -0,0 +1,86 @@
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 <QObject> // tr()
23#include <QSet>
24#include <QTextStream>
25
26namespace CoreSyntax
27{
28
29SyntaxTree::SyntaxList syntax()
30{
31 SyntaxTree::SyntaxList syntax;
32 syntax << SyntaxTree::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit);
33 syntax << SyntaxTree::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp);
34 return syntax;
35}
36
37bool exit(const QStringList &, State &)
38{
39 ::exit(0);
40 return true;
41}
42
43bool showHelp(const QStringList &commands, State &state)
44{
45 SyntaxTree::Command command = SyntaxTree::self()->match(commands);
46 if (commands.isEmpty()) {
47 state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!"));
48 state.printLine(QObject::tr("Top-level commands:"));
49
50 QSet<QString> sorted;
51 for (auto syntax: SyntaxTree::self()->syntax()) {
52 sorted.insert(syntax.keyword);
53 }
54
55 for (auto keyword: sorted) {
56 state.printLine(keyword, 1);
57 }
58 } else if (const SyntaxTree::Syntax *syntax = command.first) {
59 //TODO: get parent!
60 state.print(QObject::tr("Command `%1`").arg(syntax->keyword));
61
62 if (!syntax->help.isEmpty()) {
63 state.print(": " + syntax->help);
64 }
65 state.printLine();
66
67 if (!syntax->children.isEmpty()) {
68 state.printLine("Sub-commands:", 1);
69 QSet<QString> sorted;
70 for (auto childSyntax: syntax->children) {
71 sorted.insert(childSyntax.keyword);
72 }
73
74 for (auto keyword: sorted) {
75 state.printLine(keyword, 1);
76 }
77 }
78 } else {
79 state.printError("Unknown command: " + commands.join(" "));
80 }
81
82 return true;
83}
84
85} // namespace CoreSyntax
86