[英]Taking parameters on the command line in C++
I've a program that takes two csv files and a checkin date as inputs and renders a certain output. 我有一个程序,将两个csv文件和一个签入日期作为输入并呈现一定的输出。 Thus I'd normally run the executable in this manner, 因此,我通常会以这种方式运行可执行文件,
./my_executable file.csv 2015-10-13
However my requirement is to have the usage behave this way 但是我的要求是使用法以这种方式表现
my_executable --input1 ./file1.csv --input2 ./file2.csv --date 2015-08-01
How can I do this. 我怎样才能做到这一点。 Do I have write the words input1, input2 and date somewhere in my code. 我是否在代码中的某处写了input1,input2和date一词。 Any help appreciated. 任何帮助表示赞赏。
Simplest way I can think of: 我想到的最简单的方法:
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <cassert>
int main(int argc, char *raw_argv[]) {
using namespace std;
vector<string> const args { raw_argv+1, raw_argv+argc };
assert(args.size() < 1 || args[0] == "--input1");
assert(args.size() < 3 || args[2] == "--input2");
if (args.size() > 4) {
std::string const& csv1 = args[1];
std::string const& csv2 = args[3];
std::string date = args.size() > 4? args[4] : "(unspecified)";
std::cout << "Arguments received: " << csv1 << ", " << csv2 << " date:" << date << "\n";
}
}
Prints eg 打印例如
./test --input1 stuff.csv --input2 other.csv
Arguments received: stuff.csv, other.csv date:(unspecified)
Command line arguments are passed to your program via the argument count and argument list parameters of main
: 命令行参数通过main
的参数计数和参数列表参数传递给您的程序:
int main(int argument_count, char * argument_list[]);
The first parameter is the number of arguments, including the name of your executable. 第一个参数是参数的数量,包括可执行文件的名称。
The second argument is an array of C-style strings, one for each argument (or word) on the command line. 第二个参数是C样式字符串的数组,命令行上的每个参数(或单词)一个。 The first item is usually the name of the program. 通常,第一项是程序的名称。
You can always write a small program to test this out: 您总是可以编写一个小程序来对此进行测试:
#include <iostream>
int main(int arg_count, char * arg_list[])
{
for (unsigned int i = 0; i < arg_count; ++arg_count)
{
std::cout << "Argument " << i << ": " << arg_list[i] << std::endl;
}
return EXIT_SUCCESS;
}
Edit 1: 编辑1:
Your parameters would line up as: 您的参数将排列为:
Argument 0: my_executable 参数0:my_executable
Argument 1: --input1 参数1:--input1
Argument 2: ./file1.csv 参数2:./ file1.csv
Argument 3: --input2 参数3:--input2
Argument 4: ./file2.csv 参数4:./ file2.csv
//... // ...
If you want to compare these parameters, then yes, you would need to type "input1": 如果要比较这些参数,则需要输入“ input1”:
//...
std::string arg1 = arg_list[1];
if (arg1 == "--arg1")
{
//...
}
This should give you a kickstart. 这应该给您一个启动的机会。
https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3 https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3
or if you want to handle the arguments manually. 或者如果您想手动处理参数。
see: https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments 请参阅: https : //www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments
int main(int argc, const char **argv[])
{
for(int i = 0; i < argc; i++) {
std::cout << argv[i] << std::endl;
}
return 0;
}
Usually when you give argument in that way the order should not matter, so you'll have to be able to parse the arguments in any order. 通常,当您以这种方式提供参数时,顺序并不重要,因此您将必须能够以任何顺序解析参数。
Here is a possible solution: 这是一个可能的解决方案:
struct arguments
{
std::string input1;
std::string input2;
std::string date;
};
bool parse_arguments(int argc, char** argv, arguments& args)
{
if(argc < 7){ //or set defaults
//print usage();//implement
return false;
}
for(int i=1; i<argc;i+=2){
string command = argv[i];
string argument = argv[i+1];
if(command == "--input1"){
args.input1 = argument;
}
else if(command == "--input2"){
args.input2 = argument;
}
else if(command == "--date"){
args.date = argument;
}
else{
std::cerr<<"Unknown argument: " <<command<<std::endl;
//print usage();
return false;
}
}
if(args.input1.empty() || args.input2.empty() || args.data.empty())
return false;
return true;
}
int main(int argc, char* argv[]){
arguments args;
parse_arguments(argc,argv, args);
//now you can use the struct.
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.