cli: convert "notmuch show" to use the new argument parser

Use the new notmuch argument parser to handle arguments in "notmuch
show". There are three minor functional changes:

1) Also set params.raw = TRUE when defaulting to raw format when part
   is requested but format is not specified. This was a bug, and
   --part=0 without --format=raw did not work previously.

2) Set params.decrypt = FALSE if crypto context creation fails.

3) Only use the parameters for the last --format if specified multiple
   times. Previously this could have resulted in a non-working mixture
   of parameters.

Signed-off-by: Jani Nikula <jani@nikula.org>
This commit is contained in:
Jani Nikula 2012-02-06 21:57:22 +02:00 committed by David Bremner
parent c9c5a6f70c
commit 2d09775baf

View file

@ -1014,6 +1014,14 @@ do_show (void *ctx,
return 0; return 0;
} }
enum {
NOTMUCH_FORMAT_NOT_SPECIFIED,
NOTMUCH_FORMAT_JSON,
NOTMUCH_FORMAT_TEXT,
NOTMUCH_FORMAT_MBOX,
NOTMUCH_FORMAT_RAW
};
int int
notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
{ {
@ -1021,92 +1029,94 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_string; char *query_string;
char *opt; int opt_index;
const notmuch_show_format_t *format = &format_text; const notmuch_show_format_t *format = &format_text;
notmuch_show_params_t params; notmuch_show_params_t params = { .part = -1 };
int mbox = 0; int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
int format_specified = 0; notmuch_bool_t verify = FALSE;
int i;
params.entire_thread = FALSE; notmuch_opt_desc_t options[] = {
params.raw = FALSE; { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
params.part = -1; (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
params.cryptoctx = NULL; { "text", NOTMUCH_FORMAT_TEXT },
params.decrypt = FALSE; { "mbox", NOTMUCH_FORMAT_MBOX },
{ "raw", NOTMUCH_FORMAT_RAW },
{ 0, 0 } } },
{ NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },
{ NOTMUCH_OPT_BOOLEAN, &params.entire_thread, "entire-thread", 't', 0 },
{ NOTMUCH_OPT_BOOLEAN, &params.decrypt, "decrypt", 'd', 0 },
{ NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
{ 0, 0, 0, 0, 0 }
};
argc--; argv++; /* skip subcommand argument */ opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) {
for (i = 0; i < argc && argv[i][0] == '-'; i++) { /* diagnostics already printed */
if (strcmp (argv[i], "--") == 0) { return 1;
i++;
break;
} }
if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
opt = argv[i] + sizeof ("--format=") - 1; if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
if (strcmp (opt, "text") == 0) { /* if part was requested and format was not specified, use format=raw */
format = &format_text; if (params.part >= 0)
} else if (strcmp (opt, "json") == 0) { format_sel = NOTMUCH_FORMAT_RAW;
else
format_sel = NOTMUCH_FORMAT_TEXT;
}
switch (format_sel) {
case NOTMUCH_FORMAT_JSON:
format = &format_json; format = &format_json;
params.entire_thread = TRUE; params.entire_thread = TRUE;
} else if (strcmp (opt, "mbox") == 0) { break;
case NOTMUCH_FORMAT_TEXT:
format = &format_text;
break;
case NOTMUCH_FORMAT_MBOX:
if (params.part > 0) {
fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
return 1;
}
format = &format_mbox; format = &format_mbox;
mbox = 1; break;
} else if (strcmp (opt, "raw") == 0) { case NOTMUCH_FORMAT_RAW:
format = &format_raw; format = &format_raw;
/* If --format=raw specified without specifying part, we can only
* output single message, so set part=0 */
if (params.part < 0)
params.part = 0;
params.raw = TRUE; params.raw = TRUE;
} else { break;
fprintf (stderr, "Invalid value for --format: %s\n", opt);
return 1;
}
format_specified = 1;
} else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
params.part = atoi(argv[i] + sizeof ("--part=") - 1);
} else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
params.entire_thread = TRUE;
} else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
(STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
if (params.cryptoctx == NULL) {
#ifdef GMIME_ATLEAST_26
/* TODO: GMimePasswordRequestFunc */
if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))
#else
GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
#endif
fprintf (stderr, "Failed to construct gpg context.\n");
else
g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
#ifndef GMIME_ATLEAST_26
g_object_unref (session);
session = NULL;
#endif
}
if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
params.decrypt = TRUE;
} else {
fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
return 1;
}
} }
argc -= i; if (params.decrypt || verify) {
argv += i; #ifdef GMIME_ATLEAST_26
/* TODO: GMimePasswordRequestFunc */
params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
#else
GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
#endif
if (params.cryptoctx) {
g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
} else {
params.decrypt = FALSE;
fprintf (stderr, "Failed to construct gpg context.\n");
}
#ifndef GMIME_ATLEAST_26
g_object_unref (session);
#endif
}
config = notmuch_config_open (ctx, NULL, NULL); config = notmuch_config_open (ctx, NULL, NULL);
if (config == NULL) if (config == NULL)
return 1; return 1;
query_string = query_string_from_args (ctx, argc, argv); query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
if (query_string == NULL) { if (query_string == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return 1;
} }
if (mbox && params.part > 0) {
fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
return 1;
}
if (*query_string == '\0') { if (*query_string == '\0') {
fprintf (stderr, "Error: notmuch show requires at least one search term.\n"); fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
return 1; return 1;
@ -1123,15 +1133,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
return 1; return 1;
} }
/* if part was requested and format was not specified, use format=raw */
if (params.part >= 0 && !format_specified)
format = &format_raw;
/* If --format=raw specified without specifying part, we can only
* output single message, so set part=0 */
if (params.raw && params.part < 0)
params.part = 0;
if (params.part >= 0) if (params.part >= 0)
return do_show_single (ctx, query, format, &params); return do_show_single (ctx, query, format, &params);
else else