Skip to content

Commit 6acec03

Browse files
René Scharfegitster
authored andcommitted
parseopt: add OPT_NOOP_NOARG
Add OPT_NOOP_NOARG, a helper macro to define deprecated options in a standard way. The help text is taken from the no-op option -r of git revert. The callback could be made to emit a (conditional?) warning later. And we could also add OPT_NOOP (requiring an argument) etc. as needed. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f858c64 commit 6acec03

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ There are some macros to easily define options:
204204
"auto", set `int_var` to 1 if stdout is a tty or a pager,
205205
0 otherwise.
206206

207+
`OPT_NOOP_NOARG(short, long)`::
208+
Introduce an option that has no effect and takes no arguments.
209+
Use it to hide deprecated options that are still to be recognized
210+
and ignored silently.
211+
207212

208213
The last element of the array must be `OPT_END()`.
209214

parse-options-cb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
123123
string_list_append(v, xstrdup(arg));
124124
return 0;
125125
}
126+
127+
int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
128+
{
129+
return 0;
130+
}

parse-options.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ struct option {
153153
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
154154
parse_opt_color_flag_cb, (intptr_t)"always" }
155155

156+
#define OPT_NOOP_NOARG(s, l) \
157+
{ OPTION_CALLBACK, (s), (l), NULL, NULL, \
158+
"no-op (backward compatibility)", \
159+
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
160+
156161
/* Deprecated synonym */
157162
#define OPT_BOOLEAN OPT_COUNTUP
158163

@@ -216,6 +221,7 @@ extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
216221
extern int parse_opt_with_commit(const struct option *, const char *, int);
217222
extern int parse_opt_tertiary(const struct option *, const char *, int);
218223
extern int parse_opt_string_list(const struct option *, const char *, int);
224+
extern int parse_opt_noop_cb(const struct option *, const char *, int);
219225

220226
#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
221227
#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))

t/t0040-parse-options.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ EOF
8787
test_expect_success 'long options' '
8888
test-parse-options --boolean --integer 1729 --boolean --string2=321 \
8989
--verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
90-
> output 2> output.err &&
90+
--obsolete > output 2> output.err &&
9191
test ! -s output.err &&
9292
test_cmp expect output
9393
'

test-parse-options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int main(int argc, const char **argv)
5454
OPT_STRING(0, "string2", &string, "str", "get another string"),
5555
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
5656
OPT_STRING('o', NULL, &string, "str", "get another string"),
57+
OPT_NOOP_NOARG(0, "obsolete"),
5758
OPT_SET_PTR(0, "default-string", &string,
5859
"set string to default", (unsigned long)"default"),
5960
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),

0 commit comments

Comments
 (0)