fixed argp cehckign for right commands

This commit is contained in:
Nathan Singer 2025-02-28 00:59:56 -05:00
parent 25d4b373ce
commit e879a947d8

View File

@ -7,6 +7,7 @@
#include "include/json.h" #include "include/json.h"
struct flags { struct flags {
char* isbn;
bool json, db, print; bool json, db, print;
}; };
@ -17,8 +18,8 @@ const char* argp_program_bug_address = "supernovaa41@gmx.com";
static char doc[] = "Lookup details of a book from the ISBN. Using OpenLibrary API."; static char doc[] = "Lookup details of a book from the ISBN. Using OpenLibrary API.";
static char args_doc[] = "<ISBN>"; static char args_doc[] = "<ISBN>";
static struct argp_option options[] = { static struct argp_option options[] = {
{0, 'j', "json", 0, "output to json", 0}, {"json", 'j', 0, 0, "output to json", 0},
{0, 'd', "db", 0, "output to sqlite3 database", 0}, {"db", 'd', 0, 0, "output to sqlite3 database", 0},
{0, 'p', 0, 0, "print the full json response", 0}, {0, 'p', 0, 0, "print the full json response", 0},
{0} {0}
}; };
@ -44,7 +45,12 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state)
case 'p': case 'p':
flags->print = true; flags->print = true;
break; break;
case ARGP_KEY_ARG:
flags->isbn = arg;
break;
case ARGP_KEY_END: case ARGP_KEY_END:
if (state->arg_num < 1)
argp_usage(state);
break; break;
default: default:
return ARGP_ERR_UNKNOWN; return ARGP_ERR_UNKNOWN;
@ -63,7 +69,8 @@ int main(int argc, char* argv[])
init_flags(&flags); init_flags(&flags);
argp_parse(&argp, argc, argv, 0, 0, &flags); argp_parse(&argp, argc, argv, 0, 0, &flags);
book_api_call("9780762437818", &resp); //book_api_call("9780762437818", &resp);
book_api_call(flags.isbn, &resp);
if (flags.print) if (flags.print)
puts(resp.buf); puts(resp.buf);