3

在 c++ 中使用 argp 似乎缺少 python 的 argparse 处理得相当好的互斥。在尝试模仿相同的行为时,我在这里注意到了一个问题。如何将 argp_state 传递给 main 中的 argp_usage(或其他 argp 辅助函数)?否则它似乎无法访问。我应该在 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);
}
4

1 回答 1

2

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

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

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;
}
于 2018-10-02T21:28:33.430 回答