Skip to contentSkip to conversation

imap-send: Use parse options API to determine verbosity

PATCH Bernhard Reiter 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

---
As requested per <[email protected]>.

 Documentation/git-imap-send.txt | 14 +++++++++++++-
 imap-send.c                     | 25 +++++++++++++++----------
 2 files changed, 28 insertions(+), 11 deletions(-)
Documentation/git-imap-send.txt +13 -1
index c7c0d21..0897131 100644
@@ -9,7 +9,7 @@ git-imap-send - Send a collection of patches from stdin to an IMAP folder
 SYNOPSIS
 --------
 [verse]
-'git imap-send'
+'git imap-send' [-v] [-q]
 
 
 DESCRIPTION
@@ -26,6 +26,18 @@ Typical usage is something like:
 git format-patch --signoff --stdout --attach origin | git imap-send
 
 
+OPTIONS
+-------
+
+-v::
+--verbose::
+	Be verbose.
+
+-q::
+--quiet::
+	Be quiet.
+
+
 CONFIGURATION
 -------------
 
imap-send.c +15 -10
index 70bcc7a..7f40960 100644
@@ -26,11 +26,19 @@
 #include "credential.h"
 #include "exec_cmd.h"
 #include "run-command.h"
+#include "parse-options.h"
 #ifdef NO_OPENSSL
 typedef void *SSL;
 #endif
 
-static const char imap_send_usage[] = "git imap-send < <mbox>";
+static int verbosity;
+
+static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] < <mbox>", NULL };
+
+static struct option imap_send_options[] = {
+	OPT__VERBOSITY(&verbosity),
+	OPT_END()
+};
 
 #undef DRV_OK
 #define DRV_OK          0
@@ -38,8 +46,6 @@ static const char imap_send_usage[] = "git imap-send < <mbox>";
 #define DRV_BOX_BAD     -2
 #define DRV_STORE_BAD   -3
 
-static int Verbose, Quiet;
-
 __attribute__((format (printf, 1, 2)))
 static void imap_info(const char *, ...);
 __attribute__((format (printf, 1, 2)))
@@ -418,7 +424,7 @@ static int buffer_gets(struct imap_buffer *b, char **s)
 			if (b->buf[b->offset + 1] == '\n') {
 				b->buf[b->offset] = 0;  /* terminate the string */
 				b->offset += 2; /* next line */
-				if (Verbose)
+				if (verbosity >= 0)
 					puts(*s);
 				return 0;
 			}
