git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Gummerer <t.gummerer@gmail.com>
To: git@vger.kernel.org
Cc: peff@peff.net, bturner@atlassian.com, gitster@pobox.com,
	pedrorijo91@gmail.com, Thomas Gummerer <t.gummerer@gmail.com>
Subject: [PATCH v2 4/5] ls-remote: use parse-options api
Date: Mon, 18 Jan 2016 17:57:17 +0100	[thread overview]
Message-ID: <1453136238-19448-5-git-send-email-t.gummerer@gmail.com> (raw)
In-Reply-To: <1453136238-19448-1-git-send-email-t.gummerer@gmail.com>

Currently ls-remote uses a hand rolled parser for the 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>
---
 builtin/ls-remote.c | 82 +++++++++++++++++++----------------------------------
 1 file changed, 29 insertions(+), 53 deletions(-)

diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index db21e52..3a20378 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -3,9 +3,11 @@
 #include "transport.h"
 #include "remote.h"
 
-static const char ls_remote_usage[] =
-"git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
-"                     [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
+static const char * const ls_remote_usage[] = {
+	N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
+	   "                     [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]"),
+	NULL
+};
 
 /*
  * 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)
 
 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 {
-	int i;
 	const char *dest = NULL;
 	unsigned flags = 0;
 	int get_url = 0;
@@ -43,59 +44,34 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	struct transport *transport;
 	const struct ref *ref;
 
-	if (argc == 2 && !strcmp("-h", argv[1]))
-		usage(ls_remote_usage);
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("do not print remote URL")),
+		OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
+			   N_("path of git-upload-pack on the remote host")),
+		{ OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
+			   N_("path of git-upload-pack on the remote host"),
+			   PARSE_OPT_HIDDEN },
+		OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
+		OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
+		OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
+		OPT_BOOL(0, "get-url", &get_url,
+			 N_("take url.<base>.insteadOf into account")),
+		OPT_SET_INT(0, "exit-code", &status,
+			    N_("exit with exit code 2 if no matching refs are found"), 2),
+		OPT_END()
+	};
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
+	argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+	dest = argv[0];
 
-		if (*arg == '-') {
-			if (starts_with(arg, "--upload-pack=")) {
-				uploadpack = arg + 14;
-				continue;
-			}
-			if (starts_with(arg, "--exec=")) {
-				uploadpack = arg + 7;
-				continue;
-			}
-			if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
-				flags |= REF_TAGS;
-				continue;
-			}
-			if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
-				flags |= REF_HEADS;
-				continue;
-			}
-			if (!strcmp("--refs", arg)) {
-				flags |= REF_NORMAL;
-				continue;
-			}
-			if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
-				quiet = 1;
-				continue;
-			}
-			if (!strcmp("--get-url", arg)) {
-				get_url = 1;
-				continue;
-			}
-			if (!strcmp("--exit-code", arg)) {
-				/* return this code if no refs are reported */
-				status = 2;
-				continue;
-			}
-			usage(ls_remote_usage);
-		}
-		dest = arg;
-		i++;
-		break;
+	if (argc > 1) {
+		int i;
+		pattern = xcalloc(argc, sizeof(const char *));
+		for (i = 1; i < argc; i++)
+			pattern[i - 1] = xstrfmt("*/%s", argv[i]);
 	}
 
-	if (argv[i]) {
-		int j;
-		pattern = xcalloc(argc - i + 1, sizeof(const char *));
-		for (j = i; j < argc; j++)
-			pattern[j - i] = xstrfmt("*/%s", argv[j]);
-	}
 	remote = remote_get(dest);
 	if (!remote) {
 		if (dest)
-- 
2.7.0.30.gd0a78e9.dirty

  parent reply	other threads:[~2016-01-18 16:57 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-17 11:03 [PATCH 0/4] ls-remote: introduce symref argument Thomas Gummerer
2016-01-17 11:03 ` [PATCH 1/4] ls-remote: document --quiet option Thomas Gummerer
2016-01-17 14:47   ` Jeff King
2016-01-17 17:13     ` Thomas Gummerer
2016-01-17 11:04 ` [PATCH 2/4] ls-remote: fix synopsis Thomas Gummerer
2016-01-17 11:04 ` [PATCH 3/4] ls-remote: use parse-options api Thomas Gummerer
2016-01-17 14:44   ` Jeff King
2016-01-17 17:27     ` Thomas Gummerer
2016-01-17 11:04 ` [PATCH 4/4] builtin/ls-remote: add support for showing symrefs Thomas Gummerer
2016-01-17 11:16   ` Thomas Gummerer
2016-01-17 11:04 ` [PATCH 4/4] ls-remote: " Thomas Gummerer
2016-01-17 15:15   ` Jeff King
2016-01-17 17:38     ` Thomas Gummerer
2016-01-17 22:14     ` Junio C Hamano
2016-01-17 11:14 ` [PATCH 0/4] ls-remote: introduce symref argument Thomas Gummerer
2016-01-17 15:16 ` Jeff King
2016-01-17 17:39   ` Thomas Gummerer
2016-01-17 22:15   ` Junio C Hamano
2016-01-18 16:57 ` [PATCH v2 0/5] ls-remote: introduce symrefs argument Thomas Gummerer
2016-01-18 16:57   ` [PATCH v2 1/5] ls-remote: document --quiet option Thomas Gummerer
2016-01-18 16:57   ` [PATCH v2 2/5] ls-remote: document --refs option Thomas Gummerer
2016-01-18 19:31     ` Jeff King
2016-01-18 20:01       ` Junio C Hamano
2016-01-18 21:39         ` Thomas Gummerer
2016-01-18 16:57   ` [PATCH v2 3/5] ls-remote: fix synopsis Thomas Gummerer
2016-01-18 16:57   ` Thomas Gummerer [this message]
2016-01-18 19:33     ` [PATCH v2 4/5] ls-remote: use parse-options api Jeff King
2016-01-18 16:57   ` [PATCH v2 5/5] ls-remote: add support for showing symrefs Thomas Gummerer
2016-01-18 19:52     ` Jeff King
2016-01-18 19:53       ` Jeff King
2016-01-18 22:09         ` Thomas Gummerer
2016-01-18 22:09       ` Thomas Gummerer
2016-01-18 22:20         ` Jeff King
2016-01-18 22:35           ` Thomas Gummerer
2016-01-18 20:09     ` Junio C Hamano
2016-01-18 21:48       ` Thomas Gummerer
2016-01-18 19:53   ` [PATCH v2 0/5] ls-remote: introduce symrefs argument Jeff King
2016-01-18 23:20   ` [PATCH v3 0/5] ls-remote: introduce symref argument Thomas Gummerer
2016-01-18 23:20     ` [PATCH v3 1/5] ls-remote: document --quiet option Thomas Gummerer
2016-01-18 23:20     ` [PATCH v3 2/5] ls-remote: document --refs option Thomas Gummerer
2016-01-18 23:20     ` [PATCH v3 3/5] ls-remote: fix synopsis Thomas Gummerer
2016-01-18 23:20     ` [PATCH v3 4/5] ls-remote: use parse-options api Thomas Gummerer
2016-01-18 23:20     ` [PATCH v3 5/5] ls-remote: add support for showing symrefs Thomas Gummerer
2016-01-19 18:14     ` [PATCH v3 0/5] ls-remote: introduce symref argument Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1453136238-19448-5-git-send-email-t.gummerer@gmail.com \
    --to=t.gummerer@gmail.com \
    --cc=bturner@atlassian.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pedrorijo91@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).