gpt4 book ai didi

c - argp:如何将 argp_state 传递给 argp_usage、argp_error 等...?

转载 作者:行者123 更新时间:2023-12-04 15:54:12 25 4
gpt4 key购买 nike

在 C++ 中使用 argp 似乎缺少 python 的 argparse 处理得很好的互斥。在尝试模仿相同的行为时,我在这里注意到了一个问题。如何将 argp_state 传递给 main 中的 argp_usage(或 other argp helper functions)?否则似乎无法访问。我应该在 argp parse_opt 函数调用中使用它吗?不确定,任何可以提供帮助的人都可以。在 python 中,如果使用两个互斥选项,argsparse 会说以下内容:

usage: somescript [opt] ... [opt_n]
somescript: error: argument opt not allowed with argument optn_n

在 glibc 的 argp 中,你不能那样做,你必须想出你自己的方法。可以运行和测试以下示例代码。它表明我无法让 argp_state 传递 argp_usage() 因为我想显示用法和一些错误消息以及用法(接近尾声)。如果您知道如何正确实现,请修复它。

/* System Includes:
* ------------------- */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <argp.h>

/* Enabled Debug Print Macro. */
#define DEBUG(...) do { fprintf(stdout, __VA_ARGS__); printf("\n"); } while (0)

/*
Forward Declaration
*/
void report_last_error_to_user(int fd);

const char* argp_program_version = "1.0";

/* Program documentation. */
static const char* doc = "This is a test program";

/* The options we understand. */
static struct argp_option options[] =
{
{"test_option", 't', 0, 0, "This is a test option", 0},
{"other_option", 'o', 0, 0, "This is another option", 0},
{0, 0, 0, 0, 0, 0}
};

/* Used by main to communicate with parse_opt. */
struct Arguments
{
int test_option, other_option;
};

/* Parse a single option. */
static error_t
parse_opt (int key, char* arg, struct argp_state* state)
{
error_t err = 0;

/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct Arguments *arguments = (struct Arguments*)(state->input);

switch (key)
{
case 't':
{
arguments->test_option = 1;
break;
}
case 'o':
{
arguments->other_option = 1;
break;
}
default:
{
err = ARGP_ERR_UNKNOWN;
break;
}
}
return err;
}

/* Our argp parser. */
static struct argp argp_parser =
{
.options = options,
.parser = parse_opt,
.args_doc = "",
.doc = doc,
.children = NULL,
.help_filter = NULL,
.argp_domain = NULL
};

int main (int argc, char* argv[])
{
int exit_code = 0;

struct Arguments arguments;

/* Default values. */
arguments.test_option = 0;
arguments.other_option = 0;

/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
argp_parse(&argp_parser, argc, argv, 0, 0, &arguments);

int optionTrueCount = 0;
bool isOnlyOneOptionTrue = false;
if (arguments.test_option) optionTrueCount++;
if (arguments.other_option) optionTrueCount++;
if (1 == optionTrueCount) isOnlyOneOptionTrue = true;

if (arguments.test_option && isOnlyOneOptionTrue)
{
DEBUG("User commanded test_option");
}
else if (arguments.other_option && isOnlyOneOptionTrue)
{
DEBUG("User commanded another option");
}
else
{
argp_error(/*how do I get 'const argp_state' over here????*/NULL, "Options are mutually exclusive except version and help\n");
// OUTPUT:
// testapp: Options are mutually exclusive except version and help
//
// Segmentation fault (core dumped)
exit_code = -EINVAL;
}

exit (exit_code);
}

最佳答案

struct argp_state * state 是指向参数解析器状态的指针,它仅存在于对 argp_parse 的调用内部,因此在 argp_parse 返回后不存在。

使用 argp 验证参数的典型方法是使用 argp-special-keys ,在您的案例中,您想要为 ARGP_KEY_END 添加案例。另请注意,在默认情况下,您的解析器不应返回错误,除非您为所有特殊键实现一个案例。

static error_t
parse_opt (int key, char* arg, struct argp_state* state)
{
error_t err = 0;
struct Arguments *arguments = (struct Arguments*)(state->input);

switch (key)
{
case ARGP_KEY_INIT:
/* Do all initialization here */
break;
case ARGP_KEY_END:
/* Do final argument validation here */
if ( ... )
argp_error(state, "error");
break;
case 't':
break;
case 'o':
break;
}
return err;
}

关于c - argp:如何将 argp_state 传递给 argp_usage、argp_error 等...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com