40#if __has_include(<source_location>)
48#if __has_include(<ranges>)
67 std::vector<std::string>,
68 std::map<std::string, std::string>
105 virtual std::optional<config_value>
get(
const std::string& key)
const = 0;
110 virtual bool has(
const std::string& key)
const = 0;
143 std::optional<std::string>
get_option(
const std::string& name)
const {
145 if (it !=
options.end())
return it->second;
171 for (
int i = 1; i < argc; ++i) {
172 std::string arg = argv[i];
174 if (arg.starts_with(
"--")) {
175 std::string name = arg.substr(2);
176 auto eq_pos = name.find(
'=');
177 if (eq_pos != std::string::npos) {
178 result.
options[name.substr(0, eq_pos)] = name.substr(eq_pos + 1);
179 }
else if (i + 1 < argc && argv[i + 1][0] !=
'-') {
180 result.
options[name] = argv[++i];
184 }
else if (arg.starts_with(
"-") && arg.size() == 2) {
185 std::string short_name = arg.substr(1);
187 if (opt.short_name == short_name) {
188 if (opt.takes_value && i + 1 < argc) {
189 result.
options[opt.name] = argv[++i];
191 result.
options[opt.name] =
"true";
203 if (opt.required && !result.
has_option(opt.name)) {
204 result.
errors.push_back(
"Missing required option: " + opt.name);
Simple command-line argument parser.
void add_option(const cli_option &opt)
Add an option definition.
std::vector< cli_option > options_
cli_parse_result parse(int argc, const char *const *argv) const
Parse command-line arguments.
CliConfigParser()=default
Interface for configuration loaders.
virtual bool has(const std::string &key) const =0
Check if a key exists.
virtual bool load()=0
Load configuration from source.
virtual ~IConfigLoader()=default
virtual std::optional< config_value > get(const std::string &key) const =0
Get a configuration value.
std::variant< std::string, int64_t, double, bool, std::vector< std::string >, std::map< std::string, std::string > > config_value
Configuration value type.
Definition for a CLI option.
std::optional< std::string > default_value
bool has_option(const std::string &name) const
std::map< std::string, std::string > options
std::optional< std::string > get_option(const std::string &name) const
std::vector< std::string > positional_args
std::vector< std::string > errors
A single configuration entry.
std::optional< std::string > description
config_entry(std::string k, config_value v, std::string desc="", bool req=false)
Compile-time feature detection flags.
static constexpr bool has_source_location
static constexpr bool has_ranges
static constexpr bool has_concepts