本帖最后由 keer_zu 于 2020-6-16 09:21 编辑
刚写了一个命令参数解析的程序,使用getopt。
主要代码如下:
#include <stdio.h>
#include <string>
#include <map>
#include <stdlib.h>
#include "operation.h"
using namespace std;
std::map<char,operation_context_t> operation_contexts;
#define no_argument 0
#define required_argument 1
#define optional_argument 2
struct option *options = NULL;
static int operation_p(char *arg)
{
TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
tm.TestItemPrint();
}
static int operation_test(char *arg)
{
TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
tm.TestSpecific(arg);
}
static int operation_a(char *arg)
{
TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
tm.TestAll();
}
void operation_contexts_init()
{
int i = 0;
char opt;
opt = 'p';
operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = "p",.has_arg=no_argument,.flag=NULL,.val=opt},.operation=operation_p}));
opt = 't';
operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = "test",.has_arg=required_argument,.flag=NULL,.val=opt},.operation=operation_test}));
opt = 'n';
operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = NULL,.has_arg= 0,.flag=NULL,.val=opt},.operation=operation_null}));
options = (struct option *)malloc(sizeof(struct option) * operation_contexts.size());
if(options != NULL) {
map<char,operation_context_t>::reverse_iterator iter;
for(iter = operation_contexts.rbegin(); iter != operation_contexts.rend(); iter++) {
options[i].name = ((operation_context_t)(iter->second)).m_option.name;
options[i].has_arg = ((operation_context_t)(iter->second)).m_option.has_arg;
options[i].flag = ((operation_context_t)(iter->second)).m_option.flag;
options[i].val = ((operation_context_t)(iter->second)).m_option.val;
i ++;
}
}
}
int run_operation(char opt,char *arg)
{
map<char,operation_context_t>::iterator iter;
iter = operation_contexts.find(opt);
if(iter != operation_contexts.end()) {
return operation_contexts[opt].operation(arg);
}
}
使用用例:
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include "operation.h"
extern struct option *options;
int main(int argc, char *argv[])
{
int opt;
int digit_optind = 0;
int option_index = 0;
std::string str = "a::b:c:d";
operation_contexts_init();
if(options != NULL) {
while((opt = getopt_long_only(argc,argv,str.c_str(),options,&option_index))!= -1) {
run_operation(opt,optarg);
}
}
}
@yyy71cj @dirtwillfly
|