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 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 30, 2017 at 01:17 UTC b48cbfc5e6112952bc3be4dea0208bc5e1f331eb
1 file changed +20 -5
help.c
+20 -5
@@ -8,6 +8,7 @@
8 #include "column.h"
9 #include "version.h"
10 #include "refs.h"
11 +#include "parse-options.h"
12
13 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
14 {
@@ -424,16 +425,30 @@ const char *help_unknown_cmd(const char *cmd)
425
426 int cmd_version(int argc, const char **argv, const char *prefix)
427 {
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 +
441 /*
442 * The format of this string should be kept stable for compatibility
443 * with external projects that rely on the output of "git version".
444 + *
445 + * Always show the version, even if other options are given.
446 */
447 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? */
452 }
453 return 0;
454 }