parse-options: automatically infer PARSE_OPT_LITERAL_ARGHELP

Parseopt wraps argument help strings in a pair of angular brackets by default, to tell users that they need to replace it with an actual value. This is useful in most cases, because most option arguments are indeed single values of a certain type. The option PARSE_OPT_LITERAL_ARGHELP needs to be used in option definitions with arguments that have multiple parts or are literal strings. Stop adding these angular brackets if special characters are present, as they indicate that we don't deal with a simple placeholder. This simplifies the code a bit and makes defining special options slightly easier. Remove the flag PARSE_OPT_LITERAL_ARGHELP in the cases where the new and more cautious handling suffices. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 2, 2018 at 21:18 UTC 5f0df44cd79a953c287ccb598896ea7aaa2cc9e3
9 files changed +12 -15
builtin/add.c
+2 -3
@@ -306,9 +306,8 @@ static struct option builtin_add_options[] = {
306 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
307 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
308 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
309 - { OPTION_STRING, 0, "chmod", &chmod_arg, "(+|-)x",
310 - N_("override the executable bit of the listed files"),
311 - PARSE_OPT_LITERAL_ARGHELP },
309 + OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
310 + N_("override the executable bit of the listed files")),
311 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
312 N_("warn when adding an embedded repository")),
313 OPT_END(),
builtin/pack-objects.c
+1 -1
@@ -2943,7 +2943,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2943 N_("similar to --all-progress when progress meter is shown")),
2944 { OPTION_CALLBACK, 0, "index-version", NULL, N_("<version>[,<offset>]"),
2945 N_("write the pack index file in the specified idx format version"),
2946 - PARSE_OPT_LITERAL_ARGHELP, option_parse_index_version },
2946 + 0, option_parse_index_version },
2947 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
2948 N_("maximum size of each output pack file")),
2949 OPT_BOOL(0, "local", &local,
builtin/read-tree.c
+1 -1
@@ -134,7 +134,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
134 N_("same as -m, but discard unmerged entries")),
135 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
136 N_("read the tree into the index under <subdirectory>/"),
137 - PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
137 + PARSE_OPT_NONEG },
138 OPT_BOOL('u', NULL, &opts.update,
139 N_("update working tree with merge result")),
140 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
builtin/send-pack.c
+1 -2
@@ -179,8 +179,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
179 { OPTION_CALLBACK,
180 0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
181 N_("require old value of ref to be at this value"),
182 - PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
183 - parseopt_push_cas_option },
182 + PARSE_OPT_OPTARG, parseopt_push_cas_option },
183 OPT_END()
184 };
185
builtin/shortlog.c
+1 -2
@@ -268,8 +268,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
268 OPT_BOOL('e', "email", &log.email,
269 N_("Show the email address of each author")),
270 { OPTION_CALLBACK, 'w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
271 - N_("Linewrap output"),
272 - PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
271 + N_("Linewrap output"), PARSE_OPT_OPTARG,
272 &parse_wrap_args },
273 OPT_END(),
274 };
builtin/show-branch.c
+1 -1
@@ -655,7 +655,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
655 { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"),
656 N_("show <n> most recent ref-log entries starting at "
657 "base"),
658 - PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
658 + PARSE_OPT_OPTARG,
659 parse_reflog_param },
660 OPT_END()
661 };
builtin/update-index.c
+1 -1
@@ -972,7 +972,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
972 (parse_opt_cb *) cacheinfo_callback},
973 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+|-)x",
974 N_("override the executable bit of the listed files"),
975 - PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
975 + PARSE_OPT_NONEG,
976 chmod_callback},
977 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
978 N_("mark files as \"not changing\""),
builtin/write-tree.c
+2 -3
@@ -24,9 +24,8 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
24 struct option write_tree_options[] = {
25 OPT_BIT(0, "missing-ok", &flags, N_("allow missing objects"),
26 WRITE_TREE_MISSING_OK),
27 - { OPTION_STRING, 0, "prefix", &prefix, N_("<prefix>/"),
28 - N_("write tree object for a subdirectory <prefix>") ,
29 - PARSE_OPT_LITERAL_ARGHELP },
27 + OPT_STRING(0, "prefix", &prefix, N_("<prefix>/"),
28 + N_("write tree object for a subdirectory <prefix>")),
29 { OPTION_BIT, 0, "ignore-cache-tree", &flags, NULL,
30 N_("only useful for debugging"),
31 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, NULL,
parse-options.c
+2 -1
@@ -562,7 +562,8 @@ int parse_options(int argc, const char **argv, const char *prefix,
562 static int usage_argh(const struct option *opts, FILE *outfile)
563 {
564 const char *s;
565 - int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
565 + int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
566 + !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
567 if (opts->flags & PARSE_OPT_OPTARG)
568 if (opts->long_name)
569 s = literal ? "[=%s]" : "[=<%s>]";