|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
命令行参数解析器 更多...
#include <cmdline.hpp>
类 | |
| struct | option |
| 选项定义结构 更多... | |
| struct | option_dependency |
| 选项依赖关系 更多... | |
| struct | option_conflict |
| 选项冲突关系 更多... | |
Public 类型 | |
| enum class | conflict_behavior { error , warning , ignore } |
| 约束冲突处理行为 更多... | |
| using | validator = function<string(const string& name, const string& value)> |
| 选项值验证器类型 | |
Public 成员函数 | |
| void | add_option (const string &long_name, char short_name, const string &description, bool requires_value=false, bool allow_multiple=false, const string &default_value="") |
| 添加选项定义 | |
| void | parse_os_args () |
| 解析操作系统提供的命令行参数 | |
| void | parse (int argc, const char *argv[]) |
| 解析main函数参数 | |
| void | parse (const vector< string > &args) |
| 解析字符串向量参数 | |
| string | get (const string &long_name, size_t index=0) const |
| 获取选项值 | |
| bool | has (const string &name) const |
| 检查选项是否存在 | |
| size_t | count (const string &name) const |
| 获取选项出现的次数 | |
| const vector< string > & | positional_args () const noexcept |
| 获取位置参数 | |
| const string & | program_name () const noexcept |
| 获取程序名称 | |
| void | print_help () const |
| 打印帮助信息 | |
| void | add_dependency (const string &present, const string &required) |
| 添加选项依赖关系 | |
| void | add_conflict (const string &option_a, const string &option_b) |
| 添加选项冲突关系 | |
| void | set_conflict_behavior (conflict_behavior behavior) noexcept |
| 设置冲突处理行为 | |
| void | load_config (const string &config_path, const string §ion="") |
| 从配置文件加载选项值 | |
| void | add_validator (validator v) |
| 添加选项验证器 | |
| void | add_range_validator (string long_name, int64_t min_val, int64_t max_val) |
| 添加选项值范围验证器(数值范围) | |
| void | add_regex_validator (string long_name, const string &pattern) |
| 添加选项值正则验证器 | |
静态 Public 成员函数 | |
| static vector< string > | get_os_argv () |
| 获取操作系统提供的命令行参数 | |
命令行参数解析器
支持解析符合POSIX风格和GNU风格扩展的命令行参数。 特性:
在文件 cmdline.hpp 第 161 行定义.
| using neforce::cmdline::validator = function<string(const string& name, const string& value)> |
|
strong |
添加选项冲突关系
| option_a | 选项A |
| option_b | 选项B |
添加选项依赖关系
| present | 当此选项出现时 |
| required | 此选项也必须出现 |
| void neforce::cmdline::add_option | ( | const string & | long_name, |
| char | short_name, | ||
| const string & | description, | ||
| bool | requires_value = false, | ||
| bool | allow_multiple = false, | ||
| const string & | default_value = "" ) |
添加选项定义
| long_name | 长选项名称 |
| short_name | 短选项字符 |
| description | 选项描述 |
| requires_value | 是否需要值 |
| allow_multiple | 是否允许多次出现 |
| default_value | 默认值 |
| cmdline_exception | 选项名重复时抛出 |
| void neforce::cmdline::add_validator | ( | validator | v | ) |
获取选项出现的次数
| name | 长选项名称 |
|
nodiscard |
检查选项是否存在
| name | 长选项名称 |
从配置文件加载选项值
| config_path | 配置文件路径 |
| section | 配置节名或嵌套键名 |
| cmdline_exception | 文件不存在或解析失败时抛出 |
支持的文件格式:
配置文件中的值仅填充命令行未指定的选项,命令行参数优先级更高。
| void neforce::cmdline::parse | ( | int | argc, |
| const char * | argv[] ) |
| void neforce::cmdline::parse_os_args | ( | ) |
解析操作系统提供的命令行参数
| cmdline_exception | 解析失败时抛出 |
| void neforce::cmdline::print_help | ( | ) | const |
打印帮助信息
根据所有注册的选项自动生成格式化的帮助文本。
|
inlinenodiscardnoexcept |
|
inlinenoexcept |