Skip to content

Commit

Permalink
parseopt: add OPT_NOOP_NOARG
Browse files Browse the repository at this point in the history
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 <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and gitster committed Sep 28, 2011
1 parent f858c64 commit 6acec03
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Documentation/technical/api-parse-options.txt
Expand Up @@ -204,6 +204,11 @@ There are some macros to easily define options:
"auto", set `int_var` to 1 if stdout is a tty or a pager, "auto", set `int_var` to 1 if stdout is a tty or a pager,
0 otherwise. 0 otherwise.


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



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


Expand Down
5 changes: 5 additions & 0 deletions parse-options-cb.c
Expand Up @@ -123,3 +123,8 @@ int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
string_list_append(v, xstrdup(arg)); string_list_append(v, xstrdup(arg));
return 0; return 0;
} }

int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
{
return 0;
}
6 changes: 6 additions & 0 deletions parse-options.h
Expand Up @@ -153,6 +153,11 @@ struct option {
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \ { OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
parse_opt_color_flag_cb, (intptr_t)"always" } parse_opt_color_flag_cb, (intptr_t)"always" }


#define OPT_NOOP_NOARG(s, l) \
{ OPTION_CALLBACK, (s), (l), NULL, NULL, \
"no-op (backward compatibility)", \
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }

/* Deprecated synonym */ /* Deprecated synonym */
#define OPT_BOOLEAN OPT_COUNTUP #define OPT_BOOLEAN OPT_COUNTUP


Expand Down Expand Up @@ -216,6 +221,7 @@ extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
extern int parse_opt_with_commit(const struct option *, const char *, int); extern int parse_opt_with_commit(const struct option *, const char *, int);
extern int parse_opt_tertiary(const struct option *, const char *, int); extern int parse_opt_tertiary(const struct option *, const char *, int);
extern int parse_opt_string_list(const struct option *, const char *, int); extern int parse_opt_string_list(const struct option *, const char *, int);
extern int parse_opt_noop_cb(const struct option *, const char *, int);


#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h)) #define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h)) #define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))
Expand Down
2 changes: 1 addition & 1 deletion t/t0040-parse-options.sh
Expand Up @@ -87,7 +87,7 @@ EOF
test_expect_success 'long options' ' test_expect_success 'long options' '
test-parse-options --boolean --integer 1729 --boolean --string2=321 \ test-parse-options --boolean --integer 1729 --boolean --string2=321 \
--verbose --verbose --no-dry-run --abbrev=10 --file fi.le\ --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
> output 2> output.err && --obsolete > output 2> output.err &&
test ! -s output.err && test ! -s output.err &&
test_cmp expect output test_cmp expect output
' '
Expand Down
1 change: 1 addition & 0 deletions test-parse-options.c
Expand Up @@ -54,6 +54,7 @@ int main(int argc, const char **argv)
OPT_STRING(0, "string2", &string, "str", "get another string"), OPT_STRING(0, "string2", &string, "str", "get another string"),
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
OPT_STRING('o', NULL, &string, "str", "get another string"), OPT_STRING('o', NULL, &string, "str", "get another string"),
OPT_NOOP_NOARG(0, "obsolete"),
OPT_SET_PTR(0, "default-string", &string, OPT_SET_PTR(0, "default-string", &string,
"set string to default", (unsigned long)"default"), "set string to default", (unsigned long)"default"),
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"), OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
Expand Down

0 comments on commit 6acec03

Please sign in to comment.