Skip to content

Commit b48cbfc

Browse files
peffgitster
authored andcommitted
version: convert to parse-options
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 6b9c38e, 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5a88f97 commit b48cbfc

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

help.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "column.h"
99
#include "version.h"
1010
#include "refs.h"
11+
#include "parse-options.h"
1112

1213
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
1314
{
@@ -424,16 +425,30 @@ const char *help_unknown_cmd(const char *cmd)
424425

425426
int cmd_version(int argc, const char **argv, const char *prefix)
426427
{
428+
int build_options = 0;
429+
const char * const usage[] = {
430+
N_("git version [<options>]"),
431+
NULL
432+
};
433+
struct option options[] = {
434+
OPT_BOOL(0, "build-options", &build_options,
435+
"also print build options"),
436+
OPT_END()
437+
};
438+
439+
argc = parse_options(argc, argv, prefix, options, usage, 0);
440+
427441
/*
428442
* The format of this string should be kept stable for compatibility
429443
* with external projects that rely on the output of "git version".
444+
*
445+
* Always show the version, even if other options are given.
430446
*/
431447
printf("git version %s\n", git_version_string);
432-
while (*++argv) {
433-
if (!strcmp(*argv, "--build-options")) {
434-
printf("sizeof-long: %d\n", (int)sizeof(long));
435-
/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
436-
}
448+
449+
if (build_options) {
450+
printf("sizeof-long: %d\n", (int)sizeof(long));
451+
/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
437452
}
438453
return 0;
439454
}

0 commit comments

Comments
 (0)