getopt:不识别有效的命令行参数
getopt: not recognizing valid command line arguments
我是Linux环境中的C 编程,我正在尝试使用GetOpt解析命令行参数。我想需要一个输入-s或-Q(分别分别 - stack和 - Quele),而不是兼具的输入-O,带有所需的参数:
int opt = 0, index = 0, stack=-1, map=-1;
while((opt = getopt_long (argc, argv, ":sqho:", longOpts, &index)) != -1){
cout<<opt;
switch(opt) {
case 's':
stack=0;
cout << "Stack"<<stack<<"n";
break;
case 'q':
stack=1;
cout << "Queue"<<stack<<"n";
//optarg is defined in getopt.h
break;
case 'h':
cout<< "To run this program, use one of the valid cmd line args (longforms: stack, queue, help, output (M|L); shortforms: s, q, h, o (M|L), respectiely) naccompanied with appropriate file redirection";
exit(0);
break;
case 'o':
//opt is 'M' or 'L'
cout<<"output method is: "<<optarg<<"n";
if(*optarg=='M') map=1;
else if(*optarg=='L') map=0;
else map=-1;
cout<<map<<"n";
case ':':
cerr<<"Map or list output must be specified as an argument to -o: "<<opt<<"n";
case '?':
cerr << "Command line error. one or more flags not recognized: " <<opt<<"n";
//exit(1);
break;
}
}
for(int i=1; i<argc; i++){
cout<<*argv[i]<<endl;
}
return 0;
}
这包含顶部的适当#CRUDES,并填充了罚款。
但是,当我尝试运行./-hunt -q -o m时,cases'q','o',':'和'和'?'全部执行。我决定输出任何角色触发":和"?块,控制台显示111个字符" O"的ASCII值。
这对我来说是极其混乱的,因为在GetOpt触发了" O"块之后,是否应该返回-1表示没有更多的命令行参数?感谢任何帮助/建议。谢谢!
您在case 'o'
和case ':'
中缺少break
。
这会导致从o
到:
到?
。
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 使用 C++ 将命令行参数拆分为参数/向量
- 如何在OMNET++中添加专门的命令行参数?
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- atoi() 在应用于大型命令行参数时会产生不正确的值
- 命令行参数,cant 或两个变量
- 在 Windows 中使用 boost::p rogram_options 从命令行参数读取 Unicode 字符
- 如何在 Android/NDK 上将命令行参数从 gradlew.bat 传递到 Clang
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 命令行参数在不到 3 个 LOC 中 int?
- 在命令行参数中使用引号
- 在VS2013中使用devenv (C++)传递命令行参数argv
- 二进制数据作为命令行参数
- 如何在 C++11 中创建具有命令行参数大小的动态数组?
- 需要有关如何设置getopt_long以正确传递命令行参数的建议
- Bash 脚本和命令行参数