forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-line-app.cpp
More file actions
129 lines (112 loc) · 4.66 KB
/
command-line-app.cpp
File metadata and controls
129 lines (112 loc) · 4.66 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "command-line-app.hpp"
#include "logging.hpp"
#include "util.hpp"
#include <map>
command_line_app_t::command_line_app_t(std::string app_description)
: CLI::App(std::move(app_description))
{
set_help_flag(); // Disable autogenerated help option
add_flag("-h,--help", "Print this help message and exit.");
add_flag("-V,--version", "Show version and exit.");
}
bool command_line_app_t::want_help() const { return count("--help"); }
bool command_line_app_t::want_version() const { return count("--version"); }
void command_line_app_t::init_database_options()
{
add_option_function<std::string>("-d,--database",
[&](std::string const &value) {
m_connection_params.set("dbname",
value);
})
->description("Database name or PostgreSQL conninfo string.")
->type_name("DB")
->group("Database options");
add_option_function<std::string>("-U,--username,--user",
[&](std::string const &value) {
m_connection_params.set("user", value);
})
->description("Database user.")
->type_name("USERNAME")
->group("Database options");
add_flag_function("-W,--password",
[&](int64_t) {
m_connection_params.set("password",
util::get_password());
})
->description("Force password prompt.")
->group("Database options");
add_option_function<std::string>("-H,--host",
[&](std::string const &value) {
m_connection_params.set("host", value);
})
->description(
"Database server hostname or unix domain socket location.")
->type_name("HOST")
->group("Database options");
add_option_function<std::string>("-P,--port",
[&](std::string const &value) {
m_connection_params.set("port", value);
})
->description("Database server port.")
->type_name("PORT")
->group("Database options");
}
void command_line_app_t::init_logging_options(bool with_progress, bool with_sql)
{
static std::map<std::string, log_level> const log_levels_map{
{"debug", log_level::debug},
{"info", log_level::info},
{"warn", log_level::warn},
{"warning", log_level::warn},
{"error", log_level::error}};
add_option_function<std::string>(
"--log-level",
[](std::string const &level) {
get_logger().set_level(log_levels_map.find(level)->second);
})
->description(
"Set log level ('debug', 'info' (default), 'warn', or 'error').")
->check(CLI::IsMember(log_levels_map))
->default_val("info")
->run_callback_for_default()
->option_text("LEVEL")
->group("Logging options");
if (with_progress) {
add_option_function<std::string>(
"--log-progress",
[&](std::string const &arg) {
if (arg == "true") {
get_logger().enable_progress();
} else if (arg == "false") {
get_logger().disable_progress();
} else if (arg == "auto") {
get_logger().auto_progress();
} else {
throw fmt_error(
"Unknown value for --log-progress option: {}", arg);
}
})
->description(
"Log progress to console ('true', 'false', 'auto' (default)).")
->option_text("PROGRESS")
->group("Logging options");
}
if (with_sql) {
add_flag_function("--log-sql",
[](int64_t) { get_logger().enable_sql(); })
->description("Enable logging of SQL commands for debugging.")
->group("Logging options");
add_flag_function("--log-sql-data",
[](int64_t) { get_logger().enable_sql_data(); })
->description("Enable logging of all data added to the database.")
->group("Logging options");
}
}