agpl_1.0.0_b5da3320/src/agpl-command_line.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
with Ada.Command_line;
with Ada.Text_Io;
with Agpl.Ustrings; use Agpl.Ustrings;

with Gnat.Directory_operations;
with Gnat.Os_lib;

package body Agpl.Command_line is

   package Cl  renames Ada.Command_line;
   package Dop renames Gnat.Directory_operations;
   package Ol  renames Gnat.Os_lib;

   ------------------------------------------------------------------------
   --  Exists                                                            --
   ------------------------------------------------------------------------
   --  Ensure an option has been supplied
   function Exists (Option : in String) return Boolean is
   begin
      declare
         Dummy : constant String := Get_Option (Option);
         pragma Unreferenced (Dummy);
      begin
         --  If here, it means the option exists
         return True;
      end;
   exception
      when Option_Not_Supplied =>
         return False;
   end Exists;

   ------------------------------------------------------------------------
   --  Get_Option                                                        --
   ------------------------------------------------------------------------
   --  Returns the string after the given option (f.e: "-f <file>" will return
   --   <file>
   --  May raise Option_Not_Supplied
   function Get_Option (Option : in String) return String is
   begin
      for I in 1 .. Cl.Argument_Count loop
         if Cl.Argument (I) = Option then
            if I < Cl.Argument_Count then
               return Cl.Argument (I + 1);
            else
               return "";
            end if;
         end if;
      end loop;

      raise Option_Not_Supplied with "Missing option: " & Option;
   end Get_Option;

   ------------------------------------------------------------------------
   -- Program_path                                                       --
   ------------------------------------------------------------------------
   --  Returns the path to the executable (with trailing /, without the file).
   function Program_path return String is
   begin
      return Dop.Dir_name (Ol.Normalize_pathname (Cl.Command_name));
   end Program_path;

   ------------------------------------------------------------------------
   -- Program_name                                                       --
   ------------------------------------------------------------------------
   --  Returns the executable's name, without dot extension.
   --  I.e., the same in every platform.
   function Program_name return String is
   begin
      return Dop.Base_name (
         Cl.Command_name,
         Dop.File_extension (Cl.Command_name));
   end Program_name;

   ------------------------
   -- Wait_For_Keystroke --
   ------------------------

   procedure Wait_For_Keystroke is
      C : Character;
      use Ada.Text_IO;
   begin
      Get_Immediate (C);
      pragma Unreferenced (C);
   end Wait_For_Keystroke;

   -----------------------
   -- Full_Command_Line --
   -----------------------

   function Full_Command_Line return String is
      S : Ustring;
   begin
      Asu.Append (S, Ada.Command_Line.Command_Name);
      for I in 1 .. Ada.Command_Line.Argument_Count loop
         Asu.Append (S, " ");
         Asu.Append (S, Ada.Command_Line.Argument (I));
      end loop;

      return +S;
   end Full_Command_Line;

end Agpl.Command_line;