mailinfo: stop parsing options manually

In a later change, mailinfo will learn more options, let's switch to our robust parse_options framework before that step. Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Đoàn Trần Công Danh committed May 6, 2021 at 22:02 UTC dd9323b7fbda92b988bbfe835fd75c179a3b4780
1 file changed +45 -30
builtin/mailinfo.c
+45 -30
@@ -7,9 +7,13 @@
7 #include "utf8.h"
8 #include "strbuf.h"
9 #include "mailinfo.h"
10 +#include "parse-options.h"
11
11 -static const char mailinfo_usage[] =
12 - "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
12 +static const char * const mailinfo_usage[] = {
13 + /* TRANSLATORS: keep <> in "<" mail ">" info. */
14 + N_("git mailinfo [<options>] <msg> <patch> < mail >info"),
15 + NULL,
16 +};
17
18 struct metainfo_charset
19 {
@@ -21,6 +25,19 @@ struct metainfo_charset
25 const char *charset;
26 };
27
28 +static int parse_opt_explicit_encoding(const struct option *opt,
29 + const char *arg, int unset)
30 +{
31 + struct metainfo_charset *meta_charset = opt->value;
32 +
33 + BUG_ON_OPT_NEG(unset);
34 +
35 + meta_charset->policy = CHARSET_EXPLICIT;
36 + meta_charset->charset = arg;
37 +
38 + return 0;
39 +}
40 +
41 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
42 {
43 struct metainfo_charset meta_charset;
@@ -28,36 +45,34 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
45 int status;
46 char *msgfile, *patchfile;
47
48 + struct option options[] = {
49 + OPT_BOOL('k', NULL, &mi.keep_subject, N_("keep subject")),
50 + OPT_BOOL('b', NULL, &mi.keep_non_patch_brackets_in_subject,
51 + N_("keep non patch brackets in subject")),
52 + OPT_BOOL('m', "message-id", &mi.add_message_id,
53 + N_("copy Message-ID to the end of commit message")),
54 + OPT_SET_INT_F('u', NULL, &meta_charset.policy,
55 + N_("re-code metadata to i18n.commitEncoding"),
56 + CHARSET_DEFAULT, PARSE_OPT_NONEG),
57 + OPT_SET_INT_F('n', NULL, &meta_charset.policy,
58 + N_("disable charset re-coding of metadata"),
59 + CHARSET_NO_REENCODE, PARSE_OPT_NONEG),
60 + OPT_CALLBACK_F(0, "encoding", &meta_charset, N_("encoding"),
61 + N_("re-code metadata to this encoding"),
62 + PARSE_OPT_NONEG, parse_opt_explicit_encoding),
63 + OPT_BOOL(0, "scissors", &mi.use_scissors, N_("use scissors")),
64 + OPT_HIDDEN_BOOL(0, "inbody-headers", &mi.use_inbody_headers,
65 + N_("use headers in message's body")),
66 + OPT_END()
67 + };
68 +
69 setup_mailinfo(&mi);
70 meta_charset.policy = CHARSET_DEFAULT;
71
34 - while (1 < argc && argv[1][0] == '-') {
35 - if (!strcmp(argv[1], "-k"))
36 - mi.keep_subject = 1;
37 - else if (!strcmp(argv[1], "-b"))
38 - mi.keep_non_patch_brackets_in_subject = 1;
39 - else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
40 - mi.add_message_id = 1;
41 - else if (!strcmp(argv[1], "-u"))
42 - meta_charset.policy = CHARSET_DEFAULT;
43 - else if (!strcmp(argv[1], "-n"))
44 - meta_charset.policy = CHARSET_NO_REENCODE;
45 - else if (starts_with(argv[1], "--encoding=")) {
46 - meta_charset.policy = CHARSET_EXPLICIT;
47 - meta_charset.charset = argv[1] + 11;
48 - } else if (!strcmp(argv[1], "--scissors"))
49 - mi.use_scissors = 1;
50 - else if (!strcmp(argv[1], "--no-scissors"))
51 - mi.use_scissors = 0;
52 - else if (!strcmp(argv[1], "--no-inbody-headers"))
53 - mi.use_inbody_headers = 0;
54 - else
55 - usage(mailinfo_usage);
56 - argc--; argv++;
57 - }
72 + argc = parse_options(argc, argv, prefix, options, mailinfo_usage, 0);
73
59 - if (argc != 3)
60 - usage(mailinfo_usage);
74 + if (argc != 2)
75 + usage_with_options(mailinfo_usage, options);
76
77 switch (meta_charset.policy) {
78 case CHARSET_DEFAULT:
@@ -75,8 +90,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
90 mi.input = stdin;
91 mi.output = stdout;
92
78 - msgfile = prefix_filename(prefix, argv[1]);
79 - patchfile = prefix_filename(prefix, argv[2]);
93 + msgfile = prefix_filename(prefix, argv[0]);
94 + patchfile = prefix_filename(prefix, argv[1]);
95
96 status = !!mailinfo(&mi, msgfile, patchfile);
97 clear_mailinfo(&mi);