2013-06-28 4 views
0

스위치 '--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', 0, G_OPTION_ARG_CALLBACK, option_feature_cb, N_("Disable/enable feature"), NULL }, 
    { NULL } 
}; 

나는이 작업을 수행하는 코드를 작성하는 데 도움이 필요합니다.

UPDATE

나는 루비에서이 구문 분석 명령을 찾을 내가 뭘 C와 그놈이를 사용하는 :

스위치는 부정 형태를 가질 수있다. switch --negated는 --no-negated라고하는 반대 효과를내는 스위치를 가질 수 있습니다. 이것을 스위치 설명 문자열에 설명하려면 대괄호 안에 대체 부분을 입력하십시오. - [no-]는 부정됩니다. 첫 번째 양식이 발견되면 true가 블록으로 전달되고 두 번째 양식이 발견되면 false가 차단됩니다. 실패 때 error로 바로 이동하기 때문에

options[:neg] = false 
opts.on('-n', '--[no-]negated', "Negated forms") do|n| 
    options[:neg] = n 
end 

답변

1

no-feature에 대한 귀하의 시험 적, feature 검사 방지 할 수 있습니다. 다음은 더 잘 작동합니다.

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; 
    } 
}