UP Task,Argpase
This commit is contained in:
10
demo/arg/t_arg.h
Normal file
10
demo/arg/t_arg.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by lydxh on 2024/5/10.
|
||||
//
|
||||
|
||||
#ifndef HW_LIB_T_ARG_H
|
||||
#define HW_LIB_T_ARG_H
|
||||
|
||||
void Test_argpase();
|
||||
|
||||
#endif //HW_LIB_T_ARG_H
|
49
demo/arg/test.c
Normal file
49
demo/arg/test.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "stdio.h"
|
||||
#include "argpase.h"
|
||||
|
||||
enum Opt {
|
||||
T0 = 0, T1, T2, T3, T4, T5
|
||||
};
|
||||
Option Opts[6] = {{"T0", T0},
|
||||
{"T1", T1},
|
||||
{"T2", T2},
|
||||
{"T3", T3},
|
||||
{"T4", T4},
|
||||
{"T5", T5}};
|
||||
|
||||
void Test_argpase() {
|
||||
OptList *Head = Options_Creat("Head", -1);
|
||||
OptList *t;
|
||||
int i;
|
||||
for (i = 0; i < 6; i++) {
|
||||
t = Options_CreatOpt(&Opts[i]);
|
||||
Options_Add(Head, t);
|
||||
}
|
||||
Options_Print(Head);
|
||||
|
||||
char *s = "T1 52 T2 64 T5 72";//模拟串口参数传入
|
||||
char *argv[20];
|
||||
size_t argc;
|
||||
|
||||
argc = Str_To_Args(s, argv);
|
||||
printf("\nargs\n");
|
||||
for (i = 0; i < argc; ++i) {
|
||||
printf("arg[%d]:%s\n", i, argv[i]);//打印参数拆分情况
|
||||
}
|
||||
|
||||
OptId optid;
|
||||
while ((optid = Options_Load(Head, argv, argc)) != -1) {//开始匹配 Optarg为匹配的参数(字符类型可atoi等转换)
|
||||
switch (optid) {
|
||||
case T0:
|
||||
case T1:
|
||||
case T2:
|
||||
case T3:
|
||||
case T4:
|
||||
case T5:
|
||||
printf("Opt_%s[%d]:%s\n", Optstr, optid, Optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user