argp:如何将argp_state传递给argp_usage,argp_error等...?

时间:2018-10-02 20:33:31

标签: c command-line-interface glibc mutual-exclusion argp

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

1 个答案:

答案 0 :(得分:2)

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