public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Print --version and --help to stdout
@ 2021-02-21  1:52 Tom Tromey
  2021-02-21  1:58 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2021-02-21  1:52 UTC (permalink / raw)
  To: dwz; +Cc: Tom Tromey

Normall --version and --help output go to stdout and cause a program
to exit with status 0.  The rationale for the exit status is that the
user asked for this behavior, and the program successfully complied.
Printing --help output to stdout is nicer for piping into a pager.

This patch changes dwz to follow this approach.

I kept the program invocation name in the Usage line.  Different
programs seem to handle this differently.
---
 dwz.c | 105 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 52 insertions(+), 53 deletions(-)

diff --git a/dwz.c b/dwz.c
index de432fb..01ad7ef 100644
--- a/dwz.c
+++ b/dwz.c
@@ -16223,20 +16223,20 @@ static struct option_help dwz_misc_options_help[] =
     "Display this information." }
 };
 
-/* Print LEN spaces to stderr.  */
+/* Print LEN spaces to STREAM.  */
 static void
-do_indent (unsigned int len)
+do_indent (FILE *stream, unsigned int len)
 {
   unsigned int i;
 
   for (i = 0; i < len; i++)
-    fprintf (stderr, " ");
+    fprintf (stream, " ");
 }
 
-/* Print MSG to stderr, indenting to INDENT and wrapping at LIMIT.  Assume
-   starting position is at INDENT.  */
+/* Print MSG to STREAM, indenting to INDENT and wrapping at LIMIT.
+   Assume starting position is at INDENT.  */
 static void
-wrap (unsigned int indent, unsigned int limit, const char *msg)
+wrap (FILE *stream, unsigned int indent, unsigned int limit, const char *msg)
 {
   unsigned int len = indent;
   const char *s = msg;
@@ -16253,13 +16253,13 @@ wrap (unsigned int indent, unsigned int limit, const char *msg)
 
       if (len + 1 /* space */ + word_len > limit)
 	{
-	  fprintf (stderr, "\n");
-	  do_indent (indent);
+	  fprintf (stream, "\n");
+	  do_indent (stream ,indent);
 	  len = indent;
 	}
       else if (len > indent)
 	{
-	  fprintf (stderr, " ");
+	  fprintf (stream, " ");
 	  len += 1;
 	}
 
@@ -16267,10 +16267,10 @@ wrap (unsigned int indent, unsigned int limit, const char *msg)
 	{
 	  const char *i;
 	  for (i = s; i < e; ++i)
-	    fprintf (stderr, "%c", *i);
+	    fprintf (stream, "%c", *i);
 	}
       else
-	fprintf (stderr, "%s", s);
+	fprintf (stream, "%s", s);
       len += word_len;
 
       if (e == NULL)
@@ -16280,10 +16280,10 @@ wrap (unsigned int indent, unsigned int limit, const char *msg)
     }
 }
 
-/* Print OPTIONS_HELP of length H to stderr, indenting to help message to
+/* Print OPTIONS_HELP of length H to STREAM, indenting to help message to
    INDENT an wrapping at LIMIT.  */
 static void
