Skip to content

Commit

Permalink
Support for markdown output in help messages
Browse files Browse the repository at this point in the history
This functionality can be used to provide on-line versions of the help
messages for alire.ada.dev.

The help is generated in a single markdown page with appropiate
section titles, so it can have a table of contents.
  • Loading branch information
mgrojo committed Sep 23, 2023
1 parent 2e3a520 commit c47044a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
64 changes: 64 additions & 0 deletions src/alr/alr-commands-dev.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
with Ada.Strings.Unbounded;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;

with CLIC.Formatter;

with Alire.Selftest;

package body Alr.Commands.Dev is
Expand All @@ -24,6 +27,58 @@ package body Alr.Commands.Dev is
Trace.Always (Encode ("ⓘ✓"));
end Print_UTF_8_Sequence;

-------------------
-- Print_MD_Help --
-------------------

procedure Print_MD_Help is

use Ada.Strings.Unbounded;

Last_Group : Unbounded_String;

procedure Put_MD_Command
(Group : Unbounded_String;
Cmd : not null CLIC.Subcommand.Command_Access) is
begin

if Group /= Last_Group then
New_Line;
Put_Line ("# " & To_String (Group) & " Commands");

Last_Group := Group;
end if;

Put_Line ("## `alr " & Cmd.Name & "`");

New_Line;
Sub_Cmd.Display_Help (Cmd.Name);
New_Line;

end Put_MD_Command;

procedure Put_MD_Topic
(Topic : not null CLIC.Subcommand.Help_Topic_Access) is
begin
New_Line;
Put_Line ("## " & Topic.Name);
Sub_Cmd.Display_Help (Topic.Name);
New_Line;
end Put_MD_Topic;

begin
CLIC.Formatter.Enable_Markdown;

Put_Line ("# Usage Help");
Sub_Cmd.Display_Usage;

Sub_Cmd.Iterate_Commands (Process => Put_MD_Command'Access);

Put_Line ("# Topics");
Sub_Cmd.Iterate_Topics (Process => Put_MD_Topic'Access);

end Print_MD_Help;

-------------
-- Execute --
-------------
Expand Down Expand Up @@ -56,6 +111,10 @@ package body Alr.Commands.Dev is
if Cmd.UTF_8_Test then
Print_UTF_8_Sequence;
end if;

if Cmd.MD_Help then
Print_MD_Help;
end if;
end Execute;

----------------------
Expand Down Expand Up @@ -105,6 +164,11 @@ package body Alr.Commands.Dev is
Cmd.UTF_8_Test'Access,
"", "--utf8",
"Print a known UTF-8 sequence");

Define_Switch (Config,
Cmd.MD_Help'Access,
"", "--help-doc-markdown",
"Print a complete help page in markdown format");
end Setup_Switches;

end Alr.Commands.Dev;
1 change: 1 addition & 0 deletions src/alr/alr-commands-dev.ads
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private
Raise_Except : aliased Boolean := False;
Self_Test : aliased Boolean := False;
UTF_8_Test : aliased Boolean := False; -- Produce some UTF-8 output
MD_Help : aliased Boolean := False;
end record;

end Alr.Commands.Dev;
2 changes: 1 addition & 1 deletion src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ package body Alr.Commands is
end if;

if No_TTY then
CLIC.TTY.Force_Disable_TTY;
CLIC.Formatter.Force_Disable_TTY;
end if;

-- Use CLIC.TTY selection/detection of TTY
Expand Down
11 changes: 6 additions & 5 deletions src/alr/alr-commands.ads
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ with Alire.Solver;
with Alire.Version;

with CLIC.Subcommand;
with CLIC.Formatter;

private with GNAT.IO;
private with CLIC.Subcommand.Instance;
Expand Down Expand Up @@ -143,11 +144,11 @@ private
Put_Error => Put_Error,
Error_Exit => OS_Lib.Bailout,
Set_Global_Switches => Set_Global_Switches,
TTY_Chapter => Alire.TTY.Bold,
TTY_Description => Alire.TTY.Description,
TTY_Version => Alire.TTY.Version,
TTY_Underline => Alire.TTY.Underline,
TTY_Emph => Alire.TTY.Emph);
TTY_Chapter => CLIC.Formatter.Chapter,
TTY_Description => CLIC.Formatter.Description,
TTY_Version => CLIC.Formatter.Version,
TTY_Underline => CLIC.Formatter.Underline,
TTY_Emph => CLIC.Formatter.Emph);

Unset : constant String := "unset";
-- Canary for when a string switch is given without value
Expand Down

0 comments on commit c47044a

Please sign in to comment.