47namespace cli_error_codes {
63 std::vector<std::pair<std::string, std::string>>
overrides;
109 for (
int i = 1; i < argc; ++i) {
110 std::string arg = argv[i];
112 if (arg ==
"--help" || arg ==
"-h") {
114 }
else if (arg ==
"--version" || arg ==
"-v") {
118 }
else if (arg ==
"--config") {
122 "Missing value for --config",
129 if (kv_result.is_err()) {
132 result.
overrides.push_back(kv_result.value());
133 }
else if (arg ==
"--set") {
137 "Missing value for --set",
142 if (kv_result.is_err()) {
145 result.
overrides.push_back(kv_result.value());
149 "Unknown argument: " + arg,
155 "Unknown short argument: " + arg,
185 auto parse_result =
parse(argc, argv);
186 if (parse_result.is_err()) {
190 auto args = parse_result.value();
193 if (args.show_help || args.show_version) {
197 args.show_help ?
"help_requested" :
"version_requested",
205 if (!args.config_path.empty()) {
208 if (load_result.is_err()) {
211 config = load_result.value();
215 if (load_result.is_err()) {
218 config = load_result.value();
222 for (
const auto& [key, value] : args.overrides) {
224 if (result.is_err()) {
231 if (validation_result.is_err()) {
243 static void print_help(
const std::string& program_name =
"program") {
244 std::cout <<
"Usage: " << program_name <<
" [OPTIONS]\n\n"
246 <<
" --config=<path> Load configuration from YAML file\n"
247 <<
" --set <key>=<value> Override a configuration value\n"
248 <<
" --help, -h Show this help message\n"
249 <<
" --version, -v Show version information\n\n"
250 <<
"Configuration keys:\n";
254 for (
const auto& field : metadata) {
255 std::cout <<
" " << field.path;
256 if (!field.allowed_values.empty()) {
258 for (
size_t i = 0; i < field.allowed_values.size(); ++i) {
259 if (i > 0) std::cout <<
"|";
260 std::cout << field.allowed_values[i];
264 std::cout <<
"\n " << field.description;
265 if (!field.env_var.empty()) {
266 std::cout <<
" [env: " << field.env_var <<
"]";
271 std::cout <<
"\nExamples:\n"
272 <<
" " << program_name <<
" --config=config.yaml\n"
273 <<
" " << program_name <<
" --set logger.level=debug\n"
274 <<
" " << program_name <<
" --config=config.yaml --set thread.pool_size=16\n";
283 std::cout <<
"unified_system version " << version <<
"\n";
290 static bool starts_with(
const std::string& str,
const std::string& prefix) {
291 return str.size() >= prefix.size() &&
292 str.compare(0, prefix.size(), prefix) == 0;
299 auto pos = str.find(
'=');
300 if (pos == std::string::npos) {
303 "Invalid key=value format: " + str,
308 std::string key = str.substr(0, pos);
309 std::string value = str.substr(pos + 1);
312 key.erase(0, key.find_first_not_of(
" \t"));
313 key.erase(key.find_last_not_of(
" \t") + 1);
318 "Empty key in key=value pair",
324 std::make_pair(std::move(key), std::move(value)));
336 const std::string& key,
337 const std::string& value) {
339 if (key ==
"thread.pool_size") {
341 }
else if (key ==
"thread.queue_type") {
343 }
else if (key ==
"thread.max_queue_size") {
345 }
else if (key ==
"thread.thread_name_prefix") {
349 else if (key ==
"logger.level") {
351 }
else if (key ==
"logger.async") {
353 }
else if (key ==
"logger.buffer_size") {
355 }
else if (key ==
"logger.file_path") {
357 }
else if (key ==
"logger.max_file_size") {
359 }
else if (key ==
"logger.max_backup_files") {
361 }
else if (key ==
"logger.format_pattern") {
365 else if (key ==
"monitoring.enabled") {
367 }
else if (key ==
"monitoring.metrics_interval" ||
368 key ==
"monitoring.metrics_interval_ms") {
370 }
else if (key ==
"monitoring.health_check_interval" ||
371 key ==
"monitoring.health_check_interval_ms") {
373 }
else if (key ==
"monitoring.prometheus_port") {
375 }
else if (key ==
"monitoring.prometheus_path") {
379 else if (key ==
"monitoring.tracing.enabled") {
381 }
else if (key ==
"monitoring.tracing.sampling_rate") {
383 }
else if (key ==
"monitoring.tracing.exporter") {
385 }
else if (key ==
"monitoring.tracing.endpoint") {
389 else if (key ==
"database.backend") {
391 }
else if (key ==
"database.connection_string") {
393 }
else if (key ==
"database.log_queries") {
395 }
else if (key ==
"database.slow_query_threshold" ||
396 key ==
"database.slow_query_threshold_ms") {
398 }
else if (key ==
"database.pool.min_size") {
400 }
else if (key ==
"database.pool.max_size") {
402 }
else if (key ==
"database.pool.idle_timeout" ||
403 key ==
"database.pool.idle_timeout_ms") {
405 }
else if (key ==
"database.pool.acquire_timeout" ||
406 key ==
"database.pool.acquire_timeout_ms") {
410 else if (key ==
"network.compression") {
412 }
else if (key ==
"network.buffer_size") {
414 }
else if (key ==
"network.connect_timeout" ||
415 key ==
"network.connect_timeout_ms") {
417 }
else if (key ==
"network.io_timeout" ||
418 key ==
"network.io_timeout_ms") {
420 }
else if (key ==
"network.keepalive_interval" ||
421 key ==
"network.keepalive_interval_ms") {
423 }
else if (key ==
"network.max_connections") {
427 else if (key ==
"network.tls.enabled") {
429 }
else if (key ==
"network.tls.version") {
431 }
else if (key ==
"network.tls.cert_path") {
433 }
else if (key ==
"network.tls.key_path") {
435 }
else if (key ==
"network.tls.ca_path") {
437 }
else if (key ==
"network.tls.verify_peer") {
444 "Unknown configuration key: " + key,
456 return std::stoull(value);
464 return std::stoll(value);
472 return std::stod(value);
479 std::string lower = value;
480 std::transform(lower.begin(), lower.end(), lower.begin(),
481 [](
unsigned char c) { return std::tolower(c); });
482 return lower ==
"true" || lower ==
"1" || lower ==
"yes" || lower ==
"on";
Result type for error handling with member function support.
static Result< T > ok(U &&value)
Create a successful result with value (static factory)
Parses command-line arguments for configuration.
static bool starts_with(const std::string &str, const std::string &prefix)
Check if a string starts with a prefix.
static Result< parsed_args > parse(int argc, char **argv)
Parse command-line arguments.
static Result< unified_config > load_with_cli_overrides(int argc, char **argv)
Load configuration with CLI overrides.
static double parse_double(const std::string &value)
static void print_version(const std::string &version="0.1.0.0")
Print version information to stdout.
static size_t parse_size_t(const std::string &value)
static void print_help(const std::string &program_name="program")
Print help message to stdout.
static Result< std::pair< std::string, std::string > > parse_key_value(const std::string &str)
Parse a key=value string.
static VoidResult apply_override(unified_config &config, const std::string &key, const std::string &value)
Apply a configuration override.
static bool parse_bool(const std::string &value)
static int64_t parse_int64(const std::string &value)
static VoidResult validate(const unified_config &config)
Validate a configuration.
static Result< unified_config > load_from_env()
Load configuration from environment variables only.
static Result< unified_config > load(const std::string &path)
Load configuration from a YAML file.
YAML-based configuration loader for the unified system.
constexpr int missing_value
constexpr int invalid_format
constexpr int invalid_argument
constexpr int invalid_key
std::vector< field_metadata > get_config_metadata()
Get metadata for all configuration fields.
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
VoidResult ok()
Create a successful void result.
Umbrella header for Result<T> type and related utilities.
bool log_queries
Enable query logging.
std::string connection_string
Connection string or URI.
std::string backend
Database backend: "postgresql", "mysql", "sqlite", "mongodb", "redis".
pool_config pool
Connection pool configuration.
std::chrono::milliseconds slow_query_threshold
Slow query threshold.
bool async
Enable async logging.
size_t max_file_size
Maximum file size in bytes (for rotating_file)
std::string file_path
Log file path (for file writers)
size_t max_backup_files
Maximum number of backup files (for rotating_file)
size_t buffer_size
Async buffer size in bytes.
std::string format_pattern
Log format pattern.
std::string level
Log level: "trace", "debug", "info", "warn", "error", "critical", "off".
std::chrono::milliseconds metrics_interval
Metrics collection interval.
bool enabled
Enable monitoring.
uint16_t prometheus_port
Prometheus metrics port (0 to disable)
std::chrono::milliseconds health_check_interval
Health check interval.
std::string prometheus_path
Prometheus metrics path.
tracing_config tracing
Tracing configuration.
std::chrono::milliseconds connect_timeout
Connection timeout.
size_t max_connections
Maximum concurrent connections (server)
std::chrono::milliseconds io_timeout
Read/write timeout.
size_t buffer_size
Send/receive buffer size.
tls_config tls
TLS configuration.
std::string compression
Compression type: "none", "lz4", "gzip", "deflate", "zstd".
std::chrono::milliseconds keepalive_interval
Keep-alive interval.
Parsed command-line arguments.
bool show_version
Show version flag.
bool show_help
Show help flag.
std::vector< std::string > positional_args
Unparsed positional arguments.
std::string config_path
Configuration file path (from βconfig)
std::vector< std::pair< std::string, std::string > > overrides
Configuration overrides (from βset key=value)
size_t max_size
Maximum pool size.
size_t min_size
Minimum pool size.
std::chrono::milliseconds acquire_timeout
Connection acquisition timeout.
std::chrono::milliseconds idle_timeout
Idle connection timeout.
std::string queue_type
Queue type: "mutex", "lockfree", "bounded".
size_t pool_size
Number of worker threads (default: hardware concurrency)
size_t max_queue_size
Maximum queue size (for bounded queue)
std::string thread_name_prefix
Thread naming prefix.
std::string version
TLS version: "1.2", "1.3".
std::string key_path
Private key file path.
bool verify_peer
Verify peer certificate.
std::string cert_path
Certificate file path.
std::string ca_path
CA certificate path (for client verification)
std::string endpoint
Exporter endpoint.
std::string exporter
Exporter type: "otlp", "jaeger", "zipkin", "console".
bool enabled
Enable tracing.
double sampling_rate
Sampling rate (0.0 to 1.0)
Root configuration structure for the unified system.
network_config network
Network system configuration.
logger_config logger
Logger system configuration.
thread_config thread
Thread system configuration.
monitoring_config monitoring
Monitoring system configuration.
database_config database
Database system configuration.
Unified configuration schema for the entire system.