diff options
Diffstat (limited to 'akonadi2_cli/modules/help/help.cpp')
-rw-r--r-- | akonadi2_cli/modules/help/help.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/akonadi2_cli/modules/help/help.cpp b/akonadi2_cli/modules/help/help.cpp new file mode 100644 index 0000000..aaff6fb --- /dev/null +++ b/akonadi2_cli/modules/help/help.cpp | |||
@@ -0,0 +1,80 @@ | |||
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 "help.h" | ||
21 | |||
22 | #include <QObject> | ||
23 | #include <QSet> | ||
24 | #include <QTextStream> | ||
25 | |||
26 | #include "module.h" | ||
27 | |||
28 | namespace CLI | ||
29 | { | ||
30 | |||
31 | Help::Help() | ||
32 | { | ||
33 | Syntax topLevel = Syntax(QObject::tr("help"), &Help::showHelp, QObject::tr("Print command information: help [command]")); | ||
34 | setSyntax(topLevel); | ||
35 | } | ||
36 | |||
37 | bool Help::showHelp(const QStringList &commands, State &) | ||
38 | { | ||
39 | Module::Command command = Module::match(commands); | ||
40 | QTextStream stream(stdout); | ||
41 | if (commands.isEmpty()) { | ||
42 | stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; | ||
43 | stream << QObject::tr("Top-level commands:") << "\n"; | ||
44 | QSet<QString> sorted; | ||
45 | for (auto module: Module::modules()) { | ||
46 | sorted.insert(module.syntax().keyword); | ||
47 | } | ||
48 | |||
49 | for (auto keyword: sorted) { | ||
50 | stream << "\t" << keyword << "\n"; | ||
51 | } | ||
52 | } else if (const Syntax *syntax = command.first) { | ||
53 | //TODO: get parent! | ||
54 | stream << QObject::tr("Command `%1`").arg(syntax->keyword); | ||
55 | |||
56 | if (!syntax->help.isEmpty()) { | ||
57 | stream << ": " << syntax->help; | ||
58 | } | ||
59 | stream << "\n"; | ||
60 | |||
61 | if (!syntax->children.isEmpty()) { | ||
62 | stream << "\tSub-commands:\n"; | ||
63 | QSet<QString> sorted; | ||
64 | for (auto childSyntax: syntax->children) { | ||
65 | sorted.insert(childSyntax.keyword); | ||
66 | } | ||
67 | |||
68 | for (auto keyword: sorted) { | ||
69 | stream << "\t" << keyword << "\n"; | ||
70 | } | ||
71 | } | ||
72 | } else { | ||
73 | stream << "Unknown command: " << commands.join(" ") << "\n"; | ||
74 | } | ||
75 | |||
76 | return true; | ||
77 | } | ||
78 | |||
79 | } // namespace CLI | ||
80 | |||