项目地址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】一个开源的入参解析库的更多相关文章

  1. postman 上一个接口的返回值作为下一个接口的入参

    在使用postman做接口测试的时候,在多个接口的测试中,如果需要上一个接口的返回值作为下一个接口的入参,其基本思路是: 1.获取上一个接口的返回值 2.将返回值设置成环境变量或者全局变量 3.设置下 ...

  2. MyBatis版本升级导致OffsetDateTime入参解析异常问题复盘

    背景 最近有一个数据统计服务需要升级SpringBoot的版本,由1.5.x.RELEASE直接升级到2.3.0.RELEASE,考虑到没有用到SpringBoot的内建SPI,升级过程算是顺利.但是 ...

  3. Google开源命令行参数解析库gflags

    Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...

  4. JMeter使用JSON Extractor插件实现将一个接口的JSON返回值作为下一个接口的入参

    ##补充## 接口响应数据,一般为JSON,HTML格式的数据. 对于HTML的响应结果提取,可以使用正则表达式,也可以通过XPath来提取:对于JSON格式的数据,可以用正则表达式,JSON Ext ...

  5. 一个非常棒的Go-Json解析库

    json是一种数据格式,经常被用作数据交换,页面展示,序列化等场景,基本每种语言都有对应的json解析框架,Go语言也不例外,并且内置了json库,基本能够满足一些普通开发场景,但有些复杂场景下就不太 ...

  6. FunDA:一个开源的函数式数据处理工具库,也是Slick的补充

    如果你是一个Slick用户,或者你是一个数据库编程人员正在尝试进入函数式编程模式,那么FunDA可能会帮到你. 目前市面上FRM(Functional Relational Mapper),即函数式的 ...

  7. 介绍一个开源的SIP(VOIP)协议库PJSIP

    本文系转载,出处不可考. 假设你对SIP/VoIP技术感兴趣,哪希望你不要错过:),假设你对写出堪称优美的Code感兴趣 ,那么你也不可错过:) 这期间我想分析一下一个实际的协议栈的设计到实现的相关技 ...

  8. 一个简单的 JSON 生成/解析库

    这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...

  9. OC封装的TLV数据格式解析库

    作者:朱克锋 邮箱:zhukefeng@iboxpay.com 转载请注明出处:http://blog.csdn.net/linux_zkf TLV是一种可变格式,意思就是: Type类型, Leng ...

  10. C#构造函数在继承时必须要求与父类型构造函数入参相同怎么办?

    摘要 我们都知道,C#中,在类型继承时,由于构造子类必须先构造其父类型的内容,因此,必须子类型的构造函数中调用父类型的构造函数(无参数的不需要显式声明). 但是往往我们会出现,子类型本身的构造函数大于 ...

随机推荐

  1. Python+Selenium4自动化之JS属性

    应用场景 在自动化中, 能对JS代码进行增.删.改的话,可以帮助我们解决很多问题, 如:修改<a>标签的target属性,让它不打开新的窗口(_blank),从而不用频繁使用switch_ ...

  2. MinIO客户端之alias

    MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc alias mc alias list mc alias remove mc alias set mc al ...

  3. Baidu Comate实践指南,惊艳了我...

    1 啥是Baidu Comate Comate是百度开发的编程大模型工具,它基于文心大模型,结合百度积累多年的编程现场大数据和外部优秀开源数据,为我们生成更符合实际研发场景的优质代码:它能提升编码效率 ...

  4. hystrix的熔断降级

    hystrix的熔断降级 结合Feign使用 1.A服务通过B服务的唯-标识,从Nacos获取到可调用列表. 2.使用feigh中的Http发起远程请求. 3.超过默认配置的时限,抛出异常,结束该线程 ...

  5. 神经网络入门篇:直观理解反向传播(Backpropagation intuition)

    详细推导反向传播 下图是逻辑回归的推导: 回想一下逻辑回归的公式(参考公式1.2.公式1.5.公式1.6.公式1.15) 公式1.38: \[\left. \begin{array}{l} {x }\ ...

  6. 探索Reactor网络模型在当今应用领域的革新

    本文分享自华为云社区<驾驭网络技术的未来:探索Reactor网络模型在当今应用领域的革新>,作者: Lion Long . 本文介绍了Linux网络设计中的Reactor网络模型及其在实际 ...

  7. KubeEdge@MEC:Kubernetes容器生态与5G的结合

    摘要:边缘计算技术快速发展,5G MEC边云协同成为最新的发展趋势. 前言 边缘计算技术快速发展,5G MEC进入商业部署快车道,边云协同成为MEC的普遍诉求,KubeEdge社区洞悉这一趋势,按照C ...

  8. A/B实验背后的秘密:样本量计算

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 一.前言 背景: AB实验具有一定前瞻性,统计性,科学性的特性.用好了就实现了在大数据时代的充分利用数据分析问题, ...

  9. PPT 毕业答辩PPT应该怎么样改

    PPT 毕业答辩PPT应该怎么样改

  10. Axure 页面交互