git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 03/10] env--helper: write to opt->value in parseopt helper
Date: Wed, 30 Sep 2020 08:28:32 -0400	[thread overview]
Message-ID: <20200930122832.GC1901279@coredump.intra.peff.net> (raw)
In-Reply-To: <20200930122732.GA1901036@coredump.intra.peff.net>

We use OPT_CALLBACK_F() to call the option_parse_type() callback,
passing it the address of "cmdmode" as the value to write to. But the
callback doesn't look at opt->value at all, and instead writes to a
global variable.

This works out because that's the same global variable we happen to pass
in, but it's rather confusing.  Let's use the passed-in value instead.
We'll also make "cmdmode" a local variable of the main function,
ensuring we can't make the same mistake again.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/env--helper.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/builtin/env--helper.c b/builtin/env--helper.c
index 23c214fff6..3aa4282114 100644
--- a/builtin/env--helper.c
+++ b/builtin/env--helper.c
@@ -7,18 +7,20 @@ static char const * const env__helper_usage[] = {
 	NULL
 };
 
-static enum {
+enum cmdmode {
 	ENV_HELPER_TYPE_BOOL = 1,
 	ENV_HELPER_TYPE_ULONG
-} cmdmode = 0;
+};
 
 static int option_parse_type(const struct option *opt, const char *arg,
 			     int unset)
 {
+	enum cmdmode *cmdmode = opt->value;
+
 	if (!strcmp(arg, "bool"))
-		cmdmode = ENV_HELPER_TYPE_BOOL;
+		*cmdmode = ENV_HELPER_TYPE_BOOL;
 	else if (!strcmp(arg, "ulong"))
-		cmdmode = ENV_HELPER_TYPE_ULONG;
+		*cmdmode = ENV_HELPER_TYPE_ULONG;
 	else
 		die(_("unrecognized --type argument, %s"), arg);
 
@@ -33,6 +35,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
 	int ret;
 	int ret_int, default_int;
 	unsigned long ret_ulong, default_ulong;
+	enum cmdmode cmdmode = 0;
 	struct option opts[] = {
 		OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
 			       N_("value is given this type"), PARSE_OPT_NONEG,
-- 
2.28.0.1173.gad90222cf0


  parent reply	other threads:[~2020-09-30 12:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
2020-09-30 12:27 ` [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol() Jeff King
2020-09-30 12:28 ` [PATCH 02/10] drop unused argc parameters Jeff King
2020-09-30 12:28 ` Jeff King [this message]
2020-09-30 12:29 ` [PATCH 04/10] assert PARSE_OPT_NONEG in parse-options callbacks Jeff King
2020-09-30 12:29 ` [PATCH 05/10] push: drop unused repo argument to do_push() Jeff King
2020-09-30 12:29 ` [PATCH 06/10] sequencer: drop repository argument from run_git_commit() Jeff King
2020-09-30 12:30 ` [PATCH 07/10] sparse-checkout: fill in some options boilerplate Jeff King
2020-09-30 12:30 ` [PATCH 08/10] test-advise: check argument count with argc instead of argv Jeff King
2020-09-30 12:34 ` [PATCH 09/10] sequencer: handle ignore_footer when parsing trailers Jeff King
2020-09-30 12:35 ` [PATCH 10/10] dir.c: drop unused "untracked" from treat_path_fast() Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200930122832.GC1901279@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).