git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/8] version: convert to parse-options

From
Jeff King <peff@peff.net>
Date
May 30, 2017, 05:17 UTC
Message-ID
<20170530051742.3j3yn3ipfmyrrteu@sigill.intra.peff.net>
In-Reply-To
<20170530050949.dkgu3u26qj6ycusy@sigill.intra.peff.net>

The "git version" command didn't traditionally accept any options, and in fact ignores any you give it. When we added simple option parsing for "--build-options" in 6b9c38e14, we didn't improve this; we just loop over the arguments and pick out the one we recognize.

Instead, let's move to a real parsing loop, complain about nonsense options, and recognize conventions like "-h".

Signed-off-by: Jeff King <peff@peff.net>
---
I assume nobody was running "git version --foobar" and expecting it to
work. I guess we could also complain about "git version foobar" (no
dashes), but this patch doesn't. Mainly I wanted the auto-generated
options list.
 help.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/help.c b/help.c
index a07f01e6f..41017e88e 100644
--- a/help.c
+++ b/help.c
@@ -8,6 +8,7 @@
 #include "column.h"
 #include "version.h"
 #include "refs.h"
+#include "parse-options.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -424,16 +425,30 @@ const char *help_unknown_cmd(const char *cmd)
 
 int cmd_version(int argc, const char **argv, const char *prefix)
 {
+	int build_options = 0;
+	const char * const usage[] = {
+		N_("git version [<options>]"),
+		NULL
+	};
+	struct option options[] = {
+		OPT_BOOL(0, "build-options", &build_options,
+			 "also print build options"),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, usage, 0);
+
 	/*
 	 * The format of this string should be kept stable for compatibility
 	 * with external projects that rely on the output of "git version".
+	 *
+	 * Always show the version, even if other options are given.
 	 */
 	printf("git version %s\n", git_version_string);
-	while (*++argv) {
-		if (!strcmp(*argv, "--build-options")) {
-			printf("sizeof-long: %d\n", (int)sizeof(long));
-			/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
-		}
+
+	if (build_options) {
+		printf("sizeof-long: %d\n", (int)sizeof(long));
+		/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
 	}
 	return 0;
 }
-- 
2.13.0.613.g11c956365
Previous: Jeff KingNext: Ævar Arnfjörð Bjarmason
Message 11 of 38 in “[Bug] setup_git_env called without repository”
  1. Zero KingMay 29, 2017
  2. Ævar Arnfjörð BjarmasonMay 29, 2017
  3. Jeff KingMay 29, 2017
  4. 0/8 consistent "-h" handling in builtinsJeff King, May 30, 2017
  5. 1/8 am: handle "-h" argument earlierJeff King, May 30, 2017
  6. Junio C HamanoMay 30, 2017
  7. 2/8 credential: handle invalid arguments earlierJeff King, May 30, 2017
  8. 3/8 upload-archive: handle "-h" option earlyJeff King, May 30, 2017
  9. 4/8 remote-{ext,fd}: print usage message on invalid argumentsJeff King, May 30, 2017
  10. 5/8 submodule--helper: show usage for "-h"Jeff King, May 30, 2017
  11. 6/8 version: convert to parse-optionsJeff King, May 30, 2017
  12. version: move --build-options to a test helperÆvar Arnfjörð Bjarmason, May 30, 2017
  13. Jeff KingMay 30, 2017
  14. Johannes SchindelinMay 31, 2017
  15. Jeff KingMay 31, 2017
  16. Johannes SchindelinMay 31, 2017
  17. Ævar Arnfjörð BjarmasonMay 31, 2017
  18. Jeff KingMay 31, 2017
  19. 7/8 git: add hidden --list-builtins optionJeff King, May 30, 2017
  20. 8/8 t0012: test "-h" with builtinsJeff King, May 30, 2017
  21. Junio C HamanoMay 30, 2017
  22. Jeff KingMay 30, 2017
  23. Junio C HamanoMay 30, 2017
  24. Jeff KingMay 30, 2017
  25. Junio C HamanoMay 30, 2017
  26. Jeff KingMay 30, 2017
  27. Jeff KingMay 30, 2017
  28. Junio C HamanoMay 30, 2017
  29. Junio C HamanoJun 1, 2017
  30. Jeff KingJun 1, 2017
  31. Junio C HamanoJun 1, 2017
  32. Junio C HamanoJun 1, 2017
  33. Jeff KingJun 1, 2017
  34. Junio C HamanoJun 1, 2017
  35. Jeff KingJun 1, 2017
  36. Jonathan NiederJun 13, 2017
  37. Jeff KingJun 14, 2017
  38. Junio C HamanoMay 30, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.