diff-parseopt: convert --stat*
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Feb 21, 2019 at 18:16 UTC
84b5089e41c74ac39c96f49f584541e5e9a94ad1
1 file changed
+52
-66
diff.c
+52
-66
@@ -104,11 +104,6 @@ static const char *color_diff_slots[] = {
104
[DIFF_FILE_NEW_BOLD] = "newBold",
105
};
106
107
-static NORETURN void die_want_option(const char *option_name)
108
-{
109
- die(_("option '%s' requires a value"), option_name);
110
-}
111
-
107
define_list_config_array_extra(color_diff_slots, {"plain"});
108
109
static int parse_diff_color_slot(const char *var)
@@ -4661,77 +4656,56 @@ int parse_long_opt(const char *opt, const char **argv,
4656
return 2;
4657
}
4658
4664
-static int stat_opt(struct diff_options *options, const char **av)
4659
+static int diff_opt_stat(const struct option *opt, const char *value, int unset)
4660
{
4666
- const char *arg = av[0];
4667
- char *end;
4661
+ struct diff_options *options = opt->value;
4662
int width = options->stat_width;
4663
int name_width = options->stat_name_width;
4664
int graph_width = options->stat_graph_width;
4665
int count = options->stat_count;
4672
- int argcount = 1;
4666
+ char *end;
4667
4674
- if (!skip_prefix(arg, "--stat", &arg))
4675
- BUG("stat option does not begin with --stat: %s", arg);
4676
- end = (char *)arg;
4668
+ BUG_ON_OPT_NEG(unset);
4669
4678
- switch (*arg) {
4679
- case '-':
4680
- if (skip_prefix(arg, "-width", &arg)) {
4681
- if (*arg == '=')
4682
- width = strtoul(arg + 1, &end, 10);
4683
- else if (!*arg && !av[1])
4684
- die_want_option("--stat-width");
4685
- else if (!*arg) {
4686
- width = strtoul(av[1], &end, 10);
4687
- argcount = 2;
4688
- }
4689
- } else if (skip_prefix(arg, "-name-width", &arg)) {
4690
- if (*arg == '=')
4691
- name_width = strtoul(arg + 1, &end, 10);
4692
- else if (!*arg && !av[1])
4693
- die_want_option("--stat-name-width");
4694
- else if (!*arg) {
4695
- name_width = strtoul(av[1], &end, 10);
4696
- argcount = 2;
4697
- }
4698
- } else if (skip_prefix(arg, "-graph-width", &arg)) {
4699
- if (*arg == '=')
4700
- graph_width = strtoul(arg + 1, &end, 10);
4701
- else if (!*arg && !av[1])
4702
- die_want_option("--stat-graph-width");
4703
- else if (!*arg) {
4704
- graph_width = strtoul(av[1], &end, 10);
4705
- argcount = 2;
4706
- }
4707
- } else if (skip_prefix(arg, "-count", &arg)) {
4708
- if (*arg == '=')
4709
- count = strtoul(arg + 1, &end, 10);
4710
- else if (!*arg && !av[1])
4711
- die_want_option("--stat-count");
4712
- else if (!*arg) {
4713
- count = strtoul(av[1], &end, 10);
4714
- argcount = 2;
4715
- }
4670
+ if (!strcmp(opt->long_name, "stat")) {
4671
+ if (value) {
4672
+ width = strtoul(value, &end, 10);
4673
+ if (*end == ',')
4674
+ name_width = strtoul(end+1, &end, 10);
4675
+ if (*end == ',')
4676
+ count = strtoul(end+1, &end, 10);
4677
+ if (*end)
4678
+ return error(_("invalid --stat value: %s"), value);
4679
}
4717
- break;
4718
- case '=':
4719
- width = strtoul(arg+1, &end, 10);
4720
- if (*end == ',')
4721
- name_width = strtoul(end+1, &end, 10);
4722
- if (*end == ',')
4723
- count = strtoul(end+1, &end, 10);
4724
- }
4680
+ } else if (!strcmp(opt->long_name, "stat-width")) {
4681
+ width = strtoul(value, &end, 10);
4682
+ if (*end)
4683
+ return error(_("%s expects a numerical value"),
4684
+ opt->long_name);
4685
+ } else if (!strcmp(opt->long_name, "stat-name-width")) {
4686
+ name_width = strtoul(value, &end, 10);
4687
+ if (*end)
4688
+ return error(_("%s expects a numerical value"),
4689
+ opt->long_name);
4690
+ } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4691
+ graph_width = strtoul(value, &end, 10);
4692
+ if (*end)
4693
+ return error(_("%s expects a numerical value"),
4694
+ opt->long_name);
4695
+ } else if (!strcmp(opt->long_name, "stat-count")) {
4696
+ count = strtoul(value, &end, 10);
4697
+ if (*end)
4698
+ return error(_("%s expects a numerical value"),
4699
+ opt->long_name);
4700
+ } else
4701
+ BUG("%s should not get here", opt->long_name);
4702
4726
- /* Important! This checks all the error cases! */
4727
- if (*end)
4728
- return 0;
4703
options->output_format |= DIFF_FORMAT_DIFFSTAT;
4704
options->stat_name_width = name_width;
4705
options->stat_graph_width = graph_width;
4706
options->stat_width = width;
4707
options->stat_count = count;
4734
- return argcount;
4708
+ return 0;
4709
}
4710
4711
static int parse_dirstat_opt(struct diff_options *options, const char *params)
@@ -4958,6 +4932,21 @@ static void prep_parse_options(struct diff_options *options)
4932
OPT_BIT_F(0, "name-status", &options->output_format,
4933
N_("show only names and status of changed files"),
4934
DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
4935
+ OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
4936
+ N_("generate diffstat"),
4937
+ PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
4938
+ OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
4939
+ N_("generate diffstat with a given width"),
4940
+ PARSE_OPT_NONEG, diff_opt_stat),
4941
+ OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
4942
+ N_("generate diffstat with a given name width"),
4943
+ PARSE_OPT_NONEG, diff_opt_stat),
4944
+ OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
4945
+ N_("generate diffstat with a given graph width"),
4946
+ PARSE_OPT_NONEG, diff_opt_stat),
4947
+ OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
4948
+ N_("generate diffstat with limited lines"),
4949
+ PARSE_OPT_NONEG, diff_opt_stat),
4950
OPT_END()
4951
};
4952
@@ -4986,10 +4975,7 @@ int diff_opt_parse(struct diff_options *options,
4975
return ac;
4976
4977
/* Output format options */
4989
- if (starts_with(arg, "--stat"))
4990
- /* --stat, --stat-width, --stat-name-width, or --stat-count */
4991
- return stat_opt(options, av);
4992
- else if (!strcmp(arg, "--compact-summary")) {
4978
+ if (!strcmp(arg, "--compact-summary")) {
4979
options->flags.stat_with_summary = 1;
4980
options->output_format |= DIFF_FORMAT_DIFFSTAT;
4981
} else if (!strcmp(arg, "--no-compact-summary"))