From ab0ce3ade34ec397ad2ad2123c0debf455d06534 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Mon, 21 Aug 2017 20:41:22 +0800 Subject: [PATCH] main: merge cli/config arguments and check for duplications --- main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 942a484..0501d30 100755 --- a/main.cpp +++ b/main.cpp @@ -2544,8 +2544,8 @@ void print_help() //printf("common options,these options must be same on both side\n"); } -void process_arg(int argc, char *argv[]); -void load_config(char *config_file, char *argv0) +void process_arg(int argc, char *argv[], bool read_config = true); +void load_config(char *config_file, int argc_orig, char *argv_orig[]) { // Load configurations from config_file instead of the command line. // See config.example for example configurations @@ -2572,16 +2572,24 @@ void load_config(char *config_file, char *argv0) } } conf_file.close(); - int argc = arguments.size(); - char *argv[argc+1]; - argv[0]=argv0; + + // Append the new arguments to the original argv + int argc = arguments.size() + argc_orig; + char *argv[argc]; for(int i=0; i short_opts_map = { + {"-k","--key"}, + {"-a","--auto-rule"}, + {"-g","--gen-rule"}, + }; // Keep this in sync with the shortcuts - int option_index = 0; + int option_index = 0; + std::set checked_opts = {}; for (i = 0; i < argc; i++) { if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) @@ -2621,11 +2635,11 @@ void process_arg(int argc, char *argv[]) myexit(0); } - if(strcmp(argv[i],"--config-file")==0) + if(read_config&&strcmp(argv[i],"--config-file")==0) { if(isecond; + } + if(checked_opts.find(opt)!=checked_opts.end()) + { + char *err_msg = new char(255); + sprintf(err_msg,"Duplicate argument %s",opt.c_str()); + log_bare(log_fatal,err_msg); + myexit(-1); + } + checked_opts.insert(opt); + } } if (argc == 1) {