如果程序被指定像这样运行,那么在c++中解析命令行参数的最佳方式是什么:
prog [-abc] [input [output]]
是否有某种方法可以在标准库中实现这一点,或者我需要自己编写代码?
相关:
在unicode c++应用程序中解析命令行参数
如果程序被指定像这样运行,那么在c++中解析命令行参数的最佳方式是什么:
prog [-abc] [input [output]]
是否有某种方法可以在标准库中实现这一点,或者我需要自己编写代码?
相关:
在unicode c++应用程序中解析命令行参数
当前回答
我在一些项目中使用过GetPot: http://getpot.sourceforge.net/
主要特点:所有东西都在一个头文件中,没有构建的麻烦。只需将它保存在你机器上的某个地方,并在你的文件中使用main()
最近没有更新,但它有很好的文档记录,工作得很好。
其他回答
AnyOption是一个c++类,用于轻松解析复杂的命令行选项。它还以选项值对格式解析来自rsourcefile的选项。
AnyOption实现了传统的POSIX风格的字符选项(-n)以及较新的GNU风格的长选项(——name)。或者您可以通过要求忽略POSIX样式选项来使用更简单的长选项版本(-name)。
如果你可以使用boost库,我推荐boost::program_options。
在STL和常规的c++ /C运行时库中都没有特定的东西。
一个简单的解决方案是将argv放入std::map中,以便查找:
map<string, string> argvToMap(int argc, char * argv[])
{
map<string, string> args;
for(int i=1; i<argc; i++) {
if (argv[i][0] == '-') {
const string key = argv[i];
string value = "";
if (i+1 < argc && argv[i+1][0] != '-') {
value = string(argv[i+1]);
i++;
}
args[key] = value;
}
}
return args;
}
使用示例:
#include <map>
#include <string>
#include <iostream>
using namespace std;
map<string, string> argvToMap(int argc, char * argv[])
{
map<string, string> args;
for(int i=1; i<argc; i++) {
if (argv[i][0] == '-') {
const string key = argv[i];
string value = "";
if (i+1 < argc && argv[i+1][0] != '-') {
value = string(argv[i+1]);
i++;
}
args[key] = value;
}
}
return args;
}
void printUsage()
{
cout << "simple_args: A sample program for simple arg parsing\n"
"\n"
"Example usage:\n"
" ./simple_args --print-all --option 1 --flag 2\n";
}
int main(int argc, char * argv[])
{
auto args = argvToMap(argc, argv);
if (args.count("-h") || args.count("--help")) {
printUsage();
}
else if (args.count("--print-all")) {
for (auto const & pair: args)
cout << "{" << pair.first << ": " << pair.second << "}\n";
}
return 0;
}
输出:
$ ./simple_args --print-all --option 1 --flag "hello world"
{--flag: hello world}
{--option: 1}
{--print-all: }
这种方法肯定有很大的局限性,但我发现它很好地平衡了简单性和实用性。
谷歌的旗帜
Following from my comment and from rbaleksandar's answer, the arguments passed to any program in C are string values. You are provided the argument count (argc) which gives you the argument indexes zero-based beginning with the name of the program currently being run (which is always argv[0]). That leaves all arguments between 1 - argc as the user supplied arguments for your program. Each will be a string that is contained in the argument vector (which is a pointer to an array of strings you will seen written as char *argv[], or equivalently as a function parameter char **argv) Each of the strings argv[1] to argv[argc-1] are available to you, you simply need to test which argument is which.
这将允许您分离,并使它们可用为命令(cmd),选项(opt)和最后的参数(arg)到您的cmd。
Now it is worth noting, that the rules of your shell (bash, etc..) apply to the arguments passed to your program, word-splitting, pathname and variable expansion apply before your code gets the arguments. So you must consider whether single or more commongly double-quoting will be required around any of your arguments to prevent the normal shell splitting that would otherwise apply (e.g. ls -al my file.txt would results in 4 user-supplied arguments to your code, while ls -al "my file.txt" or ls -al my\ file.txt which would result in the 3 your were expecting.
把所有这些放在一起,您的简短解析可以像下面这样完成。(你也可以自由地做你喜欢的,使用一个开关而不是嵌套的if等…)
#include <stdio.h>
int main (int argc, char **argv) {
char *cmd = NULL, /* here, since you are using the arguments */
*opt = NULL, /* themselves, you can simply use a pointer */
*arg = NULL; /* or the argument itself without a copy */
/* looping using the acutal argument index & vector */
for (int i = 1; i < argc; i++) {
if (*argv[i] != '-') { /* checking if the 1st char is - */
if (!cmd) /* cmd is currently NULL, and */
cmd = argv[i]; /* no '-' it's going to be cmd */
else /* otherwise, cmd has value, so */
arg = argv[i]; /* the value will be opt */
}
else /* here the value has a leading '-', so */
opt = argv[i]; /* it will be the option */
}
printf ("\n cmd : %s\n opt : %s\n arg : %s\n\n",
cmd, opt, arg);
return 0;
}
使用/输出示例
如果你运行代码,你会发现它为参数提供了分离,并提供了单独的指针,以方便它们的使用:
$ ./bin/parse_cmd ls -la ./cs3000
cmd : ls
opt : -la
arg : ./cs3000
(需要注意的是,如果你的任务是构建一个命令字符串,你需要复制多个值来表示opt或arg,那么你不能再简单地使用指针,而需要创建存储,要么通过简单的数组声明而不是指针开始,或者你可以根据需要动态分配存储,例如malloc, calloc和/或realloc。然后,您将有可用的存储空间来复制和连接其中的值。)
如果这是你的挑战,那么在所有这些答案之间,你应该知道如何处理你的问题。如果你必须更进一步,实际上让你的程序执行带有opt和arg的cmd,那么你会想要查看fork来生成一个半独立的进程,在其中运行将执行你的cmd opt和arg,使用类似于execv或execvp的东西。祝你好运,如果你有进一步的问题,请发表评论。