【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#中,在类型继承时,由于构造子类必须先构造其父类型的内容,因此,必须子类型的构造函数中调用父类型的构造函数(无参数的不需要显式声明). 但是往往我们会出现,子类型本身的构造函数大于 ...
随机推荐
- 华企盾DSC服务器无法启动常见处理方法
先查看<服务问题判断>文档.常见的 1.授权已经过期--需延长授权 2.ERR_BASE64 – 机器码变更 3.不能在该计算机上使用该数据库,需要解锁才可以--打开服务器配置解锁数据库 ...
- 字符集(Character Set)和编码(Encoding)的历史演化
字符集(Character Set)和编码(Encoding)是两个相关但不同的概念,它们在计算机和信息技术领域用于处理文本数据. 字符集(Character Set): 字符集是一种定义了一组字符. ...
- c++ 期末编程题
1. 计算两点之间的距离 #include <iostream> #include <cmath> using namespace std; int main() { int ...
- 2023“强网杯”部分WP
强网先锋 SpeedUp 题目 我的解答: 分析代码可知是求2的27次方的阶乘的每一位的和. 使用在线网址直接查看:https://oeis.org/A244060/list 然后sha256加密 f ...
- 在xml中比较运算符
SQL 中,可以使用比较运算符来比较两个值,如使用小于运算符 < 比较两个值大小.但是,在 SQL 查询中,有时候需要将小于运算符 < 用于 XML 或 HTML 语法中,这会导致语法冲突 ...
- LeetCode141环形链表I、II
141. 环形链表 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. ...
- 理论+实践,揭秘昇腾CANN算子开发
摘要: 本文介绍了CANN自定义算子开发的几种开发方式和算子的编译运行流程.然后以开发一个DSL Add算子为例,讲解算子开发的基本流程. 本文分享自华为云社区<昇腾CANN算子开发揭秘> ...
- 云小课|云小课带你玩转可视化分析ELB日志
阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 云日志服务支持可视化查看 ...
- HIVE报错 need to specify partition columns because the destination table is partitioned
解决 分区需要指定分区 insert into table XXX partition(分区='')
- 火山引擎DataTester:跨境电商网站,如何快速实施AB测试 ?
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 如今中企出海的形态愈加多样,很多企业都建有独立站可直接触达海外客户,但获取优质流量的成本会更加高昂.当优质流量 ...