[PATCH v2 2/3] 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 --- notmuch-show.c | 149 1 files changed, 75 insertions(+), 74 deletions(-) diff --git a/notmuch-show.c b/notmuch-show.c index e04b3cc..b358278 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -1049,6 +1049,14 @@ do_show (void *ctx, return 0; } +enum { +NOTMUCH_FORMAT_NOT_SPECIFIED, +NOTMUCH_FORMAT_JSON, +NOTMUCH_FORMAT_TEXT, +NOTMUCH_FORMAT_MBOX, +NOTMUCH_FORMAT_RAW +}; + int notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) { @@ -1056,92 +1064,94 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) notmuch_database_t *notmuch; notmuch_query_t *query; char *query_string; -char *opt; +int opt_index; const notmuch_show_format_t *format = &format_text; -notmuch_show_params_t params; -int mbox = 0; -int format_specified = 0; -int i; +notmuch_show_params_t params = { .part = -1 }; +int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED; +notmuch_bool_t verify = FALSE; + +notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f', + (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON }, + { "text", NOTMUCH_FORMAT_TEXT }, + { "mbox", NOTMUCH_FORMAT_MBOX }, + { "raw", NOTMUCH_FORMAT_RAW }, + { 0, 0 } } }, + { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 }, + { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 }, + { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 }, + { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 }, + { 0, 0, 0, 0, 0 } +}; -params.entire_thread = FALSE; -params.raw = FALSE; -params.part = -1; -params.cryptoctx = NULL; -params.decrypt = FALSE; +opt_index = parse_arguments (argc, argv, options, 1); +if (opt_index < 0) { + /* diagnostics already printed */ + return 1; +} -argc--; argv++; /* skip subcommand argument */ +if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) { + /* if part was requested and format was not specified, use format=raw */ + if (params.part >= 0) + format_sel = NOTMUCH_FORMAT_RAW; + else + format_sel = NOTMUCH_FORMAT_TEXT; +} -for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; - break; +switch (format_sel) { +case NOTMUCH_FORMAT_JSON: + format = &format_json; + params.entire_thread = TRUE; + 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; } - if (STRNCMP_LITERAL (argv[i], "--format=") == 0) { - opt = argv[i] + sizeof ("--format=") - 1; - if (strcmp (opt, "text") == 0) { - format = &format_text; - } else if (strcmp (opt, "json") == 0) { - format = &format_json; - params.entire_thread = TRUE; - } else if (strcmp (opt, "mbox") == 0) { - format = &format_mbox; - mbox = 1; - } else if (strcmp (opt, "raw") == 0) { - format = &format_raw; - params.raw = TRUE; - } else { - 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) { + format = &format_mbox; + break; +case NOTMUCH_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; +
[PATCH v2 2/3] 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 --- notmuch-show.c | 149 1 files changed, 75 insertions(+), 74 deletions(-) diff --git a/notmuch-show.c b/notmuch-show.c index e04b3cc..b358278 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -1049,6 +1049,14 @@ do_show (void *ctx, return 0; } +enum { +NOTMUCH_FORMAT_NOT_SPECIFIED, +NOTMUCH_FORMAT_JSON, +NOTMUCH_FORMAT_TEXT, +NOTMUCH_FORMAT_MBOX, +NOTMUCH_FORMAT_RAW +}; + int notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) { @@ -1056,92 +1064,94 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) notmuch_database_t *notmuch; notmuch_query_t *query; char *query_string; -char *opt; +int opt_index; const notmuch_show_format_t *format = &format_text; -notmuch_show_params_t params; -int mbox = 0; -int format_specified = 0; -int i; +notmuch_show_params_t params = { .part = -1 }; +int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED; +notmuch_bool_t verify = FALSE; + +notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f', + (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON }, + { "text", NOTMUCH_FORMAT_TEXT }, + { "mbox", NOTMUCH_FORMAT_MBOX }, + { "raw", NOTMUCH_FORMAT_RAW }, + { 0, 0 } } }, + { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 }, + { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 }, + { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 }, + { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 }, + { 0, 0, 0, 0, 0 } +}; -params.entire_thread = FALSE; -params.raw = FALSE; -params.part = -1; -params.cryptoctx = NULL; -params.decrypt = FALSE; +opt_index = parse_arguments (argc, argv, options, 1); +if (opt_index < 0) { + /* diagnostics already printed */ + return 1; +} -argc--; argv++; /* skip subcommand argument */ +if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) { + /* if part was requested and format was not specified, use format=raw */ + if (params.part >= 0) + format_sel = NOTMUCH_FORMAT_RAW; + else + format_sel = NOTMUCH_FORMAT_TEXT; +} -for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; - break; +switch (format_sel) { +case NOTMUCH_FORMAT_JSON: + format = &format_json; + params.entire_thread = TRUE; + 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; } - if (STRNCMP_LITERAL (argv[i], "--format=") == 0) { - opt = argv[i] + sizeof ("--format=") - 1; - if (strcmp (opt, "text") == 0) { - format = &format_text; - } else if (strcmp (opt, "json") == 0) { - format = &format_json; - params.entire_thread = TRUE; - } else if (strcmp (opt, "mbox") == 0) { - format = &format_mbox; - mbox = 1; - } else if (strcmp (opt, "raw") == 0) { - format = &format_raw; - params.raw = TRUE; - } else { - 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) { + format = &format_mbox; + break; +case NOTMUCH_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; +