I am trying to use the optparse-c library. This library seems to me quite useful. What I find not
so clear is how to invoke the help without calling prog -h
.
I can do it if I remove the keyword static
from the function signature static void printopts(FILE *f)
in
optparse.c
, then I could do:
int main(int argc, char **argv)
{
int i;
opt_basename(argv[0], '/');
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
printopts(stderr);
exit(EXIT_FAILURE);
}
...
However, it seems to me the author of this library had some other idea how to invoke the help function. I would be happy if someone could help me figure this out.
I also tried this:
char h[] = "-h";
opt_basename(argv[0], '/');
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
argc++;
strcpy(argv[1], h);
opt_parse("usage: %s [options] arg1 arg2", options, argv);
exit(EXIT_FAILURE);
}
Oh well, after a lot of poking and looking into the source code, I found out the not trivial (as I am still learning C) solution:
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
opt_help(0, (void *)0);
exit(EXIT_FAILURE);
}