, , , - . , . , , DefineRequiredArgs DefineOptionalArgs, .
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
void function_to_call(std::vector<std::vector<std::string> > &req_args,
std::vector<std::vector<std::string> > &opt_args,
int num1 = 1,
int num2 = 2
)
{
std::cout << "Required Options:\n" ;
for (int i=0; i<req_args.size(); i++) {
std::cout << "\t" << req_args[i][0] << " = " << req_args[i][1] << std::endl;
}
std::cout << "Optional Options:\n" ;
for (int i=0; i<opt_args.size(); i++) {
std::cout << "\t" << opt_args[i][0] << " = " << opt_args[i][1] << std::endl;
}
}
std::vector<std::vector<std::string> > DefineRequiredArgs()
{
std::vector<std::vector<std::string> > req_args ;
std::vector<std::string> arg(2) ;
arg[1] = "" ;
arg[0] = "1st_argument" ;
req_args.push_back(arg) ;
arg[0] = "2nd_argument" ;
req_args.push_back(arg) ;
arg[0] = "3rd_argument" ;
req_args.push_back(arg) ;
arg[0] = "4th_argument" ;
req_args.push_back(arg) ;
return req_args ;
}
std::vector<std::vector<std::string> > DefineOptionalArgs()
{
std::vector<std::vector<std::string> > opt_args ;
std::vector<std::string> arg(2) ;
arg[1] = "" ;
arg[0] = "5th_argument" ;
arg[1] = "foo" ;
opt_args.push_back(arg) ;
arg[0] = "6th_argument" ;
arg[1] = "bar" ;
opt_args.push_back(arg) ;
arg[0] = "7th_argument" ;
arg[1] = "521600" ;
opt_args.push_back(arg) ;
arg[0] = "8th_argument" ;
arg[1] = "86" ;
opt_args.push_back(arg) ;
arg[0] = "9th_argument" ;
arg[1] = "somethingelse" ;
opt_args.push_back(arg) ;
return opt_args ;
}
int main(int argc, char** argv)
{
std::vector<std::vector<std::string> > req_args = DefineRequiredArgs() ;
std::vector<std::vector<std::string> > opt_args = DefineOptionalArgs() ;
if( argc < req_args.size()+1 ) {
std::cerr << "Usage: \n\t" << argv[0] ;
for (int i=0; i<req_args.size(); i++) {
std::cerr << "\n\t" << req_args[i][0] ;
}
for (int i=0; i<req_args.size(); i++) {
std::cerr << "\n\t" << opt_args[i][0]
<< " (optional Default=" << opt_args[i][1] << ")" ;
}
std::cerr << std::endl;
} else {
int opt_counter(1) ;
while ((opt_counter <= req_args.size())) {
req_args[opt_counter-1][1] = std::string(argv[opt_counter]) ;
opt_counter++ ;
}
int offset(req_args.size()+1) ;
while ((opt_counter < argc)) {
opt_args[opt_counter-offset][1] = std::string(argv[opt_counter]) ;
opt_counter++ ;
}
int num1, num2 ;
std::stringstream stream ;
stream << opt_args[2][1] << ' ' << opt_args[3][1] ;
stream >> num1 >> num2 ;
function_to_call(req_args, opt_args, num1, num2) ;
}
return 0;
}
, , , :
Usage:
./multi_option_helper
1st_argument
2nd_argument
3rd_argument
4th_argument
5th_argument (optional Default=foo)
7th_argument (optional Default=521600)
8th_argument (optional Default=86)
9th_argument (optional Default=somethingelse)
, "++", "++ 11", , g++ multi_option_helper.cpp -o multi_option_helper, ++ 11, ,
, , , , (, --Arg1=arg1_val), "GetOpt". . ( -h --help ).
: BOOST, . ( , BOOST , ), , , . GetOpt, , , . , . , .