aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2026-07-12 10:57:07 +0200
committerTristan Riehs <tristan.riehs@inria.fr>2026-07-12 10:57:07 +0200
commit19e96d63b29ae56acd201e10bf91f83b5ef884c6 (patch)
tree24c50434fe06954501f131e0c91297453984051a
parent809f3ccb18b38e06b054101bde04316f01fc2b44 (diff)
Use getopt for ftag query
-rw-r--r--src/main.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/src/main.c b/src/main.c
index 58e5837..2857474 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);
}