Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
cli_parser_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14
16
17#include <iostream>
18#include <string>
19#include <vector>
20
21using namespace kcenon::common;
22using namespace kcenon::common::config;
23
24int main(int argc, char* argv[])
25{
26 std::cout << "=== CLI Config Parser Example ===\n\n";
27
28 // If no arguments provided, demonstrate with simulated args
29 if (argc <= 1)
30 {
31 std::cout << "1. No arguments provided. Demonstrating with simulated args...\n\n";
32
33 // Simulate: program --config app.yaml --set thread.pool_size=8 --set logger.level=debug
34 std::vector<const char*> sim_args
35 = {"cli_parser_example", "--config",
36 "app.yaml", "--set",
37 "thread.pool_size=8", "--set",
38 "logger.level=debug", "positional_arg"};
39
40 auto result
41 = cli_config_parser::parse(static_cast<int>(sim_args.size()), const_cast<char**>(sim_args.data()));
42
43 if (result.is_err())
44 {
45 std::cerr << "Parse error: " << result.error().message << "\n";
46 return 1;
47 }
48
49 auto args = result.value();
50
51 std::cout << " Parsed results:\n";
52 std::cout << " Config path: " << (args.config_path.empty() ? "(none)" : args.config_path)
53 << "\n";
54 std::cout << " Show help: " << (args.show_help ? "true" : "false") << "\n";
55 std::cout << " Show version: " << (args.show_version ? "true" : "false") << "\n";
56
57 std::cout << "\n Overrides (" << args.overrides.size() << "):\n";
58 for (const auto& [key, value] : args.overrides)
59 {
60 std::cout << " " << key << " = " << value << "\n";
61 }
62
63 std::cout << "\n Positional args (" << args.positional_args.size() << "):\n";
64 for (const auto& arg : args.positional_args)
65 {
66 std::cout << " " << arg << "\n";
67 }
68 }
69 else
70 {
71 // Parse actual command-line arguments
72 std::cout << "1. Parsing actual command-line arguments...\n\n";
73
74 auto result = cli_config_parser::parse(argc, argv);
75 if (result.is_err())
76 {
77 std::cerr << "Parse error: " << result.error().message << "\n";
79 return 1;
80 }
81
82 auto args = result.value();
83
84 if (args.show_help)
85 {
87 return 0;
88 }
89
90 if (args.show_version)
91 {
93 return 0;
94 }
95
96 std::cout << " Config path: " << (args.config_path.empty() ? "(none)" : args.config_path)
97 << "\n";
98 std::cout << " Overrides: " << args.overrides.size() << "\n";
99 for (const auto& [key, value] : args.overrides)
100 {
101 std::cout << " " << key << " = " << value << "\n";
102 }
103 }
104
105 // Show help output format
106 std::cout << "\n2. Help output:\n";
107 cli_config_parser::print_help("my_application");
108
109 std::cout << "\nDone.\n";
110 return 0;
111}
int main()
static Result< parsed_args > parse(int argc, char **argv)
Parse command-line arguments.
static void print_version(const std::string &version="0.1.0.0")
Print version information to stdout.
static void print_help(const std::string &program_name="program")
Print help message to stdout.
Command-line interface configuration parser.
Core interfaces.
Definition adapter.h:21