feat(argparse): 新增命令行参数解析库
新增 argparse 库的基础框架,包含以下组件: - 创建了 argparse 库的 cbuild.toml 配置文件,定义包信息和依赖关系 - 实现了核心的参数解析功能,包括 optparse.h 和 optparse.c - 定义了参数解析相关的数据结构和枚举类型 - 实现了完整的命令行选项解析逻辑,支持长短选项、参数绑定等功能 - 添加了全面的单元测试,覆盖各种使用场景和边界情况 - 包含对短选项连写、长选项等号形式、选项终止符等特性的支持
This commit is contained in:
31
libs/argparse/include/argparse.h
Normal file
31
libs/argparse/include/argparse.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __SCC_ARGPARSE_H__
|
||||
#define __SCC_ARGPARSE_H__
|
||||
|
||||
#include "optparse.h"
|
||||
|
||||
typedef enum {
|
||||
SCC_ARGPARSE_ERROR_NONE,
|
||||
} scc_argparse_error_t;
|
||||
|
||||
typedef struct {
|
||||
} scc_argparse_cmd_t;
|
||||
|
||||
typedef struct {
|
||||
} scc_argparse_arg_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *description;
|
||||
const char *epilog;
|
||||
const char *usage;
|
||||
const char *help;
|
||||
scc_optparse_t optparse;
|
||||
int usage_margin;
|
||||
int usage_max_width;
|
||||
int help_vertical_space;
|
||||
int help_usage_margin;
|
||||
int help_description_margin;
|
||||
int help_max_width;
|
||||
} scc_argparse_t;
|
||||
|
||||
#endif /* __SCC_ARGPARSE_H__ */
|
||||
45
libs/argparse/include/optparse.h
Normal file
45
libs/argparse/include/optparse.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef __SCC_OPTPARSER_H__
|
||||
#define __SCC_OPTPARSER_H__
|
||||
|
||||
typedef struct scc_optparse_opt {
|
||||
const char prefix;
|
||||
const char short_name;
|
||||
const char *long_name;
|
||||
int min_args;
|
||||
int max_args;
|
||||
int parsed_args;
|
||||
const char *default_value;
|
||||
void (*invoke)(void *value);
|
||||
} scc_optparse_opt_t;
|
||||
|
||||
typedef enum scc_optparse_error {
|
||||
SCC_OPT_ERROR_NONE,
|
||||
SCC_OPT_ERROR_NOT_FOUND_LONG_ARG,
|
||||
SCC_OPT_ERROR_NOT_FOUND_SHORT_ARG,
|
||||
SCC_OPT_ERROR_NOT_ENOUGH_ARGS,
|
||||
SCC_OPT_ERROR_TOO_MANY_ARGS,
|
||||
} scc_optparse_error_t;
|
||||
|
||||
typedef struct scc_optparse_result {
|
||||
scc_optparse_opt_t *opt;
|
||||
const char *value;
|
||||
int error;
|
||||
} scc_optparse_result_t;
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
const char **argv;
|
||||
scc_optparse_opt_t *opts;
|
||||
scc_optparse_opt_t *prev;
|
||||
int handle_positional;
|
||||
int current_arg_pos;
|
||||
int short_opt_pos;
|
||||
} scc_optparse_t;
|
||||
|
||||
void scc_optparse_init(scc_optparse_t *parser, int argc, const char **argv);
|
||||
void scc_optparse_drop(scc_optparse_t *parser);
|
||||
void scc_optparse_set(scc_optparse_t *parser, scc_optparse_opt_t *opts);
|
||||
void scc_optparse_reset(scc_optparse_t *parser);
|
||||
void scc_optparse_parse(scc_optparse_t *parser, scc_optparse_result_t *res);
|
||||
|
||||
#endif /* __SCC_OPTPARSER_H__ */
|
||||
Reference in New Issue
Block a user