我试图编写代码,其中开关"--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
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, 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
您对no-feature
的测试阻止了对feature
的检查,因为它在失败时会直接进入error
。以下应该更有效:
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;
}
}