parse-options API: simplify OPT_ALIAS()
PATCH 1/1 parse-options API: simplify OPT_ALIAS()
Remove OPT_ALIAS() while satisfying the use-case that brought it into existence. This is mostly a code removal, but also a code simplification.
The reason we've got OPT_ALIAS is detailed in5c387428f10 (parse-options: don't emit "ambiguous option" for aliases, 2019-04-29), i.e. wanting to have "git clone" understand the incomplete "--recurs" form without complaining that "--recursive" and "--recurse-submodules" are ambiguous.
Only the "clone" caller cared about that use-case, the other four, including two recently added by me in 98e2d9d6f7d (upload-pack:
document and rename --advertise-refs, 2021-08-05), only cared about the small amount of typing saved by re-declaring the relevant OPT_*() argument again with mostly the same parameters.
For the more verbose caller in "clone" let's provide a macro inline to clearly show what is and isn't the same, and for the rest duplicate the definition inline. The description of the aliased options was "alias of --%s", which is being carried forward here.
--- builtin/clone.c | 12 ++-- builtin/log.c | 2 +- builtin/receive-pack.c | 2 +- builtin/upload-pack.c | 2 +- parse-options.c | 111 +--------------------------------- parse-options.h | 6 +- t/helper/test-parse-options.c | 3 +- 7 files changed, 17 insertions(+), 121 deletions(-)
builtin/clone.c +8 -4
@@ -107,10 +107,14 @@ static struct option builtin_clone_options[] = { N_("don't use local hardlinks, always copy")),OPT_BOOL ('s' ,"shared" , &option_shared,N_ ("setup as shared repository" )), - { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules, -N_ ("pathspec" ),N_ ("initialize submodules in the clone" ), - PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." }, -OPT_ALIAS (0 ,"recursive" ,"recurse-submodules" ), +#define OPT_RECURSE_F(l, d, f) \ + { OPTION_CALLBACK, 0, (l), &option_recurse_submodules, \ +N_ ("pathspec" ), (d), \ + PARSE_OPT_OPTARG | (f), recurse_submodules_cb, (intptr_t)"." } +OPT_RECURSE_F ("recurse-submodules" , +N_ ("initialize submodules in the clone" ),0 ), +OPT_RECURSE_F ("recursive" ,N_ ("alias of --recurse-submodules" ), +PARSE_OPT_AMBIGUOUS_ALIAS ),OPT_INTEGER ('j' ,"jobs" , &max_jobs,N_ ("number of submodules cloned in parallel" )), OPT_STRING(0, "template", &option_template, N_("template-directory"),
builtin/log.c +1 -1
@@ -179,7 +179,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,OPT__QUIET (&quiet,N_ ("suppress diff output" )),OPT_BOOL (0 ,"source" , &source,N_ ("show source" )),OPT_BOOL (0 ,"use-mailmap" , &mailmap,N_ ("use mail map file" )), -OPT_ALIAS (0 ,"mailmap" ,"use-mailmap" ), +OPT_BOOL (0 ,"mailmap" , &mailmap,N_ ("alias of --use-mailmap" )),OPT_STRING_LIST (0 ,"decorate-refs" , &decorate_refs_include,N_ ("pattern" ),N_ ("only decorate refs that match <pattern>" )), OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
builtin/receive-pack.c +1 -1
@@ -2480,7 +2480,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)OPT__QUIET (&quiet,N_ ("quiet" )),OPT_HIDDEN_BOOL (0 ,"stateless-rpc" , &stateless_rpc,NULL ),OPT_HIDDEN_BOOL (0 ,"http-backend-info-refs" , &advertise_refs,NULL ), -OPT_ALIAS (0 ,"advertise-refs" ,"http-backend-info-refs" ), +OPT_HIDDEN_BOOL (0 ,"advertise-refs" , &advertise_refs,NULL ),OPT_HIDDEN_BOOL (0 ,"reject-thin-pack-for-testing" , &reject_thin,NULL ), OPT_END() };
builtin/upload-pack.c +1 -1
@@ -24,7 +24,7 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix) N_("quit after a single request/response exchange")),OPT_HIDDEN_BOOL (0 ,"http-backend-info-refs" , &advertise_refs,N_ ("serve up the info/refs for git-http-backend" )), -OPT_ALIAS (0 ,"advertise-refs" ,"http-backend-info-refs" ), +OPT_HIDDEN_BOOL (0 ,"advertise-refs" , &advertise_refs,NULL ),OPT_BOOL (0 ,"strict" , &strict,N_ ("do not try <directory>/.git/ if <directory> is no Git directory" )), OPT_INTEGER(0, "timeout", &timeout,
parse-options.c +3 -108
158 lines not on this page · raw message
parse-options.h +1 -5
@@ -10,7 +10,6 @@ enum parse_opt_type {OPTION_END ,OPTION_GROUP ,OPTION_NUMBER , -OPTION_ALIAS ,/* options with no arguments */ OPTION_BIT , OPTION_NEGBIT,
@@ -44,7 +43,7 @@ enum parse_opt_option_flags {PARSE_OPT_LASTARG_DEFAULT =1 <<4 ,PARSE_OPT_NODASH =1 <<5 ,PARSE_OPT_LITERAL_ARGHELP =1 <<6 , -PARSE_OPT_FROM_ALIAS =1 <<7 , +PARSE_OPT_AMBIGUOUS_ALIAS =1 <<7 ,PARSE_OPT_NOCOMPLETE =1 <<9 ,PARSE_OPT_COMP_ARG =1 <<10 , PARSE_OPT_CMDMODE = 1 << 11,
@@ -197,9 +196,6 @@ struct option {N_ ("no-op (backward compatibility)" ), \ PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb } -#define OPT_ALIAS (s, l, source_long_name) \ - { OPTION_ALIAS, (s), (l), (source_long_name) } -/* * parse_options() will filter out the processed options and leave the * non-option arguments in argv[]. argv0 is assumed program name and
t/helper/test-parse-options.c +2 -1
@@ -153,7 +153,8 @@ int cmd__parse_options(int argc, const char **argv) collect_expect),OPT_GROUP ("Alias" ),OPT_STRING ('A' ,"alias-source" , &string,"string" ,"get a string" ), -OPT_ALIAS ('Z' ,"alias-target" ,"alias-source" ), + { OPTION_STRING, 'Z', "alias-target", &string, "string", + "alias of --alias-source", PARSE_OPT_AMBIGUOUS_ALIAS }, OPT_END(), };int i;
Signature · 1 line
2.34.0.rc2.809.g11e21d44b24