【ArgParse】一个开源的入参解析库
项目地址:argtable3

本地验证:
编译构建

新增验证
// examples/skull.c
#include "argtable3.h"
int main(int argc, char **argv)
{
const char* progname = "skull";
struct arg_lit *help = arg_lit0("h", "help", "this is help info");
struct arg_lit *litn = arg_litn("l", "literal", 0, 5, "./progname -l --literal");
struct arg_int *intn = arg_intn("i", "integer", "<int>", 0, 5, "./program -i 10 -i 20 -i 30");
struct arg_dbl *dbln = arg_dbln("d", "double", "<double>", 0, 5, "./program -d 3.14 -d 2.718 -d 1.414");
struct arg_str *strn = arg_strn("s", "string", "<string>", 0, 5, "./program -s hello -s world");
struct arg_rex *rexn = arg_rexn("r", "regular", "^[A-Za-z0-9]+$", "<regex>", 0, 5, ARG_REX_ICASE, "./program -r [digit] -r [letters]");
struct arg_file *filen = arg_filen("f", "file", "<file>", 0, 5, "./program file1.txt file2.txt file3.txt");
struct arg_date *daten = arg_daten("a", "date", "%Y-%m-%d", "<date>", 0, 5, "./program -a 2023-09-01 -a 2023-09-02 -a 2023-09-03");
struct arg_end *end = arg_end(20);
void* argtable[] = {help, litn, intn, dbln, strn, rexn, filen, daten, end};
int nerrors;
int exitcode=0;
/* verify the argtable[] entries were allocated sucessfully */
if (arg_nullcheck(argtable) != 0) {
/* NULL entries were detected, some allocations must have failed */
printf("%s: insufficient memory\n", progname);
exitcode=1;
goto exit;
}
/* Parse the command line as defined by argtable[] */
nerrors = arg_parse(argc, argv, argtable);
/* If the parser returned any errors then display them and exit */
if (nerrors > 0) {
/* Display the error details contained in the arg_end struct.*/
arg_print_errors(stdout,end,progname);
printf("Try '%s --help' for more information.\n", progname);
exitcode=1;
goto exit;
}
/* special case: '--help' takes precedence over error reporting */
if (help->count > 0) {
printf("Usage: %s", progname);
arg_print_syntax(stdout, argtable, "\n");
printf("Print certain system information. With no options, same as -s.\n\n");
arg_print_glossary(stdout, argtable," %-25s %s\n");
printf("\nReport bugs to <foo@bar>.\n");
exitcode=0;
goto exit;
}
/* special case: '--version' takes precedence error reporting */
if (litn->count > 0) {
printf("'%s' count: %d.\n", litn->hdr.glossary, litn->count);
exitcode=0;
goto exit;
}
if (intn->count > 0) {
printf("'%s' count: %d.\n", intn->hdr.glossary, intn->count);
for (int i = 0; i < intn->count; i++) {
printf("Integer %d: %d\n", i+1, intn->ival[i]);
}
exitcode=0;
goto exit;
}
if (dbln->count > 0) {
printf("'%s' count: %d.\n", dbln->hdr.glossary, dbln->count);
for (int i = 0; i < dbln->count; i++) {
printf("Double %d: %lf\n", i+1, dbln->dval[i]);
}
exitcode=0;
goto exit;
}
if (strn->count > 0) {
printf("'%s' count: %d.\n", strn->hdr.glossary, strn->count);
for (int i = 0; i < strn->count; i++) {
printf("String %d: %s\n", i+1, strn->sval[i]);
}
exitcode=0;
goto exit;
}
if (rexn->count > 0) {
printf("'%s' count: %d.\n", rexn->hdr.glossary, rexn->count);
for (int i = 0; i < rexn->count; i++) {
printf("Regular %d: %s\n", i+1, rexn->sval[i]);
}
exitcode=0;
goto exit;
}
if (filen->count > 0) {
printf("'%s' count: %d.\n", filen->hdr.glossary, filen->count);
for (int i = 0; i < filen->count; i++) {
printf("File %d: %s\n", i+1, filen->filename[i]);
}
exitcode=0;
goto exit;
}
if (daten->count > 0) {
printf("'%s' count: %d.\n", daten->hdr.glossary, daten->count);
for (int i = 0; i < daten->count; i++) {
printf("Date %d: %04d-%02d-%02d\n", i+1, daten->tmval[i].tm_year, daten->tmval[i].tm_mon, daten->tmval[i].tm_mday);
}
exitcode=0;
goto exit;
}
exit:
/* deallocate each non-null entry in argtable[] */
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return exitcode;
}
源码修改
实测月份少1,年份是基于1900
// src/arg_date.c
char* arg_strptime(const char* buf, const char* fmt, struct tm* tm) {
while ((c = *fmt) != '\0') {
/* Clear `alternate' modifier prior to new conversion. */
alt_format = 0;
/* Eat up white-space. */
if (isspace(c)) {
while (isspace((int)(*bp)))
bp++;
fmt++;
continue;
}
if ((c = *fmt++) != '%')
goto literal;
again:
switch (c = *fmt++) {
case 'm': /* The month. */
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &i, 1, 12)))
return (0);
/* tm->tm_mon = i- 1; @skull #2023-9-14 10:18:34 */
tm->tm_mon = i;
break;
case 'Y': /* The year. */
LEGAL_ALT(ALT_E);
if (!(conv_num(&bp, &i, 0, 9999)))
return (0);
/* tm->tm_year = i - TM_YEAR_BASE; @skull #2023-9-14 10:19:59 */
tm->tm_year = i;
break;
执行



