|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <git2.h> |
| 10 | +#include "cli.h" |
| 11 | + |
| 12 | +static int show_version = 0; |
| 13 | + |
| 14 | +static const cli_opt_spec common_opts[] = { |
| 15 | + { CLI_OPT_TYPE_SWITCH, "version", 0, &show_version, 1, |
| 16 | + CLI_OPT_USAGE_DEFAULT, NULL, "display the version" }, |
| 17 | + { 0 } |
| 18 | +}; |
| 19 | + |
| 20 | +int main(int argc, char **argv) |
| 21 | +{ |
| 22 | + cli_opt_parser optparser; |
| 23 | + cli_opt opt; |
| 24 | + int ret = 0; |
| 25 | + |
| 26 | + if (git_libgit2_init() < 0) { |
| 27 | + cli_error("failed to initialize libgit2"); |
| 28 | + exit(CLI_EXIT_GIT); |
| 29 | + } |
| 30 | + |
| 31 | + cli_opt_parser_init(&optparser, common_opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU); |
| 32 | + |
| 33 | + /* Parse the top-level (common) options and command information */ |
| 34 | + while (cli_opt_parser_next(&opt, &optparser)) { |
| 35 | + if (!opt.spec) { |
| 36 | + cli_opt_status_fprint(stderr, PROGRAM_NAME, &opt); |
| 37 | + cli_opt_usage_fprint(stderr, PROGRAM_NAME, common_opts); |
| 38 | + ret = CLI_EXIT_USAGE; |
| 39 | + goto done; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (show_version) { |
| 44 | + printf("%s version %s\n", PROGRAM_NAME, LIBGIT2_VERSION); |
| 45 | + goto done; |
| 46 | + } |
| 47 | + |
| 48 | +done: |
| 49 | + git_libgit2_shutdown(); |
| 50 | + return ret; |
| 51 | +} |
0 commit comments