ls-remote: use parse-options api

Currently ls-remote uses a hand rolled parser for its command line arguments. Use the parse-options api instead of the hand rolled parser to simplify the code and make it easier to add new arguments. In addition this improves the help message. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jan 19, 2016 at 00:20 UTC ba5f28bf79ea652fbe2be12f5ca6b351bb6ad591
1 file changed +29 -53
builtin/ls-remote.c
+29 -53
@@ -3,9 +3,11 @@
3 #include "transport.h"
4 #include "remote.h"
5
6 -static const char ls_remote_usage[] =
7 -"git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8 -" [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
6 +static const char * const ls_remote_usage[] = {
7 + N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8 + " [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]"),
9 + NULL
10 +};
11
12 /*
13 * Is there one among the list of patterns that match the tail part
@@ -30,7 +32,6 @@ static int tail_match(const char **pattern, const char *path)
32
33 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
34 {
33 - int i;
35 const char *dest = NULL;
36 unsigned flags = 0;
37 int get_url = 0;
@@ -43,59 +44,34 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
44 struct transport *transport;
45 const struct ref *ref;
46
46 - if (argc == 2 && !strcmp("-h", argv[1]))
47 - usage(ls_remote_usage);
47 + struct option options[] = {
48 + OPT__QUIET(&quiet, N_("do not print remote URL")),
49 + OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
50 + N_("path of git-upload-pack on the remote host")),
51 + { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
52 + N_("path of git-upload-pack on the remote host"),
53 + PARSE_OPT_HIDDEN },
54 + OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
55 + OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
56 + OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
57 + OPT_BOOL(0, "get-url", &get_url,
58 + N_("take url.<base>.insteadOf into account")),
59 + OPT_SET_INT(0, "exit-code", &status,
60 + N_("exit with exit code 2 if no matching refs are found"), 2),
61 + OPT_END()
62 + };
63
49 - for (i = 1; i < argc; i++) {
50 - const char *arg = argv[i];
64 + argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
65 + PARSE_OPT_STOP_AT_NON_OPTION);
66 + dest = argv[0];
67
52 - if (*arg == '-') {
53 - if (starts_with(arg, "--upload-pack=")) {
54 - uploadpack = arg + 14;
55 - continue;
56 - }
57 - if (starts_with(arg, "--exec=")) {
58 - uploadpack = arg + 7;
59 - continue;
60 - }
61 - if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
62 - flags |= REF_TAGS;
63 - continue;
64 - }
65 - if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
66 - flags |= REF_HEADS;
67 - continue;
68 - }
69 - if (!strcmp("--refs", arg)) {
70 - flags |= REF_NORMAL;
71 - continue;
72 - }
73 - if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
74 - quiet = 1;
75 - continue;
76 - }
77 - if (!strcmp("--get-url", arg)) {
78 - get_url = 1;
79 - continue;
80 - }
81 - if (!strcmp("--exit-code", arg)) {
82 - /* return this code if no refs are reported */
83 - status = 2;
84 - continue;
85 - }
86 - usage(ls_remote_usage);
87 - }
88 - dest = arg;
89 - i++;
90 - break;
68 + if (argc > 1) {
69 + int i;
70 + pattern = xcalloc(argc, sizeof(const char *));
71 + for (i = 1; i < argc; i++)
72 + pattern[i - 1] = xstrfmt("*/%s", argv[i]);
73 }
74
93 - if (argv[i]) {
94 - int j;
95 - pattern = xcalloc(argc - i + 1, sizeof(const char *));
96 - for (j = i; j < argc; j++)
97 - pattern[j - i] = xstrfmt("*/%s", argv[j]);
98 - }
75 remote = remote_get(dest);
76 if (!remote) {
77 if (dest)