-print_options_help (struct option_help *options_help, unsigned int n,
+print_options_help (FILE *stream, struct option_help *options_help, unsigned int n,
 		    unsigned int indent, unsigned int limit)
 {
   unsigned len;
@@ -16294,29 +16294,29 @@ print_options_help (struct option_help *options_help, unsigned int n,
     {
       len = 0;
 
-      fprintf (stderr, "  ");
+      fprintf (stream, "  ");
       len += 2;
 
       s = options_help[i].short_name;
       if (s)
 	{
-	  fprintf (stderr, "-%s", s);
+	  fprintf (stream, "-%s", s);
 	  len += 2;
 	}
 
       s = options_help[i].long_name;
       if (len == 4)
 	{
-	  fprintf (stderr, ", ");
+	  fprintf (stream, ", ");
 	  len += 2;
 	}
-      fprintf (stderr, "--%s", s);
+      fprintf (stream, "--%s", s);
       len += 2 + strlen (s);
 
       s = options_help[i].argument;
       if (s)
 	{
-	  fprintf (stderr, " %s", s);
+	  fprintf (stream, " %s", s);
 	  len += 1 + strlen (s);
 	}
 
@@ -16325,65 +16325,65 @@ print_options_help (struct option_help *options_help, unsigned int n,
 	{
 	  if (len > indent)
 	    {
-	      fprintf (stderr, "\n");
-	      do_indent (indent);
+	      fprintf (stream, "\n");
+	      do_indent (stream, indent);
 	    }
 	  else
-	    do_indent (indent - len);
+	    do_indent (stream, indent - len);
 	  len = indent;
 
-	  wrap (indent, limit, s);
+	  wrap (stream, indent, limit, s);
 	}
-      fprintf (stderr, "\n");
+      fprintf (stream, "\n");
 
       s = options_help[i].default_value;
       if (s)
 	{
-	  do_indent (indent);
-	  fprintf (stderr, "Default value: %s.\n", s);
+	  do_indent (stream, indent);
+	  fprintf (stream, "Default value: %s.\n", s);
 	}
     }
 }
 
 /* Print usage and exit.  */
 static void
-usage (void)
+usage (const char *progname, int failing)
 {
   unsigned int n;
   unsigned int indent, limit;
-  const char *msg;
+  FILE *stream = failing ? stderr : stdout;
 
-  msg
-    = ("Usage:\n"
-       "  dwz [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [FILES]\n"
-       "  dwz [common options] -o OUTFILE FILE\n"
-       "  dwz [ -v | -? ]");
-  error (0, 0, "%s", msg);
+  fprintf (stream,
+	   ("Usage:\n"
+	    "  %s [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [FILES]\n"
+	    "  %s [common options] -o OUTFILE FILE\n"
+	    "  %s [ -v | -? ]\n"),
+	   progname, progname, progname);
 
   indent = 30;
   limit = 80;
-  fprintf (stderr, "Common options:\n");
+  fprintf (stream, "Common options:\n");
   n = (sizeof (dwz_common_options_help)
        / sizeof (dwz_common_options_help[0]));
-  print_options_help (dwz_common_options_help, n, indent, limit);
+  print_options_help (stream, dwz_common_options_help, n, indent, limit);
 
-  fprintf (stderr, "Single-file options:\n");
+  fprintf (stream, "Single-file options:\n");
   n = (sizeof (dwz_single_file_options_help)
        / sizeof (dwz_single_file_options_help[0]));
-  print_options_help (dwz_single_file_options_help, n, indent, limit);
+  print_options_help (stream, dwz_single_file_options_help, n, indent, limit);
 
-  fprintf (stderr, "Multi-file options:\n");
+  fprintf (stream, "Multi-file options:\n");
   n = (sizeof (dwz_multi_file_options_help)
        / sizeof (dwz_multi_file_options_help[0]));
-  print_options_help (dwz_multi_file_options_help, n, indent, limit);
+  print_options_help (stream, dwz_multi_file_options_help, n, indent, limit);
 
-  fprintf (stderr, "Miscellaneous options:\n");
+  fprintf (stream, "Miscellaneous options:\n");
   n = (sizeof (dwz_misc_options_help)
        / sizeof (dwz_misc_options_help[0]));
-  print_options_help (dwz_misc_options_help, n, indent, limit);
+  print_options_help (stream, dwz_misc_options_help, n, indent, limit);
 
 #if DEVEL
-  fprintf (stderr, "Development options:\n");
+  fprintf (stream, "Development options:\n");
   msg
     = ("  --devel-trace\n"
        "  --devel-progress\n"
@@ -16405,24 +16405,23 @@ usage (void)
        "  --devel-deduplication-mode={none,intra-cu,inter-cu}\n"
        "  --devel-uni-lang / --devel-no-uni-lang\n"
        "  --devel-gen-cu / --devel-no-gen-cu\n");
-  fprintf (stderr, "%s", msg);
+  fprintf (stream, "%s", msg);
 #endif
 
-  exit (1);
+  exit (failing);
 }
 
 /* Print version and exit.  */
 static void
 version (void)
 {
-  fprintf (stderr,
-	   "dwz version " DWZ_VERSION "\n"
-	   "Copyright (C) " RH_YEARS " Red Hat, Inc.\n"
-	   "Copyright (C) " FSF_YEARS " Free Software Foundation, Inc.\n"
-	   "Copyright (C) " SUSE_YEARS " SUSE LLC.\n"
-	   "This program is free software; you may redistribute it under the terms of\n"
-	   "the GNU General Public License version 3 or (at your option) any later version.\n"
-	   "This program has absolutely no warranty.\n");
+  printf ("dwz version " DWZ_VERSION "\n"
+	  "Copyright (C) " RH_YEARS " Red Hat, Inc.\n"
+	  "Copyright (C) " FSF_YEARS " Free Software Foundation, Inc.\n"
+	  "Copyright (C) " SUSE_YEARS " SUSE LLC.\n"
+	  "This program is free software; you may redistribute it under the terms of\n"
+	  "the GNU General Public License version 3 or (at your option) any later version.\n"
+	  "This program has absolutely no warranty.\n");
   exit (0);
 }
 
@@ -16451,7 +16450,7 @@ main (int argc, char *argv[])
 	{
 	default:
 	case '?':
-	  usage ();
+	  usage (argv[0], c != '?');
 	  break;
 
 	case 0:
-- 
2.26.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Print --version and --help to stdout
  2021-02-21  1:52 [PATCH] Print --version and --help to stdout Tom Tromey
@ 2021-02-21  1:58 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2021-02-21  1:58 UTC (permalink / raw)
  To: Tom Tromey; +Cc: dwz

Tom> This patch changes dwz to follow this approach.

I forgot to 'make check' and so this fails.
I will update it.

Tom

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-21  1:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-21  1:52 [PATCH] Print --version and --help to stdout Tom Tromey
2021-02-21  1:58 ` Tom Tromey

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 read-only IMAP folder(s) and NNTP newsgroup(s).