@@ -433,7 +439,7 @@ static void imap_info(const char *msg, ...)
 {
 	va_list va;
 
-	if (!Quiet) {
+	if (verbosity >= 0) {
 		va_start(va, msg);
 		vprintf(msg, va);
 		va_end(va);
@@ -445,7 +451,7 @@ static void imap_warn(const char *msg, ...)
 {
 	va_list va;
 
-	if (Quiet < 2) {
+	if (verbosity < 2) {
 		va_start(va, msg);
 		vfprintf(stderr, msg, va);
 		va_end(va);
@@ -522,7 +528,7 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
 				  cmd->tag, cmd->cmd, cmd->cb.dlen,
 				  CAP(LITERALPLUS) ? "+" : "");
 
-	if (Verbose) {
+	if (verbosity >= 0) {
 		if (imap->num_in_progress)
 			printf("(%d in progress) ", imap->num_in_progress);
 		if (!starts_with(cmd->cmd, "LOGIN"))
@@ -1352,12 +1358,11 @@ int main(int argc, char **argv)
 
 	git_setup_gettext();
 
-	if (argc != 1)
-		usage(imap_send_usage);
-
 	setup_git_directory_gently(&nongit_ok);
 	git_imap_config();
 
+	argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
+
 	if (!server.port)
 		server.port = server.use_ssl ? 993 : 143;
 
Signature · 1 line
2.1.2.557.g06ecad4
#1 lore raw
Quoted 3 lines · Bernhard Reiter

---
As requested per <[email protected]>.

Heh, I didn't quite "request" it, but thanks for a patch anyway.

Quoted 26 lines
imap-send.c +9 -1
index 70bcc7a..7f40960 100644
@@ -26,11 +26,19 @@
 #include "credential.h"
 #include "exec_cmd.h"
 #include "run-command.h"
+#include "parse-options.h"
 #ifdef NO_OPENSSL
 typedef void *SSL;
 #endif
 
-static const char imap_send_usage[] = "git imap-send < <mbox>";
+static int verbosity;
+
+static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] < <mbox>", NULL };
+
+static struct option imap_send_options[] = {
+	OPT__VERBOSITY(&verbosity),
+	OPT_END()
+};
 
 #undef DRV_OK
 #define DRV_OK          0

imap-send.c static const char imap_send_usage[] = "git imap-send < <mbox>"; · line 41

Quoted 4 lines
imap-send.c +0 -1
@@ -38,8 +46,6 @@ static const char imap_send_usage[] = "git imap-send < <mbox>";
 #define DRV_BOX_BAD     -2
 #define DRV_STORE_BAD   -3
 
-static int Verbose, Quiet;

So it used to be Verbose and Quiet both defaulting to 0 (false). Now we have verbosity that defaults to 0, positive telling us to be verbose and negative telling us to be quiet.

imap-send.c static int buffer_gets(struct imap_buffer *b, char **s) · line 428

Quoted 10 lines
imap-send.c +1 -1
 __attribute__((format (printf, 1, 2)))
 static void imap_info(const char *, ...);
 __attribute__((format (printf, 1, 2)))
@@ -418,7 +424,7 @@ static int buffer_gets(struct imap_buffer *b, char **s)
 			if (b->buf[b->offset + 1] == '\n') {
 				b->buf[b->offset] = 0;  /* terminate the string */
 				b->offset += 2; /* next line */
-				if (Verbose)
+				if (verbosity >= 0)
 					puts(*s);

... which means this should be "if (0 < verbosity)", i.e. we do not squeak without "-v", no?

imap-send.c static void imap_info(const char *msg, ...) · line 442

Quoted 6 lines
imap-send.c +1 -1
@@ -433,7 +439,7 @@ static void imap_info(const char *msg, ...)
 {
 	va_list va;
 
-	if (!Quiet) {
+	if (verbosity >= 0) {

This one looks sensible; unless we are told to be "-q"uiet, we used to give this message, and we will continue to do so.

imap-send.c static void imap_warn(const char *msg, ...) · line 457

Quoted 12 lines
imap-send.c +1 -1
 		va_start(va, msg);
 		vprintf(msg, va);
 		va_end(va);
@@ -445,7 +451,7 @@ static void imap_warn(const char *msg, ...)
 {
 	va_list va;
 
-	if (Quiet < 2) {
+	if (verbosity < 2) {
 		va_start(va, msg);
 		vfprintf(stderr, msg, va);
 		va_end(va);

The cut-off value "2" looks strange, but more importantly this conversion looks bogus no matter what the cut-off value is.

"As long as the verbosity is smaller than this value, emit this error message????" That sounds backwards that you do not give the message if you see sufficient number of "-v" options, doesn't it?

I think

	if (-2 < verbosity)

would be in line with what the original logic seems to have wanted, that is "If the caller did not ask us to be extra quiet (i.e. just the regular Quiet=1 does not count), show this message".

imap-send.c static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx, · line 531

Quoted 6 lines
imap-send.c +1 -1
@@ -522,7 +528,7 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
 				  cmd->tag, cmd->cmd, cmd->cb.dlen,
 				  CAP(LITERALPLUS) ? "+" : "");
 
-	if (Verbose) {
+	if (verbosity >= 0) {

Same comment as the one at hunk -418,7 above applies.