Skip to content

Commit

Permalink
Merge branch 'jc/parse-options-boolean'
Browse files Browse the repository at this point in the history
* jc/parse-options-boolean:
  apply: use OPT_NOOP_NOARG
  revert: use OPT_NOOP_NOARG
  parseopt: add OPT_NOOP_NOARG
  archive.c: use OPT_BOOL()
  parse-options: deprecate OPT_BOOLEAN

Conflicts:
	builtin/revert.c
  • Loading branch information
Junio C Hamano committed Oct 12, 2011
2 parents fbca691 + af1032e commit af54383
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 22 deletions.
21 changes: 16 additions & 5 deletions Documentation/technical/api-parse-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ There are some macros to easily define options:
describes the group or an empty string.
Start the description with an upper-case letter.

`OPT_BOOLEAN(short, long, &int_var, description)`::
Introduce a boolean option.
`int_var` is incremented on each use.
`OPT_BOOL(short, long, &int_var, description)`::
Introduce a boolean option. `int_var` is set to one with
`--option` and set to zero with `--no-option`.

`OPT_COUNTUP(short, long, &int_var, description)`::
Introduce a count-up option.
`int_var` is incremented on each use of `--option`, and
reset to zero with `--no-option`.

`OPT_BIT(short, long, &int_var, description, mask)`::
Introduce a boolean option.
Expand All @@ -148,8 +153,9 @@ There are some macros to easily define options:
If used, `int_var` is bitwise-anded with the inverted `mask`.

`OPT_SET_INT(short, long, &int_var, description, integer)`::
Introduce a boolean option.
If used, set `int_var` to `integer`.
Introduce an integer option.
`int_var` is set to `integer` with `--option`, and
reset to zero with `--no-option`.

