[PATCH 4/6] cli: notmuch count: use getopt_long for parsing command line options
Jani Nikula
jani at nikula.org
Sun Nov 13 14:47:24 PST 2011
Signed-off-by: Jani Nikula <jani at nikula.org>
---
notmuch-count.c | 33 ++++++++++++++++++---------------
1 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/notmuch-count.c b/notmuch-count.c
index 20ce334..4987ca8 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -28,34 +28,37 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
notmuch_database_t *notmuch;
notmuch_query_t *query;
char *query_str;
- int i;
notmuch_bool_t output_messages = TRUE;
- argc--; argv++; /* skip subcommand argument */
+ while (1) {
+ int opt;
+ static struct option options[] = {
+ { "output", required_argument, NULL, 0 },
+ { NULL, 0, NULL, 0 },
+ };
- for (i = 0; i < argc && argv[i][0] == '-'; i++) {
- if (strcmp (argv[i], "--") == 0) {
- i++;
+ opt = getopt_long (argc, argv, "", options, NULL);
+ if (opt == -1)
break;
- }
- if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
- const char *opt = argv[i] + sizeof ("--output=") - 1;
- if (strcmp (opt, "threads") == 0) {
+
+ switch (opt) {
+ case 0:
+ if (strcmp (optarg, "threads") == 0) {
output_messages = FALSE;
- } else if (strcmp (opt, "messages") == 0) {
+ } else if (strcmp (optarg, "messages") == 0) {
output_messages = TRUE;
} else {
- fprintf (stderr, "Invalid value for --output: %s\n", opt);
+ fprintf (stderr, "Invalid value for --output: %s\n", optarg);
return 1;
}
- } else {
- fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+ break;
+ case '?':
return 1;
}
}
- argc -= i;
- argv += i;
+ argc -= optind;
+ argv += optind;
config = notmuch_config_open (ctx, NULL, NULL);
if (config == NULL)
--
1.7.5.4
More information about the notmuch
mailing list