Skip to content

Commit dd9323b

Browse files
sgngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d582992 commit dd9323b

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

builtin/mailinfo.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
#include "utf8.h"
88
#include "strbuf.h"
99
#include "mailinfo.h"
10+
#include "parse-options.h"
1011

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+
};
1317

1418
struct metainfo_charset
1519
{
@@ -21,43 +25,54 @@ struct metainfo_charset
2125
const char *charset;
2226
};
2327

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+
2441
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
2542
{
2643
struct metainfo_charset meta_charset;
2744
struct mailinfo mi;
2845
int status;
2946
char *msgfile, *patchfile;
3047

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+
3169
setup_mailinfo(&mi);
3270
meta_charset.policy = CHARSET_DEFAULT;
3371

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);
5873

59-
if (argc != 3)
60-
usage(mailinfo_usage);
74+
if (argc != 2)
75+
usage_with_options(mailinfo_usage, options);
6176

6277
switch (meta_charset.policy) {
6378
case CHARSET_DEFAULT:
@@ -75,8 +90,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
7590
mi.input = stdin;
7691
mi.output = stdout;
7792

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]);
8095

8196
status = !!mailinfo(&mi, msgfile, patchfile);
8297
clear_mailinfo(&mi);

0 commit comments

Comments
 (0)