-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
command_help.cpp
98 lines (76 loc) · 2.89 KB
/
command_help.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Osmium -- OpenStreetMap data manipulation command line tool
http://osmcode.org/osmium
Copyright (C) 2013-2016 Jochen Topf <jochen@topf.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include "command_help.hpp"
bool CommandHelp::setup(const std::vector<std::string>& arguments) {
m_topic = arguments.empty() ? "help" : arguments.front();
return true;
}
void show_help(const std::string& topic, const std::string& info) {
#ifndef _MSC_VER
// show man page on non-Windows systems
std::string manpage{"osmium-"};
manpage += topic;
::execlp("man", "man", manpage.c_str(), nullptr);
// if exec fails, fall thru
#endif
// show info string and link on Windows systems
std::cout << info << "\n";
std::cout << "You'll find more documentation at http://osmcode.org/osmium/\n";
}
bool CommandHelp::run() {
auto commands = CommandFactory::help();
if (m_topic == "help") {
std::cout << "Usage: " << synopsis()
<< "\n\nCOMMANDS:\n";
// print command names and descriptions in a nice table
for (const auto& cmd : commands) {
std::cout << " "
<< std::setw(CommandFactory::max_command_name_length())
<< std::left
<< cmd.first
<< std::setw(0)
<< " "
<< cmd.second
<< "\n";
}
std::cout << "\nUse 'osmium COMMAND -h' for short usage information.\n"
"Use 'osmium help COMMAND' for detailed information on a specific command.\n";
return true;
}
std::string description = CommandFactory::get_description(m_topic);
if (!description.empty()) {
show_help(m_topic, std::string{"osmium "} + m_topic + ": " + description);
return true;
}
if (m_topic == "file-formats") {
show_help("file-formats", "osmium file-formats: Supported formats are 'xml', 'pbf', and 'opl'.");
return true;
}
std::cerr << "Unknown help topic '" << m_topic << "'.\n";
return false;
}
namespace {
const bool register_help_command = CommandFactory::add("help", "Show osmium help", []() {
return new CommandHelp();
});
}