#include const char * argp_program_version = "argp-bug??"; static struct argp_option opts[] = { {"good", 'g', "ARG", 0, "This one is good" }, {"bad", 'b', "ARG", OPTION_ARG_OPTIONAL, "This one is bad" }, {"cheap-hack", 'h', "ARG", OPTION_ARG_OPTIONAL, "Cheap hack" }, { NULL, 0, NULL, 0, NULL } }; struct args { char * good; char * bad; char * cheap_hack; }; static error_t parse_opt (int key, char * arg, struct argp_state * state) { struct args * args = state->input; switch (key) { case 'g': args->good = arg; break; case 'b': if (arg) args->bad = arg; break; case 'h': if (arg) args->cheap_hack = arg; else if (state->next < state->argc) { if (state->argv[state->next][0] != '-') { args->cheap_hack = state->argv[state->next]; state->next++; } } break; default: break; } return (0); } int main (int argc, char ** argv) { struct args args = { NULL, NULL, NULL }; struct argp argp = { opts, parse_opt, NULL, NULL }; argp_parse (&argp, argc, argv, 0, 0, &args); printf ("Good: %s\nBad: %s\nCheap Hack: %s\n", args.good, args.bad, args.cheap_hack); return (0); }