diff options
author | Aaron Seigo <aseigo@kde.org> | 2015-12-23 11:43:37 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2015-12-23 11:43:37 +0100 |
commit | 071f4ef0122a8bfceeda9a10b41e85ad9a34a28d (patch) | |
tree | e5d1cb7efcac5eb16f4848619ff06142906b6cea /akonadi2_cli/modules/core_syntax.cpp | |
parent | 255d73af197faf8437343abc10bd98cca2057a1e (diff) | |
download | sink-071f4ef0122a8bfceeda9a10b41e85ad9a34a28d.tar.gz sink-071f4ef0122a8bfceeda9a10b41e85ad9a34a28d.zip |
vastly simplify by getting rid of Module as a base class
just a move slightly more towards functional
Diffstat (limited to 'akonadi2_cli/modules/core_syntax.cpp')
-rw-r--r-- | akonadi2_cli/modules/core_syntax.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/akonadi2_cli/modules/core_syntax.cpp b/akonadi2_cli/modules/core_syntax.cpp new file mode 100644 index 0000000..8324c31 --- /dev/null +++ b/akonadi2_cli/modules/core_syntax.cpp | |||
@@ -0,0 +1,85 @@ | |||
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 | Module::SyntaxList syntax() | ||
30 | { | ||
31 | Module::SyntaxList syntax; | ||
32 | syntax << Module::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); | ||
33 | syntax << Module::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 &) | ||
44 | { | ||
45 | Module::Command command = Module::self()->match(commands); | ||
46 | QTextStream stream(stdout); | ||
47 | if (commands.isEmpty()) { | ||
48 | stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; | ||
49 | stream << QObject::tr("Top-level commands:") << "\n"; | ||
50 | QSet<QString> sorted; | ||
51 | for (auto syntax: Module::self()->syntax()) { | ||
52 | sorted.insert(syntax.keyword); | ||
53 | } | ||
54 | |||
55 | for (auto keyword: sorted) { | ||
56 | stream << "\t" << keyword << "\n"; | ||
57 | } | ||
58 | } else if (const Module::Syntax *syntax = command.first) { | ||
59 | //TODO: get parent! | ||
60 | stream << QObject::tr("Command `%1`").arg(syntax->keyword); | ||
61 | |||
62 | if (!syntax->help.isEmpty()) { | ||
63 | stream << ": " << syntax->help; | ||
64 | } | ||
65 | stream << "\n"; | ||
66 | |||
67 | if (!syntax->children.isEmpty()) { | ||
68 | stream << "\tSub-commands:\n"; | ||
69 | QSet<QString> sorted; | ||
70 | for (auto childSyntax: syntax->children) { | ||
71 | sorted.insert(childSyntax.keyword); | ||
72 | } | ||
73 | |||
74 | for (auto keyword: sorted) { | ||
75 | stream << "\t" << keyword << "\n"; | ||
76 | } | ||
77 | } | ||
78 | } else { | ||
79 | stream << "Unknown command: " << commands.join(" ") << "\n"; | ||
80 | } | ||
81 | return true; | ||
82 | } | ||
83 | |||
84 | } // namespace CoreSyntax | ||
85 | |||