C 在命令行选项解析器中的选项名称之间切换
我试着编写代码,其中开关'--feature'可以产生相反的效果,称为'--no feature' 伪代码:C 在命令行选项解析器中的选项名称之间切换,c,command-line,C,Command Line,我试着编写代码,其中开关'--feature'可以产生相反的效果,称为'--no feature' 伪代码: static gboolean option_feature_cb (const gchar *option_name, const gchar *value, gpointer data, GError **error) { if (strcmp(option_name, "no-feature") != 0) goto error; else
static gboolean
option_feature_cb (const gchar *option_name, const gchar *value, gpointer data, GError **error)
{
if (strcmp(option_name, "no-feature") != 0)
goto error;
else
x = 0;
if (strcmp(option_name, "feature") != 0)
goto error;
else
x = 1;
return TRUE;
error:
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
_("invalid option name (%s), must be '--feature' or '--no-feature'"), value);
return FALSE;
}
int main(int argc, char* argv[])
{
.................................................................................................................
const GOptionEntry entries[] = {
{ "[no-]feature", '\0', 0, G_OPTION_ARG_CALLBACK, option_feature_cb, N_("Disable/enable feature"), NULL },
{ NULL }
};
我需要帮助来编写代码来完成这项工作
更新
我在Ruby中发现了这种解析命令,但我知道在c和gnome中如何使用它:
开关可以具有否定形式。开关--negated可以有一个产生相反效果的开关,称为--no negated。要在开关描述字符串中对此进行描述,请将替代部分放在括号中:--[no-]negated。如果遇到第一个表单,则会将true传递给块,如果遇到第二个表单,则会阻止false
options[:neg] = false
opts.on( '-n', '--[no-]negated', "Negated forms" ) do|n|
options[:neg] = n
end
您对
无功能的测试
会阻止您检查功能
,因为它失败时会直接进入错误
。以下措施应能更好地发挥作用:
static gboolean
option_feature_cb (const gchar *option_name, const gchar *value, gpointer data, GError **error)
{
if (strcmp(option_name, "no-feature") == 0) {
x = 0;
return TRUE;
} elseif (strcmp(option_name, "feature") == 0) {
x = 1;
return TRUE;
} else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
_("invalid option name (%s), must be '--feature' or '--no-feature'"), value);
return FALSE;
}
}