public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Mike Frysinger" <vapier@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: /, man/
Date: Mon, 28 Mar 2016 04:53:50 +0000 (UTC)	[thread overview]
Message-ID: <1459096978.f27b1c430a13ed45dba638f2390e6a22f7199f8f.vapier@gentoo> (raw)

commit:     f27b1c430a13ed45dba638f2390e6a22f7199f8f
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Sun Mar 27 16:42:58 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Sun Mar 27 16:42:58 2016 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=f27b1c43

usage: handle optional args, and auto-align display

Since --help is not performance sensitive, add a bit of logic to auto
align the width of the help columns.

 main.c       | 24 ++++++++++++++++++++----
 man/mkman.py |  4 ++--
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/main.c b/main.c
index d366bef..e9bacda 100644
--- a/main.c
+++ b/main.c
@@ -117,7 +117,11 @@ void no_colors(void)
 static void usage(int status, const char *flags, struct option const opts[],
                   const char * const help[], int blabber)
 {
-	unsigned long i;
+	const char opt_arg[] = "[arg]";
+	const char a_arg[] = "<arg>";
+	size_t a_arg_len = strlen(a_arg) + 1;
+	size_t i, optlen;
+
 	if (status != EXIT_SUCCESS)
 		dup2(STDERR_FILENO, STDOUT_FILENO);
 	if (blabber == 0) {
@@ -140,6 +144,15 @@ static void usage(int status, const char *flags, struct option const opts[],
 	if (module_name != NULL)
 		printf("%sLoaded module:%s\n%s%8s%s %s<args>%s\n", GREEN, NORM, YELLOW, module_name, NORM, DKBLUE, NORM);
 
+	/* Prescan the --long opt length to auto-align. */
+	optlen = 0;
+	for (i = 0; opts[i].name; ++i) {
+		size_t l = strlen(opts[i].name);
+		if (opts[i].has_arg != no_argument)
+			l += a_arg_len;
+		optlen = MAX(l, optlen);
+	}
+
 	printf("\n%sOptions:%s -[%s]\n", GREEN, NORM, flags);
 	for (i = 0; opts[i].name; ++i) {
 		/* this assert is a life saver when adding new applets. */
@@ -153,11 +166,14 @@ static void usage(int status, const char *flags, struct option const opts[],
 
 		/* then the long flag + help text */
 		if (opts[i].has_arg == no_argument)
-			printf("--%-15s%s*%s %s\n", opts[i].name,
+			printf("--%-*s %s*%s %s\n", (int)optlen, opts[i].name,
 				RED, NORM, _(help[i]));
 		else
-			printf("--%-8s %s<arg>%s %s*%s %s\n", opts[i].name,
-				DKBLUE, NORM, RED, NORM, _(help[i]));
+			printf("--%s %s%s%s%*s %s*%s %s\n",
+				opts[i].name,
+				DKBLUE, (opts[i].has_arg == a_argument ? a_arg : opt_arg), NORM,
+				(int)(optlen - strlen(opts[i].name) - a_arg_len), "",
+				RED, NORM, _(help[i]));
 	}
 	exit(status);
 }

diff --git a/man/mkman.py b/man/mkman.py
index 1ec8584..0751b92 100755
--- a/man/mkman.py
+++ b/man/mkman.py
@@ -89,8 +89,8 @@ def MkMan(applets, applet, output):
                 flags += [option[0].rstrip(',')]
                 option.pop(0)
 
-            if option[0] == '<arg>':
-                flags = [r'\fB%s\fR \fI<arg>\fR' % x for x in flags]
+            if option[0] in ('<arg>', '[arg]'):
+                flags = [r'\fB%s\fR \fI%s\fR' % (x, option[0]) for x in flags]
                 option.pop(0)
             else:
                 flags = [r'\fB%s\fR' % x for x in flags]


             reply	other threads:[~2016-03-28  4:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-28  4:53 Mike Frysinger [this message]
  -- strict thread matches above, loose matches on Subject: below --
2020-05-02  8:45 [gentoo-commits] proj/portage-utils:master commit in: /, man/ Fabian Groffen
2020-01-06 15:03 Fabian Groffen
2019-12-14 17:01 Fabian Groffen
2019-11-24 12:29 Fabian Groffen
2019-11-09 10:13 Fabian Groffen
2019-10-27 12:22 Fabian Groffen
2019-09-28 13:19 Fabian Groffen
2019-09-10 18:25 Fabian Groffen
2019-07-14 13:09 Fabian Groffen
2019-07-14 10:21 Fabian Groffen
2019-07-14 10:21 Fabian Groffen
2019-07-14 10:21 Fabian Groffen
2019-07-14 10:21 Fabian Groffen
2019-05-20 10:46 Fabian Groffen
2016-02-22 20:37 Mike Frysinger

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=1459096978.f27b1c430a13ed45dba638f2390e6a22f7199f8f.vapier@gentoo \
    --to=vapier@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /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