2#ifndef _CONSOLIX_CLI_COMPONENT_HPP_INCLUDED
3#define _CONSOLIX_CLI_COMPONENT_HPP_INCLUDED
9#if CONSOLIX_USE_CXXOPTS == 1
22 class CliComponent :
public IAppComponent {
30 const std::string& name,
31 const std::string& description,
32 std::function<
void(CliOptions&)> creator)
33 : m_creator(std::move(creator)) {
34 register_service<CliOptions>([&name, &description]() {
35 return std::make_shared<CliOptions>(name, description);
46 const std::string& name,
47 const std::string& description,
48 std::function<
void(CliOptions&)> creator,
51 : m_creator(std::move(creator)) {
52 register_service<CliOptions>([&name, &description](){
53 return std::make_shared<CliOptions>(name, description);
56 for (
int i = 0; i < argc; ++i) {
57 m_argv_c.push_back(argv[i]);
66 void add_option(
const std::string& key,
const std::string& description) {
67 get_service<CliOptions>().add_options()(key, description, cxxopts::value<T>());
76 void add_option(
const std::string& key,
const std::string& description, T default_value) {
77 get_service<CliOptions>().add_options()(key, description, cxxopts::value<T>()->default_value(std::to_string(default_value)));
86 void add_option_implicit(
const std::string& key,
const std::string& description, T implicit_value) {
87 get_service<CliOptions>().add_options()(key, description, cxxopts::value<T>()->implicit_value(std::to_string(implicit_value)));
93 void parse(
int argc,
const char* argv[]) {
95 auto result = get_service<CliOptions>().parse(argc, argv);
96 register_service<CliArguments>([&result](){
97 return std::make_shared<CliArguments>(result);
99 }
catch (
const std::exception& e) {
100 CONSOLIX_STREAM() <<
"Error parsing command-line arguments: " << e.what() << std::endl;
101# if CONSOLIX_USE_LOGIT == 1
102 LOGIT_PRINT_ERROR(
"Error parsing command-line arguments: ", e.what());
113 template <
typename T>
114 T get(
const std::string& key)
const {
115 if (!get_service<CliArguments>().count(key)) {
116# if CONSOLIX_USE_LOGIT == 1
117 LOGIT_PRINT_ERROR(
"Option not found: ", key);
119 throw std::runtime_error(
"Option not found: " + key);
121 return get_service<CliArguments>()[key].as<T>();
128 bool initialize()
override {
130 auto &options = get_service<CliOptions>();
132 throw std::runtime_error(
"CLI options creator function is null. Ensure you provide a valid function to initialize CLI options.");
135# if defined(_WIN32) || defined(_WIN64)
139 parse(m_argc, m_argv_c.data());
142 }
catch (
const std::exception& e) {
143# if CONSOLIX_USE_LOGIT == 1
144 LOGIT_PRINT_ERROR(
"CLI component initialization failed: ", e.what());
147 CONSOLIX_STREAM() <<
"Error initializing CLI component: " << e.what() << std::endl;
157 bool is_initialized()
const override {
162 void process()
override {}
166 std::vector<const char*> m_argv_c;
167 std::function<void(CliOptions&)> m_creator;
168 std::atomic<bool> m_is_init{
false};
170# if defined(_WIN32) || defined(_WIN64)
172 void parse_windows() {
174 LPWSTR* argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
176 std::vector<std::string> args;
177 for (
int i = 0; i < argc; ++i) {
178 args.push_back(utf16_to_utf8(argv_w[i]));
182 std::vector<const char*> argv_c;
183 for (
const auto& arg : args) {
184 argv_c.push_back(arg.c_str());
187 parse(argc, argv_c.data());
#define CONSOLIX_STREAM()
Fallback for general logging.
< Utility modules and helpers.