This is a cosmetic change only, and gives better control over the help
message that is automatically generated from the definition of valid
switches. Instead of using the generic name "ARG", users can now specify
their own name, which sometimes helps clarify the intents of the parameter.

Tested on x86_64-pc-linux-gnu, committed on trunk

2012-05-15  Emmanuel Briot  <br...@adacore.com>

        * g-comlin.adb, g-comlin.ads (Define_Switch): Allow specifying the name
        of the argument, for the automatic help message.
        (Getopt): do not systematically initialize switches with string values
        to the empty string, when the user has already specified a default.

Index: g-comlin.adb
===================================================================
--- g-comlin.adb        (revision 187501)
+++ g-comlin.adb        (working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1999-2011, Free Software Foundation, Inc.         --
+--          Copyright (C) 1999-2012, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -128,7 +128,8 @@
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "");
+      Section     : String := "";
+      Argument    : String := "ARG");
    --  Initialize [Def] with the contents of the other parameters.
    --  This also checks consistency of the switch parameters, and will raise
    --  Invalid_Switch if they do not match.
@@ -1280,11 +1281,12 @@
    ---------------------------
 
    procedure Initialize_Switch_Def
-     (Def : out Switch_Definition;
+     (Def         : out Switch_Definition;
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "")
+      Section     : String := "";
+      Argument    : String := "ARG")
    is
       P1, P2       : Switch_Parameter_Type := Parameter_None;
       Last1, Last2 : Integer;
@@ -1316,6 +1318,10 @@
          Def.Section := new String'(Section);
       end if;
 
+      if Argument /= "ARG" then
+         Def.Argument := new String'(Argument);
+      end if;
+
       if Help /= "" then
          Def.Help := new String'(Help);
       end if;
@@ -1330,12 +1336,14 @@
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "")
+      Section     : String := "";
+      Argument    : String := "ARG")
    is
       Def : Switch_Definition;
    begin
       if Switch /= "" or else Long_Switch /= "" then
-         Initialize_Switch_Def (Def, Switch, Long_Switch, Help, Section);
+         Initialize_Switch_Def
+            (Def, Switch, Long_Switch, Help, Section, Argument);
          Add (Config, Def);
       end if;
    end Define_Switch;
@@ -1375,12 +1383,14 @@
       Help        : String := "";
       Section     : String := "";
       Initial     : Integer := 0;
-      Default     : Integer := 1)
+      Default     : Integer := 1;
+      Argument    : String := "ARG")
    is
       Def : Switch_Definition (Switch_Integer);
    begin
       if Switch /= "" or else Long_Switch /= "" then
-         Initialize_Switch_Def (Def, Switch, Long_Switch, Help, Section);
+         Initialize_Switch_Def
+            (Def, Switch, Long_Switch, Help, Section, Argument);
          Def.Integer_Output  := Output.all'Unchecked_Access;
          Def.Integer_Default := Default;
          Def.Integer_Initial := Initial;
@@ -1398,12 +1408,14 @@
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "")
+      Section     : String := "";
+      Argument    : String := "ARG")
    is
       Def : Switch_Definition (Switch_String);
    begin
       if Switch /= "" or else Long_Switch /= "" then
-         Initialize_Switch_Def (Def, Switch, Long_Switch, Help, Section);
+         Initialize_Switch_Def
+            (Def, Switch, Long_Switch, Help, Section, Argument);
          Def.String_Output  := Output.all'Unchecked_Access;
          Add (Config, Def);
       end if;
@@ -3206,17 +3218,31 @@
                   Decompose_Switch (Def.Long_Switch.all, P2, Last2);
                   Append (Result, ", "
                           & Def.Long_Switch (Def.Long_Switch'First .. Last2));
-                  Append (Result, Param_Name (P2, "ARG"));
 
+                  if Def.Argument = null then
+                     Append (Result, Param_Name (P2, "ARG"));
+                  else
+                     Append (Result, Param_Name (P2, Def.Argument.all));
+                  end if;
+
                else
-                  Append (Result, Param_Name (P1, "ARG"));
+                  if Def.Argument = null then
+                     Append (Result, Param_Name (P1, "ARG"));
+                  else
+                     Append (Result, Param_Name (P1, Def.Argument.all));
+                  end if;
                end if;
 
             else  --  Long_Switch necessarily not null
                Decompose_Switch (Def.Long_Switch.all, P2, Last2);
                Append (Result,
                        Def.Long_Switch (Def.Long_Switch'First .. Last2));
-               Append (Result, Param_Name (P2, "ARG"));
+
+               if Def.Argument = null then
+                  Append (Result, Param_Name (P2, "ARG"));
+               else
+                  Append (Result, Param_Name (P2, Def.Argument.all));
+               end if;
             end if;
          end if;
 
@@ -3393,7 +3419,9 @@
                  Config.Switches (S).Integer_Initial;
 
             when Switch_String =>
-               Config.Switches (S).String_Output.all := new String'("");
+               if Config.Switches (S).String_Output.all = null then
+                  Config.Switches (S).String_Output.all := new String'("");
+               end if;
          end case;
       end loop;
 
Index: g-comlin.ads
===================================================================
--- g-comlin.ads        (revision 187501)
+++ g-comlin.ads        (working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---                     Copyright (C) 1999-2011, AdaCore                     --
+--                     Copyright (C) 1999-2012, AdaCore                     --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -595,7 +595,8 @@
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "");
+      Section     : String := "";
+      Argument    : String := "ARG");
    --  Indicates a new switch. The format of this switch follows the getopt
    --  format (trailing ':', '?', etc for defining a switch with parameters).
    --
@@ -617,6 +618,9 @@
    --
    --  In_Section indicates in which section the switch is valid (you need to
    --  first define the section through a call to Define_Section).
+   --
+   --  Argument is the name of the argument, as displayed in the automatic
+   --  help message. It is always capitalized for consistency.
 
    procedure Define_Switch
      (Config      : in out Command_Line_Configuration;
@@ -643,7 +647,8 @@
       Help        : String := "";
       Section     : String := "";
       Initial     : Integer := 0;
-      Default     : Integer := 1);
+      Default     : Integer := 1;
+      Argument    : String := "ARG");
    --  See Define_Switch for a description of the parameters.
    --  When the switch is found on the command line, Getopt will set
    --  Output.all to the value of the switch's parameter. If the parameter is
@@ -651,6 +656,7 @@
    --  Output is always initialized to Initial. If the switch has an optional
    --  argument which isn't specified by the user, then Output will be set to
    --  Default.
+   --  The switch must accept an argument.
 
    procedure Define_Switch
      (Config      : in out Command_Line_Configuration;
@@ -658,10 +664,14 @@
       Switch      : String := "";
       Long_Switch : String := "";
       Help        : String := "";
-      Section     : String := "");
+      Section     : String := "";
+      Argument    : String := "ARG");
    --  Set Output to the value of the switch's parameter when the switch is
    --  found on the command line.
-   --  Output is always initialized to the empty string.
+   --  Output is always initialized to the empty string if it does not have
+   --  a value already (otherwise it is left as is so that you can specify the
+   --  default value directly in the declaration of the variable).
+   --  The switch must accept an argument.
 
    procedure Set_Usage
      (Config   : in out Command_Line_Configuration;
@@ -1096,6 +1106,10 @@
       Section     : GNAT.OS_Lib.String_Access;
       Help        : GNAT.OS_Lib.String_Access;
 
+      Argument    : GNAT.OS_Lib.String_Access;
+      --  null if "ARG".
+      --  Name of the argument for this switch.
+
       case Typ is
          when Switch_Untyped =>
             null;

Reply via email to