diff options
Diffstat (limited to 'akonadi2_cli/syntax_modules')
-rw-r--r-- | akonadi2_cli/syntax_modules/core_syntax.cpp | 86 | ||||
-rw-r--r-- | akonadi2_cli/syntax_modules/core_syntax.h | 30 |
2 files changed, 116 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 | |||
26 | namespace CoreSyntax | ||
27 | { | ||
28 | |||
29 | SyntaxTree::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 | |||
37 | bool exit(const QStringList &, State &) | ||
38 | { | ||
39 | ::exit(0); | ||
40 | return true; | ||
41 | } | ||
42 | |||
43 | bool 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 | |||
diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h new file mode 100644 index 0000000..0db6661 --- /dev/null +++ b/akonadi2_cli/syntax_modules/core_syntax.h | |||
@@ -0,0 +1,30 @@ | |||
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 | #pragma once | ||
21 | |||
22 | #include "syntaxtree.h" | ||
23 | |||
24 | namespace CoreSyntax | ||
25 | { | ||
26 | SyntaxTree::SyntaxList syntax(); | ||
27 | bool exit(const QStringList &commands, State &state); | ||
28 | bool showHelp(const QStringList &commands, State &); | ||
29 | } | ||
30 | |||