[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#313175: test case for failing argp



Attached is a minimal test case showing the problem. 

Compile with "gcc -o minargp minargp.c", 
run with somthing like "./minargp -r 15"
to get
	argument value for -r = (null)
	repeat count has been set to 10

Expected behaviour:
	argument value for -r = 15
	repeat count has been set to 15

Why is the argument value empty?

Drew

#include <stdlib.h>
#include <argp.h>

/* Program documentation. */
static char doc[] =
"minimal argp test for OPTION_ARG_OPTIONAL.";

/* A description of the arguments we accept. */
static char args_doc[] = "";

/* The options we understand. */
static struct argp_option options[] = {
    {"repeat",   'r', "COUNT", OPTION_ARG_OPTIONAL,
     "Repeat the output COUNT (default 10) times"},
    { 0 }
};

/* Used by `main' to communicate with `parse_opt'. */
struct arguments
{
    int repeat_count;             /* COUNT arg to `--repeat' */
};

/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
    /* Get the `input' argument from `argp_parse', which we
       know is a pointer to our arguments structure. */
    struct arguments *arguments = state->input;

    switch (key)
    {
    case 'r':
	printf("argument value for -r = %s\n", arg);
	arguments->repeat_count = arg ? atoi (arg) : 10;
	break;
    }
    return 0;
}

/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };

int main (int argc, char **argv)
{
    int i, j;
    struct arguments arguments;

    /* Default values. */
    arguments.repeat_count = 1;

    argp_parse (&argp, argc, argv, 0, 0, &arguments);

    printf("repeat count has been set to %i\n", arguments.repeat_count );
}


Reply to: