linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chen Yang <chenyang.fnst@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Cc: Chen Yang <chenyang.fnst@cn.fujitsu.com>
Subject: [PATCH 1/2] Btrfs-progs: Improve the command help system of btrfs
Date: Thu, 25 Oct 2012 16:49:08 +0800	[thread overview]
Message-ID: <1351154948-7237-1-git-send-email-chenyang.fnst@cn.fujitsu.com> (raw)

Btrfs's command help system allow you to show only one line short usage.
But in some case we need to use two or more lines to show
the short usage of a complicated command.

This patch add a filed 'lines' in cmd_struct, which is the number of the
extra short usage lines you want to append.

For example you have N lines short usage; you should set the field 'lines'
to N-1.

Signed-off-by: Cheng Yang <chenyang.fnst@cn.fujitsu.com>
---
 commands.h |    4 ++++
 help.c     |   31 ++++++++++++++++++++++---------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/commands.h b/commands.h
index bb6d2dd..f535dc2 100644
--- a/commands.h
+++ b/commands.h
@@ -48,6 +48,9 @@ struct cmd_struct {
 
 	/* if true don't list this token in help listings */
 	int hidden;
+
+	/* number of the extra short usage lines  */
+	int lines;
 };
 
 struct cmd_group {
@@ -70,6 +73,7 @@ int handle_command_group(const struct cmd_group *grp, int argc,
 /* help.c */
 extern const char * const generic_cmd_help_usage[];
 
+void usage_lines(const char * const *usagestr, int lines);
 void usage(const char * const *usagestr);
 void usage_command(const struct cmd_struct *cmd, int full, int err);
 void usage_command_group(const struct cmd_group *grp, int all, int err);
diff --git a/help.c b/help.c
index 6d04293..5679933 100644
--- a/help.c
+++ b/help.c
@@ -28,15 +28,21 @@ extern char argv0_buf[ARGV0_BUF_SIZE];
 #define USAGE_LISTING		8U
 
 static int do_usage_one_command(const char * const *usagestr,
-				unsigned int flags, FILE *outf)
+				unsigned int flags, FILE *outf, int lines)
 {
-	int pad = 4;
+	int pad = 4, i = 0;
 
 	if (!usagestr || !*usagestr)
 		return -1;
 
 	fprintf(outf, "%s%s\n", (flags & USAGE_LISTING) ? "    " : "usage: ",
 		*usagestr++);
+	for (i = 0; i < lines; i++) {
+		fprintf(outf, "%s%s\n",
+			(flags & USAGE_LISTING) ? "    " : "       ",
+			usagestr[i]);
+	}
+	usagestr += lines;
 
 	/* a short one-line description (mandatory) */
 	if ((flags & USAGE_SHORT) == 0)
@@ -79,7 +85,7 @@ static int do_usage_one_command(const char * const *usagestr,
 
 static int usage_command_internal(const char * const *usagestr,
 				  const char *token, int full, int lst,
-				  FILE *outf)
+				  FILE *outf, int lines)
 {
 	unsigned int flags = USAGE_SHORT;
 	int ret;
@@ -89,7 +95,7 @@ static int usage_command_internal(const char * const *usagestr,
 	if (lst)
 		flags |= USAGE_LISTING;
 
-	ret = do_usage_one_command(usagestr, flags, outf);
+	ret = do_usage_one_command(usagestr, flags, outf, lines);
 	switch (ret) {
 	case -1:
 		fprintf(outf, "No usage for '%s'\n", token);
@@ -103,24 +109,31 @@ static int usage_command_internal(const char * const *usagestr,
 }
 
 static void usage_command_usagestr(const char * const *usagestr,
-				   const char *token, int full, int err)
+			   const char *token, int full, int err, int lines)
 {
 	FILE *outf = err ? stderr : stdout;
 	int ret;
 
-	ret = usage_command_internal(usagestr, token, full, 0, outf);
+	ret = usage_command_internal(usagestr, token, full, 0, outf, lines);
 	if (!ret)
 		fputc('\n', outf);
 }
 
 void usage_command(const struct cmd_struct *cmd, int full, int err)
 {
-	usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
+	usage_command_usagestr(
+		cmd->usagestr, cmd->token, full, err, cmd->lines);
 }
 
 void usage(const char * const *usagestr)
 {
-	usage_command_usagestr(usagestr, NULL, 1, 1);
+	usage_lines(usagestr, 0);
+	exit(129);
+}
+
+void usage_lines(const char * const *usagestr, int lines)
+{
+	usage_command_usagestr(usagestr, NULL, 1, 1, lines);
 	exit(129);
 }
 
@@ -144,7 +157,7 @@ static void usage_command_group_internal(const struct cmd_group *grp, int full,
 			}
 
 			usage_command_internal(cmd->usagestr, cmd->token, full,
-					       1, outf);
+					       1, outf, cmd->lines);
 			continue;
 		}
 
-- 
1.7.7.6


                 reply	other threads:[~2012-10-25  8:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1351154948-7237-1-git-send-email-chenyang.fnst@cn.fujitsu.com \
    --to=chenyang.fnst@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).