0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
cli_handler.h
Go to the documentation of this file.
1/**
2 * @file src/options/cli_handler.h
3 *
4 * Copyright (c) 2021-2025 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
20#include "common/model/enums.h"
21#include "config/config.h"
22#include "util/logging.h"
23
24#include <cli11/CLI11.hpp>
25
26#include <optional>
27
28namespace clanguml::cli {
29
30/**
31 * @brief This class holds command line parameters not directly related to
32 * specific diagram configurations.
33 */
35 int verbose{};
36 std::vector<clanguml::common::generator_type_t> generators{};
37 bool print_from{};
38 bool print_to{};
39 bool progress{};
40 unsigned int thread_count{};
42 std::string output_directory{};
43};
44
45/**
46 * This enum represents possible exit states of the command line parser.
47 */
48enum class cli_flow_t {
49 kExit, /*!< The application should exit (e.g. `-h`) */
50 kError, /*!< The options or configuration file were invalid */
51 kContinue /*!< Continue with processing diagrams */
52};
53
54/**
55 * @brief Command line options handler
56 *
57 * This class is responsible for handling the command line options
58 * and executing required actions.
59 */
61public:
62 cli_handler(std::ostream &ostr = std::cout,
63 std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt(
64 "clanguml-logger", spdlog::color_mode::automatic));
65
66 /**
67 * Main CLI handling method.
68 *
69 * @embed{cli_handle_options_sequence.svg}
70 *
71 * @param argc
72 * @param argv
73 * @return Command line handler state
74 */
75 cli_flow_t handle_options(int argc, const char **argv);
76
77 /**
78 * Print the program version and basic information
79 *
80 * @return Command line handler state
81 */
83
84 /**
85 * Print list of diagrams available in the configuration file
86 *
87 * @return Command line handler state
88 */
90
91 /**
92 * Print list of available diagram templates, including their names
93 * and types.
94 *
95 * @return Command line handler state
96 */
98
99 /**
100 * Print definition of a specific diagram template.
101 *
102 * @param template_name Name of the diagram template
103 * @return Command line handler state
104 */
105 cli_flow_t print_diagram_template(const std::string &template_name);
106
107 /**
108 * Print effective config after loading and setting default values.
109 *
110 * @return Command line handler state
111 */
113
114 /**
115 * Generate sample configuration file and exit.
116 *
117 * @return Command line handler state
118 */
120
121 /**
122 * Add example diagram of given type to the config file.
123 *
124 * @param type Type of the sample diagram to add
125 * @param config_file_path Path to the config file
126 * @param name Name of the new diagram
127 * @return Command line handler state
128 */
130 const std::string &config_file_path, const std::string &name);
131
132 /**
133 * Add diagram based on template
134 *
135 * @param config_file_path Path to the configuration file
136 * @param template_name Name of the diagram template
137 * @return Command line handler state
138 */
140 const std::string &config_file_path, const std::string &template_name);
141
142 /**
143 * Render diagram template
144 *
145 * @param template_name Template name
146 * @param diagram_node Reference to YAML output node
147 * @return Command line handler state
148 */
150 const std::string &template_name, YAML::Node &diagram_node);
151
152 /**
153 * Generate diagram based on template
154 *
155 * @param template_name Name of the diagram template
156 * @return Command line handler state
157 */
158 cli_flow_t generate_diagram_from_template(const std::string &template_name);
159
160 /**
161 * Check if diagram output directory exists, if not create it
162 *
163 * @param dir Path to the output directory
164 * @return True if directory exists or has been created
165 */
166 bool ensure_output_directory_exists(const std::string &dir);
167
168 /**
169 * @brief Combines runtime configuration parameters into a single structure
170 *
171 * @return Runtime config instance
172 */
174
175 /**
176 * @brief Set the default config path
177 *
178 * @param path
179 */
180 void set_config_path(const std::string &path);
181
182 static void create_json_progress_logger(spdlog::sink_ptr sink = {});
183
184 std::string config_path{".clang-uml"};
185 std::optional<std::string> compilation_database_dir{};
186 std::vector<std::string> diagram_names{};
187 std::optional<std::string> output_directory{};
189 unsigned int thread_count{};
190 bool show_version{false};
191 int verbose{};
192 logging::logger_type_t logger_type{logging::logger_type_t::text};
193 bool progress{false};
194 bool list_diagrams{false};
195 bool quiet{false};
196 bool initialize{false};
198 std::optional<std::vector<std::string>> add_compile_flag;
199 std::optional<std::vector<std::string>> remove_compile_flag;
200 std::vector<std::pair<std::string, std::string>> user_data;
201#if !defined(_WIN32)
202 std::optional<std::string> query_driver;
203#endif
204 std::optional<std::string> add_class_diagram;
205 std::optional<std::string> add_sequence_diagram;
206 std::optional<std::string> add_package_diagram;
207 std::optional<std::string> add_include_diagram;
208 std::optional<std::string> add_diagram_from_template;
209 std::optional<std::string> generate_from_template;
210 bool dump_config{false};
211 bool print_from{false};
212 bool print_to{false};
213 std::optional<bool> paths_relative_to_pwd{};
214 std::vector<std::string> template_variables{};
215 bool list_templates{false};
216 std::optional<bool> no_metadata{};
217 std::optional<std::string> show_template;
218 std::vector<clanguml::common::generator_type_t> generators{
220 bool no_validate{false};
221 bool validate_only{false};
222 bool render_diagrams{false};
223 std::optional<std::string> plantuml_cmd;
224 std::optional<std::string> mermaid_cmd;
225
227
228private:
229 /**
230 * This method parses the command line options using CLI11 library.
231 *
232 * @param argc
233 * @param argv
234 * @return Command line handler state
235 */
236 cli_flow_t parse(int argc, const char **argv);
237
238 /**
239 * Handle command line options before parsing the configuration file
240 *
241 * @return Command line handler state
242 */
244
245 /**
246 * Load configuration file from file or stdin
247 *
248 * @return Command line handler state
249 */
251
252 /**
253 * Handle command line options before parsing the configuration file
254 *
255 * @return Command line handler state
256 */
258
259 /**
260 * Handle custom Jinja context user data provider through `--user-data`
261 *
262 * @return Command line handler state
263 */
265
266 /**
267 * Setup spdlog library depending on provided command line options
268 */
269 void setup_logging();
270
271 std::ostream &ostr_;
272 std::shared_ptr<spdlog::logger> logger_;
273 CLI::App app{"Clang-based UML diagram generator for C++"};
274};
275} // namespace clanguml::cli