4

Using argp in c++ appears to be missing mutual exclusion that python's argparse handles fairly well. In trying to emulate the same behavior I've noted a problem here. How to pass argp_state to the argp_usage (or other argp helper functions) in main? It appears to be inaccessible otherwise. Am I supposed to use it in the argp parse_opt function call? Not sure, anyone who can help please do. In python, argsparse would say the following if two mutually exclusive options were used:

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

In glibc's argp, you can't do that you have to come up with your own way. The following example code can be run and tested. It shows that I can't get argp_state to pass argp_usage() for when I want to display the usage and some error message along with the usage (near the end). Please fix it if you know how to implement this properly.

/* 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);
}
nndhawan
  • 597
  • 6
  • 24

1 Answers1

3

struct argp_state * state is a pointer to the argument parser state and that only exists inside of the call to argp_parse, and hence don't exists after argp_parse has returned.

The typical way to validate arguments with argp is to use the argp-special-keys, in your case you want to add a case for ARGP_KEY_END. Also note that in the default case of your parser should not return an error unless you implement a case for all the special keys.

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;
}
jo-art
  • 215
  • 1
  • 9