【ArgParse】一个开源的入参解析库的更多相关文章
- postman 上一个接口的返回值作为下一个接口的入参
在使用postman做接口测试的时候,在多个接口的测试中,如果需要上一个接口的返回值作为下一个接口的入参,其基本思路是: 1.获取上一个接口的返回值 2.将返回值设置成环境变量或者全局变量 3.设置下 ...
- MyBatis版本升级导致OffsetDateTime入参解析异常问题复盘
背景 最近有一个数据统计服务需要升级SpringBoot的版本,由1.5.x.RELEASE直接升级到2.3.0.RELEASE,考虑到没有用到SpringBoot的内建SPI,升级过程算是顺利.但是 ...
- Google开源命令行参数解析库gflags
Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...
- JMeter使用JSON Extractor插件实现将一个接口的JSON返回值作为下一个接口的入参
##补充## 接口响应数据,一般为JSON,HTML格式的数据. 对于HTML的响应结果提取,可以使用正则表达式,也可以通过XPath来提取:对于JSON格式的数据,可以用正则表达式,JSON Ext ...
- 一个非常棒的Go-Json解析库
json是一种数据格式,经常被用作数据交换,页面展示,序列化等场景,基本每种语言都有对应的json解析框架,Go语言也不例外,并且内置了json库,基本能够满足一些普通开发场景,但有些复杂场景下就不太 ...
- FunDA:一个开源的函数式数据处理工具库,也是Slick的补充
如果你是一个Slick用户,或者你是一个数据库编程人员正在尝试进入函数式编程模式,那么FunDA可能会帮到你. 目前市面上FRM(Functional Relational Mapper),即函数式的 ...
- 介绍一个开源的SIP(VOIP)协议库PJSIP
本文系转载,出处不可考. 假设你对SIP/VoIP技术感兴趣,哪希望你不要错过:),假设你对写出堪称优美的Code感兴趣 ,那么你也不可错过:) 这期间我想分析一下一个实际的协议栈的设计到实现的相关技 ...
- 一个简单的 JSON 生成/解析库
这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...
- OC封装的TLV数据格式解析库
作者:朱克锋 邮箱:zhukefeng@iboxpay.com 转载请注明出处:http://blog.csdn.net/linux_zkf TLV是一种可变格式,意思就是: Type类型, Leng ...
- C#构造函数在继承时必须要求与父类型构造函数入参相同怎么办?
摘要 我们都知道,C#中,在类型继承时,由于构造子类必须先构造其父类型的内容,因此,必须子类型的构造函数中调用父类型的构造函数(无参数的不需要显式声明). 但是往往我们会出现,子类型本身的构造函数大于 ...
随机推荐
- python操作mongodb基本使用
使用pymongo,具体可以参考官方文档: 语法上基本和原生mongodb是一样的,所以非常容易入手... https://pymongo.readthedocs.io/en/stable/tutor ...
- 【内核】kernel 热升级-1:kexec 机制
内核热升级是指,预先准备好需要升级的内核镜像文件,在秒级时间内,完成内核切换,追求用户服务进程无感知. 欧拉操作系统提供了一套比较成熟的解决方案,该解决方案提供了用户态程序和内核态程序两部分: kex ...
- npm 发布流程
登录npm 查看本地是否登录 # 全局配置源 npm who am i # 官方源 npm who am i --registry https://registry.npmjs.org 注: npm源 ...
- 在arm架构的银河麒麟系统部署Redis
以下是在arm架构的银河麒麟系统上部署Redis的详细步骤: 1. 创建文件夹 首先,在合适的位置创建必要的文件夹.在本例中,我们将创建/opt/redis和/usr/src/redis两个文件夹. ...
- python tkinter使用(五)
python tkinter使用(五) 本篇文章讲述tkinter 中treeview的使用 Treeview是一个多列列表框,可以显示层次数据. #!/usr/bin/python3 # -*- c ...
- ASR项目实战-数据
使用机器学习方法来训练模型,使用训练得到的模型来预测语音数据,进而得到识别的结果文本,这是实现语音识别产品的一般思路. 本文着重介绍通用语音识别产品对于数据的诉求. 对数据的要求 训练集 相关要求,如 ...
- ElasticSearch之cat health API
命令样例如下: curl -X GET "https://localhost:9200/_cat/health?v=true&pretty" --cacert $ES_HO ...
- Shell的概念、shebang、bash的概念
什么是shell shell的作用是 解释执行用户输入的命令或程序等 用户输入一条命令,shell就解释一条 键盘输入命令,Linux给与响应的方式,称之为交互式 linux的计算机是如何跟用户进行交 ...
- CSS3学习笔记-句子排版效果
CSS3提供了丰富的排版效果,可以通过样式属性来控制文本的排列方式.字体样式.行高.字间距等.以下是一些常用的句子排版效果示例: 文本对齐方式: .text-center { text-align: ...
- 在ubuntu下将virtualbox虚拟机的磁盘重设大小的方法
1.VBoxManage modifyhd /home/beyond/xxx.vdi --resize 20480 {20480(单位:M)是你要扩容之后的总大小,/home/beyond 是你存放 ...