#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
const char *argp_program_version = "argp_example 1.0";
const char *argp_program_bug_address = "<[email protected]>";
static char doc[] =
"This is a simple example of using argp to parse command-line arguments.";
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output"},
{"output", 'o', "FILE", 0, "Output to FILE instead of the standard output"},
{0}
};
struct arguments {
char *output_file;
int verbose;
};
static error_t parse_opt(int key, char arg, struct argp_state state) {
struct arguments *arguments = state->input;
switch (key) {
case 'v':
arguments->verbose = 1;
break;
case 'o':
arguments->output_file = arg;
break;
case ARGP_KEY_ARG:
return 0;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, 0, doc};
int main(int argc, char argv) {
struct arguments arguments;
arguments.output_file = "-";
arguments.verbose = 0;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
printf("OUTPUT FILE: %s\n", arguments.output_file);
printf("VERBOSE: %s\n", arguments.verbose ? "yes" : "no");
return 0;
}