From 2c19c83df1f4ad6068872e8efa484daeab95a345 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Tue, 7 Mar 2017 13:08:12 -0500 Subject: [PATCH] changes: - increment version - add missing command line options --- src/ruiji.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/ruiji.c b/src/ruiji.c index 3227a81..8f5d333 100644 --- a/src/ruiji.c +++ b/src/ruiji.c @@ -6,7 +6,7 @@ #include "interface.h" -#define VERSION "0.1" +#define VERSION "0.2" #define IQDB_URL "https://iqdb.org" #define IQDB_UPLOAD_FIELD "file" @@ -31,14 +31,20 @@ struct ruiji_arg_opts { char *file; }; - +/* struct for command line argument options */ static struct argp_option options[] = { - { "verbose", 'v', 0, 0, "Produce verbose output" }, - { "quiet", 'q', 0, 0, "Suppress verbose output" }, - { "file", 'f', "FILE", 0, "Takes in the given file to upload" }, - { "noprompt", 'y', 0, 0, "Skips user interactions and downloads the most similar image" }, - { "threshold", 't', "Numbah", 0, - "Only show images above certain similarity percentage" }, + { "version", 'V', 0, 0, + "Show version number" }, + { "verbose", 'v', 0, 0, + "Produce verbose output" }, + { "quiet", 'q', 0, 0, + "Suppress verbose output" }, + { "file", 'f', "FILE", 0, + "Takes in the given file to upload" }, + { "noprompt", 'y', 0, 0, + "Skips user interactions and downloads the most similar image" }, + { "threshold", 't', "Numbah", 0, + "Only show images above certain similarity percentage" }, { 0 } }; @@ -54,9 +60,9 @@ void set_default_opt(struct ruiji_arg_opts *arg_opt) arg_opt->file = "file.png"; } +/* Parse and process command line arguments */ error_t parse_opt(int key, char *arg, struct argp_state *state) { - struct ruiji_arg_opts *arguments = state->input; switch (key) { @@ -75,6 +81,9 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) case 'y': arguments->prompt = 0; break; + case 'V': + arguments->showversion = 1; + break; // case ARGP_KEY_ARG: // arguments->args[state->arg_num] = arg; // break; @@ -97,8 +106,10 @@ static struct argp ruiji_args = { }; + int main(int argc, char *argv[]) { + int exit_code = 0; /* create new arguments struct for ruiji and set them to default values */ struct ruiji_arg_opts arg_opts; set_default_opt(&arg_opts); @@ -106,6 +117,11 @@ int main(int argc, char *argv[]) /* parse given command line arguments */ argp_parse(&ruiji_args, argc, argv, 0, 0, &arg_opts); + if (arg_opts.showversion) { + printf("ruiji-%s\n", VERSION); + return exit_code; + } + /* check if selected image file exists */ FILE *img_fd; char *file_name = arg_opts.file; @@ -176,11 +192,7 @@ int main(int argc, char *argv[]) } else { printf("No results! :(\n"); - /* free up allocated memory */ - free_similar_image_db(&sim_db); - /* clean up curl */ - ruiji_curl_cleanup(); - return 1; + exit_code = 1; } /* free up allocated memory */ @@ -188,7 +200,7 @@ int main(int argc, char *argv[]) /* clean up curl */ ruiji_curl_cleanup(); - return 0; + return exit_code; } /* dl_url = get_image_url(dl_image->link, DANBOORU_SOURCE_ID, DANBOORU_URL, '"');