Skip to content

Commit e2dbc60

Browse files
committed
Enhance options_manager with versioning and example usage
The options_manager class has been expanded with new features. It now includes methods to set and get a version string, add example usages, print these details and a description. Additionally, color-coding has been implemented in print functions for enhanced console output.
1 parent 3c41da9 commit e2dbc60

1 file changed

Lines changed: 128 additions & 13 deletions

File tree

cclip.hpp

Lines changed: 128 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <vector>
99
#include <iostream>
1010
#include <sstream>
11-
// #include <sec_api/string_s.h>
1211

1312
using namespace std;
1413

@@ -48,17 +47,57 @@ namespace cclip
4847
const char *context;
4948
vector<option *> options;
5049
vector<option *> present_options;
50+
vector<const char *> example_usages;
51+
const char *version;
52+
const char* description;
5153

5254
option *get_option_from_global_list(const std::string &name);
5355

5456
public:
5557
/**
56-
* Create a new options manager.
57-
* @param context the application context (e.g. "myapp" or "My App")
58+
* Constructor for options_manager class.
59+
*
60+
* @param context The context of the options manager.
61+
* @param description The description of the options manager.
5862
*/
59-
explicit options_manager(const char *context);
63+
explicit options_manager(const char *context, const char* description = nullptr);
6064

61-
~options_manager() = default;
65+
/**
66+
* Add an example usage to the options manager.
67+
* @param example_usage the example usage to add
68+
*/
69+
void add_example_usage(const char *example_usage);
70+
71+
/**
72+
* Set the version of the options manager.
73+
*
74+
* @param version the version string to set (e.g. "1.0.0")
75+
*/
76+
void set_version(const char *version);
77+
78+
/**
79+
* Get the version of the options manager.
80+
*
81+
* @return the version string
82+
*/
83+
const char *get_version();
84+
85+
/**
86+
* Get the version of the options manager.
87+
*
88+
* @return the version string
89+
*/
90+
[[nodiscard]] char *get_version() const;
91+
92+
/**
93+
* Print the version of the options manager to the standard output.
94+
*/
95+
void print_version() const;
96+
97+
/**
98+
* Print examples of how to use the options manager.
99+
*/
100+
void print_examples() const;
62101

63102
/**
64103
* Add an option to the options manager.
@@ -81,7 +120,7 @@ namespace cclip
81120
/**
82121
* Print the help message to stdout.
83122
*/
84-
void print_help() const;
123+
void print_help(bool print_examples = true) const;
85124

86125
/**
87126
* Get the help message. This is useful if you want to print the help message to a file or something.<br>
@@ -105,9 +144,69 @@ namespace cclip
105144
option *get_option(const std::string &name);
106145
};
107146

108-
inline options_manager::options_manager(const char *context)
147+
inline options_manager::options_manager(const char *context, const char* description)
109148
{
110149
this->context = context;
150+
this->description = description;
151+
this->version = nullptr;
152+
}
153+
154+
inline void options_manager::add_example_usage(const char *example_usage)
155+
{
156+
this->example_usages.push_back(example_usage);
157+
}
158+
159+
inline void options_manager::set_version(const char *version)
160+
{
161+
this->version = version;
162+
}
163+
164+
inline const char *options_manager::get_version()
165+
{
166+
return this->version;
167+
}
168+
169+
inline void options_manager::print_version() const
170+
{
171+
if (this->version != nullptr)
172+
{
173+
std::cout <<
174+
#ifdef ANSIConsoleColors
175+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Green) <<
176+
#endif
177+
this->context << " " <<
178+
#ifdef ANSIConsoleColors
179+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Yellow) <<
180+
#endif
181+
this->version <<
182+
#ifdef ANSIConsoleColors
183+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Default) <<
184+
#endif
185+
std::endl;
186+
}
187+
}
188+
189+
inline void options_manager::print_examples() const
190+
{
191+
std::cout <<
192+
#ifdef ANSIConsoleColors
193+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Yellow) <<
194+
#endif
195+
"Example Usages:" <<
196+
#ifdef ANSIConsoleColors
197+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Blue) <<
198+
#endif
199+
std::endl;
200+
for (const auto &example_usage: this->example_usages)
201+
{
202+
std::cout <<
203+
example_usage <<
204+
std::endl;
205+
}
206+
207+
#ifdef ANSIConsoleColors
208+
colors::ConsoleColors::ResetConsoleColor();
209+
#endif
111210
}
112211

113212
inline option *options_manager::add_option(const char *short_name, const char *long_name, const char *description, const bool is_required, const bool has_argument)
@@ -202,23 +301,39 @@ namespace cclip
202301
}
203302
if (missing)
204303
{
205-
// std::cerr << std::endl;
206304
this->print_help();
207305
exit(1);
208306
}
209307
}
210308

211-
inline void options_manager::print_help() const
309+
inline void options_manager::print_help(const bool print_examples) const
212310
{
213311
const char *help = this->get_help();
312+
if (print_examples)
313+
{
314+
this->print_examples();
315+
}
214316
std::cout << help << std::endl;
215-
delete[] help; // Deletes the memory allocated by get_help()
317+
delete(help); // Deletes the memory allocated by get_help()
216318
}
217319

320+
218321
inline const char *options_manager::get_help() const
219322
{
220323
std::stringstream buf;
221-
buf << this->context << " Help:\n";
324+
buf <<
325+
#ifdef ANSIConsoleColors
326+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Magenta) <<
327+
#endif
328+
this->context << " Help:\n";
329+
if(this->description != nullptr)
330+
{
331+
buf <<
332+
#ifdef ANSIConsoleColors
333+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::LightGray) <<
334+
#endif
335+
this->description << "\n";
336+
}
222337

223338
for (auto &option: this->options)
224339
{
@@ -243,15 +358,15 @@ namespace cclip
243358
{
244359
buf <<
245360
#ifdef ANSIConsoleColors
246-
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Red)
361+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Red)
247362
<<
248363
#endif
249364
" (required)";
250365
}
251366

252367
buf <<
253368
#ifdef ANSIConsoleColors
254-
colors::ConsoleColors::GetColorCode(colors::ColorCodes::Green)
369+
colors::ConsoleColors::GetColorCode(colors::ColorCodes::LightGray)
255370
<<
256371
#endif
257372
"\n\t" << option->description << "\n";

0 commit comments

Comments
 (0)