diff options
| -rw-r--r-- | src/main.c | 40 |
1 files changed, 21 insertions, 19 deletions
@@ -709,7 +709,8 @@ static void ftag_help(int, char **) static void ftag_query_usage(void) { - puts("usage: ftag query [-a DATE] [-b DATE] [-t TAG]..."); + /* TODO: add details for each option */ + puts("Usage: ftag query [-a DATE] [-b DATE] [-h] [-t TAG]..."); } /* Debugging callback that prints all the output. */ @@ -791,29 +792,27 @@ static void ftag_query(int argc, char **argv) time_t before_date __attribute__((unused)) = 0; char **tags = NULL; int tag_count = 0; - /* TODO: use getopt here */ - while (argc > 0) { - if (strcmp(argv[0], "-a") == 0) { - assert(argc >= 2); - after_date = time_from_str(argv[1]); - } - else if (strcmp(argv[0], "-b") == 0) { - assert(argc >= 2); - before_date = time_from_str(argv[1]); - } - else if (strcmp(argv[0], "-t") == 0) { - assert(argc >= 2); + int opt; + while ((opt = getopt(argc, argv, "a:b:ht:")) != -1) { + switch (opt) { + case 'a': + after_date = time_from_str(optarg); + break; + case 'b': + before_date = time_from_str(optarg); + break; + case 't': tag_count++; tags = realloc(tags, tag_count*sizeof(*tags)); - tags[tag_count-1] = argv[1]; - } - else { - fprintf(stderr, "ftag query: bad option \"%s\"\n", argv[0]); + tags[tag_count-1] = strdup(optarg); + break; + case 'h': + ftag_query_usage(); + exit(EXIT_SUCCESS); + default: ftag_query_usage(); exit(EXIT_FAILURE); } - argc -= 2; - argv += 2; } char sql[4096]; sqlite3 *db; @@ -830,6 +829,9 @@ static void ftag_query(int argc, char **argv) rc = sqlite3_exec(db, sql, print_all_callback, &print_header, NULL); sqlite3_check(rc, db); sqlite3_close(db); + for (int i = 0; i < tag_count; i++) { + free(tags[i]); + } free(tags); } |
