mold_1.0.3_a26094a7/src/mold_cli.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
-------------------------------------------------------------------------------
--
--  MOLD - Meta-variable Operations for Lean Development
--  Copyright (c) 2023, 2024 Francesc Rocher <francesc.rocher@gmail.com>
--  SPDX-License-Identifier: MIT
--
-------------------------------------------------------------------------------

with Ada.Text_IO;
with Ada.Strings.UTF_Encoding;
with GNAT.OS_Lib;

with CLIC.Subcommand.Instance;
with CLIC.TTY;
with Simple_Logging;

with Mold_Apply;
with Mold_Lib_Config;

package body Mold_CLI is

   package Log renames Simple_Logging;

   use all type Conf.Build_Profile_Kind;

   subtype UTF_8_String is Ada.Strings.UTF_Encoding.UTF_8_String;

   function Info (Text : CLIC.TTY.UTF_8_String := "") return UTF_8_String is
     (CLIC.TTY.Bold (Text));

   procedure Set_Global_Switches
     (Config : in out CLIC.Subcommand.Switches_Configuration);

   --!pp off
   package CLI_Command is new CLIC.Subcommand.Instance
   (
      Main_Command_Name => Conf.Crate_Name,
      Version => Conf.Crate_Version &
         " (lib-" & Mold_Lib_Config.Crate_Version & ")" & (
            if Conf.Build_Profile = Conf.release then ""
               else " " & Conf.Build_Profile'Image
         ),
      Set_Global_Switches => Set_Global_Switches,
      Put                 => Ada.Text_IO.Put,
      Put_Line            => Ada.Text_IO.Put_Line,
      Put_Error           => Ada.Text_IO.Put_Line,
      Error_Exit          => GNAT.OS_Lib.OS_Exit,
      TTY_Chapter         => Info,
      TTY_Description     => CLIC.TTY.Description,
      TTY_Version         => CLIC.TTY.Version,
      TTY_Underline       => CLIC.TTY.Underline,
      TTY_Emph            => CLIC.TTY.Emph
   );
   --!pp on

   -------------------------
   -- Set_Global_Switches --
   -------------------------

   procedure Set_Global_Switches
     (Config : in out CLIC.Subcommand.Switches_Configuration)
   is
      use CLIC.Subcommand;
   begin
      --!pp off
      pragma Style_Checks (off);

      Define_Switch (
         Config,
         Output      => Global_Switch.Help'Access,
         Switch      => "-h",
         Long_Switch => "--help",
         Help        => "Display command help"
      );

      Define_Switch (
         Config,
         Global_Switch.No_Color'Access,
         Long_Switch => "--no-color",
         Help        => "Disable color",
         Value       => False
      );

      Define_Switch (
         Config,
         Global_Switch.No_TTY'Access,
         Long_Switch => "--no-tty",
         Help        => "Disable control characters",
         Value       => False
      );

      Define_Switch (
         Config,
         Global_Switch.Verbose'Access,
         Switch      => "-v",
         Long_Switch => "--verbose",
         Help        => "Show command activity"
      );

      case Mold_Config.Build_Profile is
         when Mold_Config.release => null;
         when others =>
         Define_Switch (
            Config,
            Global_Switch.Debug'Access,
            Switch      => "-d",
            Long_Switch => "--debug",
            Help        => "Enable debug messages"
         );
      end case;

      pragma Style_Checks (on);
      --!pp on
   end Set_Global_Switches;

   -------------
   -- Execute --
   -------------

   procedure Execute is
   begin
      CLI_Command.Parse_Global_Switches;

      --  default Log level
      Log.Level := Log.Info;

      if Global_Switch.Verbose then
         Log.Level := Log.Detail;
         Log.Debug ("Log level set to " & Log.Level'Image);
      end if;

      if Global_Switch.Debug then
         Log.Level := Log.Debug;
         Log.Debug ("show debug information");
         Log.Debug ("Global_Switch" & Global_Switch'Image);
      end if;

      if not Global_Switch.No_TTY then
         CLIC.TTY.Force_Disable_TTY;
         Log.Debug ("disable TTY");
      end if;

      if Global_Switch.No_Color and then Global_Switch.No_TTY then
         CLIC.TTY.Enable_Color (Force => True);
         Log.Debug ("disable Color");
      end if;

      CLI_Command.Execute;
   end Execute;

begin

   CLI_Command.Register ("General", new CLI_Command.Builtin_Help);
   CLI_Command.Register ("Process", new Mold_Apply.Cmd_Type);

end Mold_CLI;