]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Wrap help options when building help string
authorTom Tromey <tromey@adacore.com>
Mon, 3 Jun 2024 16:06:51 +0000 (10:06 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 11 Nov 2024 14:45:02 +0000 (07:45 -0700)
When building a help string, it's possible that the resulting options
will go over 80 columns.  This patch changes this code to add line
wrapping where needed.

This can most be seen by looking "help bt" and in particular the
"-frame-info" help text.

gdb/cli/cli-decode.h
gdb/cli/cli-option.c

index 7365c3f01573eb50f71b5edd49ccfd8b4e8d6f74..7e25374fc80e42165a77b0e5c93ca98e9ab34a6f 100644 (file)
@@ -29,6 +29,9 @@
 #include "gdbsupport/intrusive_list.h"
 #include "gdbsupport/buildargv.h"
 
+/* The allowed length of a line in a documentation string.  */
+constexpr int cli_help_line_length = 80;
+
 /* Not a set/show command.  Note that some commands which begin with
    "set" or "show" might be in this category, if their syntax does
    not fall into one of the following categories.  */
index 9eb9ff81154569ce5cd52f598d07ea85fc9cb528..fa00b913c99d81608220ab853244989fec511078 100644 (file)
@@ -732,52 +732,71 @@ process_options (const char **args,
     }
 }
 
-/* Helper for build_help.  Return a fragment of a help string showing
-   OPT's possible values.  Returns NULL if OPT doesn't take an
-   argument.  */
+/* Helper for build_help.  Append a fragment of a help string showing
+   OPT's possible values.  LEN_AT_START is the length of HELP at the
+   start of the current line.  This is used when wrapping is
+   needed.  */
 
-static const char *
-get_val_type_str (const option_def &opt, std::string &buffer)
+static void
+append_val_type_str (std::string &help, const option_def &opt,
+                    size_t len_at_start)
 {
   if (!opt.have_argument)
-    return nullptr;
+    return;
 
   switch (opt.type)
     {
     case var_boolean:
-      return "[on|off]";
+      help += " [on|off]";
+      break;
     case var_uinteger:
     case var_integer:
     case var_pinteger:
       {
-       buffer = "NUMBER";
+       help += " NUMBER";
        if (opt.extra_literals != nullptr)
          for (const literal_def *l = opt.extra_literals;
               l->literal != nullptr;
               l++)
            {
-             buffer += '|';
-             buffer += l->literal;
+             help += '|';
+             help += l->literal;
            }
-       return buffer.c_str ();
       }
+      break;
     case var_enum:
       {
-       buffer = "";
+       help += ' ';
+       /* If wrapping is needed, subsequent lines will be indented
+          this amount.  */
+       size_t indent = help.length () - len_at_start;
        for (size_t i = 0; opt.enums[i] != nullptr; i++)
          {
            if (i != 0)
-             buffer += "|";
-           buffer += opt.enums[i];
+             {
+               size_t new_len = help.length () + 1 + strlen (opt.enums[i]);
+
+               if (new_len - len_at_start >= cli_help_line_length)
+                 {
+                   help += "\n";
+                   len_at_start = help.length ();
+
+                   help.append (indent, ' ');
+                 }
+               help += "|";
+             }
+           help += opt.enums[i];
          }
-       return buffer.c_str ();
       }
+      break;
     case var_string:
-      return "STRING";
+      help += "STRING";
+      break;
     case var_filename:
-      return "FILENAME";
+      help += "FILENAME";
+      break;
     default:
-      return nullptr;
+      break;
     }
 }
 
@@ -815,15 +834,11 @@ build_help_option (gdb::array_view<const option_def> options,
       if (o.set_doc == nullptr)
        continue;
 
+      size_t initial_len = help.length ();
       help += "  -";
       help += o.name;
 
-      const char *val_type_str = get_val_type_str (o, buffer);
-      if (val_type_str != nullptr)
-       {
-         help += ' ';
-         help += val_type_str;
-       }
+      append_val_type_str (help, o, initial_len);
       help += "\n";
       append_indented_doc (o.set_doc, help);
       if (o.help_doc != nullptr)