ParamterParsing.c
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include "JZsdkLib.h"
#include "./ParamterParsing.h"
/*
struct option {
const char *name; 参数名称
int has_arg; 指明是否带有参数
int *flag; flag=NULL时,返回value;不为空时,*flag=val,返回0
int val; 用于指定函数找到选项的返回值或flag非空时指定*flag的值
};
no_argument 不带参数 如: --version,--help
required_argument 必须带参数 --参数 值 或者 --参数=值。如:--dir=/root
optional_argument 参数可选 只能为:--参数=值
char *optstring="ab:c::";
运行程序可以给最多三个参数,分别为-a,-b和-c,其中
a选项 :因为没有跟冒号,不能有参数,如:-a
b选项 :因为后面跟了1个冒号,必须有参数,如:-b 100
c选项 :因为跟了2个冒号,参数可有可无,如:-c 100 或 -c 都行
*/
typedef enum parameter{
PARAMETER_OUT_TIMEFLAG = 0x1010, //备用超时标志位
PARAMETER_OUT_TIMEOUT = 0x1011, //备用超时时间
PARAMETER_PLAY_PROMPT_TTS = 0x1012, //播放提示音
}parameter;
static const struct option storage_opts[] = {
{ "OutFlag", required_argument, 0, PARAMETER_OUT_TIMEFLAG }, //超时标志
{ "OutTime", required_argument, 0, PARAMETER_OUT_TIMEOUT }, //超时时间
{ "play_prompt_tts", required_argument, NULL, PARAMETER_PLAY_PROMPT_TTS }, //提示选项
{ 0, 0, 0, 0 }
};
static int Timeout_flag = JZ_FLAGCODE_OFF; //超时标记位,默认关闭
static int Timeout_time = 60; //超时时间
unsigned char *g_JZsdkParam_PlayPromptTTS = NULL; //播放提示音
int JZsdk_ParamterParsing_GetTimeOutFlag()
{
return Timeout_flag;
}
int JZsdk_ParamterParsing_GetTimeOutTime()
{
return Timeout_time;
}
/*************************************
*
* * 程序参数解析
* * 输入:参数字符串
* 输出:模式
* 目前 模式0 为不开启任何单独功能,即psdk或者串口模式
* 模式1 为开起喊话器播放模式
*
* ********************************/
T_JZsdkReturnCode JZsdk_ParamterParsing(int argc, char *argv[], unsigned int *mode)
{
JZSDK_LOG_INFO("参数解析");
int insCode = 0; //指令码
//获取短选项
static char *opt_storage = "t:f:d:b";
//解析参数
while (1)
{
int opt_index = 0;
insCode = getopt_long(argc, argv, opt_storage, storage_opts, &opt_index);
if (insCode == -1)
break;
switch (insCode)
{
case PARAMETER_OUT_TIMEFLAG:
printf("备用:outtimeflag:%d \n", atoi(optarg));
Timeout_flag = atoi(optarg);
break;
case PARAMETER_OUT_TIMEOUT:
printf("备用:time %d \n", atoi(optarg));
Timeout_time = atoi(optarg);
break;
//提示音选项
case PARAMETER_PLAY_PROMPT_TTS:
printf("prompt_option:%s \n", optarg);
g_JZsdkParam_PlayPromptTTS = optarg;
*mode = 1;
break;
case 'f': //超时标记位
Timeout_flag = atoi(optarg);
break;
case 't': //超时时间
Timeout_time = atoi(optarg);
break;
case '?':
// 如果getopt遇到一个它不认识的选项,它会打印一个消息到stderr
// 并返回'?'。然后我们可以选择退出或给出更多帮助。
if (isprint(optopt))
JZSDK_LOG_ERROR("Unknown option `-%c'.", optopt);
else
JZSDK_LOG_ERROR("Unknown option character `\\x%x'.", optopt);
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
break;
default:
break;
}
}
JZSDK_LOG_INFO("参数解析成功");
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}