`OPT_SET_PTR(short, long, &ptr_var, description, ptr)`::
Introduce a boolean option.
Expand Down Expand Up @@ -198,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,
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()`.

Expand Down
4 changes: 2 additions & 2 deletions archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ static int parse_archive_args(int argc, const char **argv,
"prepend prefix to each pathname in the archive"),
OPT_STRING('o', "output", &output, "file",
"write the archive to this file"),
OPT_BOOLEAN(0, "worktree-attributes", &worktree_attributes,
OPT_BOOL(0, "worktree-attributes", &worktree_attributes,
"read .gitattributes in working directory"),
OPT__VERBOSE(&verbose, "report archived files on stderr"),
OPT__COMPR('0', &compression_level, "store only", 0),
Expand All @@ -332,7 +332,7 @@ static int parse_archive_args(int argc, const char **argv,
OPT__COMPR_HIDDEN('8', &compression_level, 8),
OPT__COMPR('9', &compression_level, "compress better", 9),
OPT_GROUP(""),
OPT_BOOLEAN('l', "list", &list,
OPT_BOOL('l', "list", &list,
"list supported archive formats"),
OPT_GROUP(""),
OPT_STRING(0, "remote", &remote, "repo",
Expand Down
9 changes: 2 additions & 7 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -3831,7 +3831,6 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
int i;
int errs = 0;
int is_not_gitdir = !startup_info->have_repository;
int binary;
int force_apply = 0;

const char *whitespace_option = NULL;
Expand All @@ -3850,12 +3849,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
"ignore additions made by the patch"),
OPT_BOOLEAN(0, "stat", &diffstat,
"instead of applying the patch, output diffstat for the input"),
{ OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
NULL, "old option, now no-op",
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
{ OPTION_BOOLEAN, 0, "binary", &binary,
NULL, "old option, now no-op",
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
OPT_NOOP_NOARG(0, "allow-binary-replacement"),
OPT_NOOP_NOARG(0, "binary"),
OPT_BOOLEAN(0, "numstat", &numstat,
"shows number of added and deleted lines in decimal notation"),
OPT_BOOLEAN(0, "summary", &summary,
Expand Down
4 changes: 1 addition & 3 deletions builtin/revert.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
{
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
const char *me = action_name(opts);
int noop;
int reset = 0;
int contin = 0;
struct option options[] = {
OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
OPT_BOOLEAN(0, "continue", &contin, "continue the current operation"),
OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
{ OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)",
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 0 },
OPT_NOOP_NOARG('r', NULL),
OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
Expand Down
5 changes: 5 additions & 0 deletions parse-options-cb.c
Original file line number Diff line number Diff line change
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));
return 0;
}

int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
{
return 0;
}
4 changes: 2 additions & 2 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static int get_value(struct parse_opt_ctx_t *p,
*(int *)opt->value &= ~opt->defval;
return 0;

case OPTION_BOOLEAN:
case OPTION_COUNTUP:
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
return 0;

Expand Down Expand Up @@ -319,7 +319,7 @@ static void parse_options_check(const struct option *opts)
err |= optbug(opts, "uses feature "
"not supported for dashless options");
switch (opts->type) {
case OPTION_BOOLEAN:
case OPTION_COUNTUP:
case OPTION_BIT:
case OPTION_NEGBIT:
case OPTION_SET_INT:
Expand Down
16 changes: 14 additions & 2 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum parse_opt_type {
/* options with no arguments */
OPTION_BIT,
OPTION_NEGBIT,
OPTION_BOOLEAN, /* _INCR would have been a better name */
OPTION_COUNTUP,
OPTION_SET_INT,
OPTION_SET_PTR,
/* options with arguments (usually) */
Expand All @@ -21,6 +21,9 @@ enum parse_opt_type {
OPTION_FILENAME
};

/* Deprecated synonym */
#define OPTION_BOOLEAN OPTION_COUNTUP

enum parse_opt_flags {
PARSE_OPT_KEEP_DASHDASH = 1,
PARSE_OPT_STOP_AT_NON_OPTION = 2,
Expand Down Expand Up @@ -122,10 +125,11 @@ struct option {
PARSE_OPT_NOARG, NULL, (b) }
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG, NULL, (b) }
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, \
#define OPT_COUNTUP(s, l, v, h) { OPTION_COUNTUP, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG }
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG, NULL, (i) }
#define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG, NULL, (p) }
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
Expand All @@ -149,6 +153,13 @@ struct option {
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
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 */
#define OPT_BOOLEAN OPT_COUNTUP

/* parse_options() will filter out the processed options and leave the
* non-option arguments in argv[].
Expand Down Expand Up @@ -210,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_tertiary(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__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))
Expand Down
2 changes: 1 addition & 1 deletion t/t0040-parse-options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ EOF
test_expect_success 'long options' '
test-parse-options --boolean --integer 1729 --boolean --string2=321 \
--verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
> output 2> output.err &&
--obsolete > output 2> output.err &&
test ! -s output.err &&
test_cmp expect output
'
Expand Down
9 changes: 9 additions & 0 deletions t/t5001-archive-attr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ test_expect_missing worktree/ignored
test_expect_exists worktree/ignored-by-tree
test_expect_missing worktree/ignored-by-worktree

test_expect_success 'git archive --worktree-attributes option' '
git archive --worktree-attributes --worktree-attributes HEAD >worktree.tar &&
(mkdir worktree2 && cd worktree2 && "$TAR" xf -) <worktree.tar
'

test_expect_missing worktree2/ignored
test_expect_exists worktree2/ignored-by-tree
test_expect_missing worktree2/ignored-by-worktree

test_expect_success 'git archive vs. bare' '
(cd bare && git archive HEAD) >bare-archive.tar &&
test_cmp archive.tar bare-archive.tar
Expand Down
1 change: 1 addition & 0 deletions test-parse-options.c
Original file line number Diff line number Diff line change
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, "st", &string, "st", "get another string (pervert ordering)"),
OPT_STRING('o', NULL, &string, "str", "get another string"),
OPT_NOOP_NOARG(0, "obsolete"),
OPT_SET_PTR(0, "default-string", &string,
"set string to default", (unsigned long)"default"),
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
Expand Down

0 comments on commit af54383

Please sign in to comment.