- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 C++ 程序中,我希望有一个带有必需参数的“long-only”选项。下面是我使用 getopt_long() 的最小示例,但它不起作用:
#include <getopt.h>
#include <cstdlib>
#include <iostream>
using namespace std;
void help (char ** argv)
{
cout << "`" << argv[0] << "` experiments with long options." << endl;
}
void parse_args (int argc, char ** argv, int & verbose, int & param)
{
int c = 0;
while (1)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"verbose", required_argument, 0, 'v'},
{"param", required_argument, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hv:",
long_options, &option_index);
cout << "c=" << c << endl;
if (c == -1)
break;
switch (c)
{
case 0:
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'h':
help (argv);
exit (0);
case 'v':
verbose = atoi(optarg);
break;
case 'param':
param = atoi(optarg);
break;
case '?':
abort ();
default:
abort ();
}
}
}
int main (int argc, char ** argv)
{
int verbose = 0;
int param = 0;
parse_args (argc, argv, verbose, param);
cout << "verbose=" << verbose << " param=" << param << endl;
return EXIT_SUCCESS;
}
我用这个命令编译它(gcc 版本 4.1.2 20080704 Red Hat 4.1.2-46):
g++ -Wall test.cpp
它告诉我:
test.cpp:44:10: warning: character constant too long for its type
结果如下:
$ ./a.out -v 2 --param 3
c=118
c=0
option param with arg 3
c=-1
verbose=2 param=0
我试图让它在 ideone 上运行但它甚至无法识别选项 -v
。
正如 trojanfoe 在他的 comments 中指出的那样另一个问题,应该可以使用“long-only”选项,因为 GNU tar 可以做到。但是,GNU tar 使用 argp我很难理解它的 source code .
有人可以给我一个最小的例子吗,用 GNU getopt_long()
或 argp()
?
最佳答案
有两个问题:
根据示例代码(您的链接),结构中定义的最终选项必须是 {0,0,0,0}
.我建议将定义更改为
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"verbose", required_argument, 0, 'v'},
{"param", required_argument, 0, 0},
{0,0,0,0}
};
(更重要的是,)您必须包含实际处理“param”选项的代码。你在 '0'
中这样做案例:
case 0:
if (long_options[option_index].flag != 0)
break;
if (strcmp(long_options[option_index].name,"param") == 0)
param = atoi(optarg);
break;
如您所见,我使用了 strcmp
比较字符串的函数;为此你需要 #include <cstring>
.对了,你还需要#include <cstdio>
供您使用 printf
.
有了这些更改,该程序对我来说工作正常(在 GCC 4.5.1 上测试)。
关于c++ - 在 C++ 中,如何只使用带有必需参数的长选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213071/
内部 TransactionScope 将哪个事务作为环境事务? using ( var t1 = new TransactionScope( TransactionScopeOption.Requi
我需要匹配以下模式:N.N.N 324324.234324.234324 匹配,以及 1.1.1 I have the following pattern: (\d*\.\d*\.\d*) 问题是,它
这个问题在这里已经有了答案: HTML5 required attribute not working (2 个答案) 关闭上个月。 我最近为我创建了一个网页来学习更多关于 HTML 的知识,我发现
我在 NgForm 中有以下字段: 0'" /> Dependency is required 问题是这个字段总是需要的,不管 [required]相关条件depSelected>0 . d
我有一个模型类,其中包含几个必填字段: public class UserMetadata { [Required(ErrorMessage = "Please enter a name.")]
我有一张预订表格,需要验证。某些字段的启用/禁用取决于之前选择的选项。我的问题是,我无法提交禁用字段的表单,因为它等待来自空字段的“有效”输入数据。 是否有一种方法可以仅在启用字段集时启用/禁用这些字
如何为textarea设置两个属性(默认文本,点击后隐藏,必填)? Default text HTML“认为”默认文本是我的输入。我该如何修复它?谢谢大家。 最佳答案 如果您不介意兼容性,简单
我是编码新手,所以这可能是一个非常简单的问题。 编译时出现错误: GradeAnalyzer.java:49: error: method getAverage in class GradeAnaly
我有一个带有 2 个输入字段的 from:手机和电话。我希望至少需要其中一个字段,以便可以提交表单。要么您输入手机号码,不再需要电话输入,要么反之亦然。 我发现 Jquery 验证器有条件语句,
看看下面的代码,我正在使用 required(System.ComponentModel.DataAnnotations) 命名空间,但它一直向我显示红色波浪线,并显示无法找到“type or nam
所以我有一个 knockout 原型(prototype),您可以在其中动态添加输入,然后设置每个设置。将其视为表单生成器就是这样。然而,我注意到禁用和必需的效果不太好。它将值设置为禁用或必需,但是当
我正在使用 Entity Framework 的表拆分功能来拆分我的实体数据模型,如下所示: +--------+ +--------------+ | News | | NewsI
我有 3 个关于 TransactionScopeOption 的问题。Required 把我逼疯了,我无法在网上找到他们的答案。 A. 我很难思考什么时候我必须在现实中编写这段代码?为什么我不应该将
public static int biggestArrayGap(int []a, int n) { int biggestGap = Math.abs(a[1]-a[0]); for (i
我真的发现 django 表单集令人困惑。 我尤其对以下我不太了解的概念有疑问: The formset is smart enough to ignore extra forms that were
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我正在使用jquery validation插入。当我使用 required( dependency-expression ) 时,我注意到 required( dependency-expressi
代码: ArrayList marks = new ArrayList(); String output = "Class average:" + calculateAverage() + "\
static void sort (Textbook [ ] info, int numEntries) { long tempIsbn = 0; String tempTitle =
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我是一名优秀的程序员,十分优秀!