0.5.4
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-2024 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
23#include <cli11/CLI11.hpp>
24
25#include <optional>
26
27namespace clanguml::cli {
28
29/**
30 * @brief This class holds command line parameters not directly related to
31 * specific diagram configurations.
32 */
34 int verbose{};
35 std::vector<clanguml::common::generator_type_t> generators{};
36 bool print_from{};
37 bool print_to{};
38 bool progress{};
39 unsigned int thread_count{};
41 std::string output_directory{};
42};
43
44/**
45 * This enum represents possible exit states of the command line parser.
46 */
47enum class cli_flow_t {
48 kExit, /*!< The application should exit (e.g. `-h`) */
49 kError, /*!< The options or configuration file were invalid */
50 kContinue /*!< Continue with processing diagrams */
51};
52
53/**
54 * @brief Command line options handler
55 *
56 * This class is responsible for handling the command line options
57 * and executing required actions.
58 */
60public:
61 cli_handler(std::ostream &ostr = std::cout,
62 std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt(
63 "clanguml-logger", spdlog::color_mode::automatic));
64
65 /**
66 * Main CLI handling method.
67 *
68 * @embed{cli_handle_options_sequence.svg}
69 *
70 * @param argc
71 * @param argv
72 * @return Command line handler state
73 */
74 cli_flow_t handle_options(int argc, const char **argv);
75
76 /**
77 * Print the program version and basic information
78 *
79 * @return Command line handler state
80 */
82
83 /**
84 * Print list of diagrams available in the configuration file
85 *
86 * @return Command line handler state
87 */
89
90 /**
91 * Print list of available diagram templates, including their names
92 * and types.
93 *
94 * @return Command line handler state
95 */
97
98 /**
99 * Print definition of a specific diagram template.
100 *
101 * @param template_name Name of the diagram template
102 * @return Command line handler state
103 */
104 cli_flow_t print_diagram_template(const std::string &template_name);
105
106 /**
107 * Print effective config after loading and setting default values.
108 *
109 * @return Command line handler state
110 */
112
113 /**
114 * Generate sample configuration file and exit.
115 *
116 * @return Command line handler state
117 */
119
120 /**
121 * Add example diagram of given type to the config file.
122 *
123 * @param type Type of the sample diagram to add
124 * @param config_file_path Path to the config file
125 * @param name Name of the new diagram
126 * @return Command line handler state
127 */
129 const std::string &config_file_path, const std::string &name);
130
131 /**
132 * Add diagram based on template
133 *
134 * @param config_file_path Path to the configuration file
135 * @param template_name Name of the diagram template
136 * @return Command line handler state
137 */
139 const std::string &config_file_path, const std::string &template_name);
140
141 /**
142 * Check if diagram output directory exists, if not create it
143 *
144 * @param dir Path to the output directory
145 * @return True if directory exists or has been created
146 */
147 bool ensure_output_directory_exists(const std::string &dir);
148
149 /**
150 * @brief Combines runtime configuration parameters into a single structure
151 *
152 * @return Runtime config instance
153 */
155
156 /**
157 * @brief Set the default config path
158 *
159 * @param path
160 */
161 void set_config_path(const std::string &path);
162
163 std::string config_path{".clang-uml"};
164 std::optional<std::string> compilation_database_dir{};
165 std::vector<std::string> diagram_names{};
166 std::optional<std::string> output_directory{};
168 unsigned int thread_count{};
169 bool show_version{false};
170 int verbose{};
171 bool progress{false};
172 bool list_diagrams{false};
173 bool quiet{false};
174 bool initialize{false};
176 std::optional<std::vector<std::string>> add_compile_flag;
177 std::optional<std::vector<std::string>> remove_compile_flag;
178#if !defined(_WIN32)
179 std::optional<std::string> query_driver;
180#endif
181 std::optional<std::string> add_class_diagram;
182 std::optional<std::string> add_sequence_diagram;
183 std::optional<std::string> add_package_diagram;
184 std::optional<std::string> add_include_diagram;
185 std::optional<std::string> add_diagram_from_template;
186 bool dump_config{false};
187 bool print_from{false};
188 bool print_to{false};
189 std::optional<bool> paths_relative_to_pwd{};
190 std::vector<std::string> template_variables{};
191 bool list_templates{false};
192 std::optional<bool> no_metadata{};
193 std::optional<std::string> show_template;
194 std::vector<clanguml::common::generator_type_t> generators{
196 bool no_validate{false};
197 bool validate_only{false};
198 bool render_diagrams{false};
199 std::optional<std::string> plantuml_cmd;
200 std::optional<std::string> mermaid_cmd;
201
203
204private:
205 /**
206 * This method parses the command line options using CLI11 library.
207 *
208 * @param argc
209 * @param argv
210 * @return Command line handler state
211 */
212 cli_flow_t parse(int argc, const char **argv);
213
214 /**
215 * Handle command line options before parsing the configuration file
216 *
217 * @return Command line handler state
218 */
220
221 /**
222 * Load configuration file from file or stdin
223 *
224 * @return Command line handler state
225 */
227
228 /**
229 * Handle command line options before parsing the configuration file
230 *
231 * @return Command line handler state
232 */
234
235 /**
236 * Setup spdlog library depending on provided command line options
237 */
238 void setup_logging();
239
240 std::ostream &ostr_;
241 std::shared_ptr<spdlog::logger> logger_;
242 CLI::App app{"Clang-based UML diagram generator for C++"};
243};
244} // namespace clanguml::cli