1. -- 
  2.  
  3. -- <summary> 
  4. --  This is a generic package implementing a simple-to-use command line 
  5. --  parser.  Yes, I know, everyone makes his/her own command line parser... 
  6. --  so, I wrote mine.  As they say, every open source project starts 
  7. --  with a programmer that schratches hes own itch. So I did... If 
  8. --  you find this useful, you are welcome to use it. 
  9. -- 
  10. -- The ideas behind this package are the following 
  11. -- 
  12. -- * Parameters are nominal, non positional.  The syntax is of 
  13. --   "named parameter" type, that is, each command line parameter is 
  14. --   expected to have thefollowing format 
  15. -- 
  16. --          label ['=' value] 
  17. -- 
  18. --    where "label" is any string without '='. 
  19. -- 
  20. -- * Parsed value are written in a "configuration variable" whose type 
  21. --   is a formal parameter of this package.  The values are written 
  22. --   in the configuration variable by using some callbacks provided 
  23. --   by caller. 
  24. -- 
  25. -- The names of the parameters are given to the parser in "parameter 
  26. -- description array" that is an array of records that specify 
  27. -- 
  28. --     + The parameter name 
  29. -- 
  30. --     + A default value (if needed) 
  31. -- 
  32. --     + If the parameter is mandatory 
  33. -- 
  34. --     + If it can be specified more than once 
  35. -- 
  36. --     + The callback function to be called when the parameter is found 
  37. -- 
  38. -- In order to parse the command line it suffices to call Parse_Command_Line 
  39. -- giving as argument the array of parameter descriptors and the configuration 
  40. -- variable to be written.  For every parameter found, the corresponding 
  41. -- callback function is called.  If at the end of the parsing there are some 
  42. -- optional parameters that were missing from the command line, the 
  43. -- corresponding callbacks are called with the default parameter. 
  44. -- </summary> 
  45. with Ada.Strings.Unbounded; 
  46. with Ada.Text_IO; 
  47.  
  48. generic 
  49.    type Config_Data is limited private; 
  50.    -- The parameters read from the command line will be written in 
  51.    -- a variable of this type 
  52.  
  53.    -- Set this to False if you want case insensitive option matching. 
  54.    -- For example, if you set this to False, "input", "Input", "INPUT" 
  55.    -- and "InPuT" will be equivalent names for the option "input" 
  56.    Case_Sensitive : Boolean := True; 
  57. package Generic_Line_Parser is 
  58.    use Ada.Strings.Unbounded; 
  59.  
  60.    type Parameter_Callback is 
  61.      access procedure (Name   : in     Unbounded_String; 
  62.                        Value  : in     Unbounded_String; 
  63.                        Result : in out Config_Data); 
  64.  
  65.    type Parameter_Descriptor is 
  66.       record 
  67.          Name      : Unbounded_String;    -- Parameter name 
  68.          Default   : Unbounded_String;    -- Default value used if not on C.L. 
  69.          Mandatory : Boolean;             -- Parameter MUST be given 
  70.          Only_Once : Boolean;             -- Parameter MUST NOT be given more than once 
  71.          Callback  : Parameter_Callback;  -- Called when parameter found 
  72.       end record; 
  73.    -- <description>Record holding the description of a parameter.  The fields 
  74.    --  should be self-explenatory (I hope).  The only field that needs some 
  75.    -- explanation is Name since it allows to specify more than one 
  76.    -- name for each parameter.  The syntax is very simple: just separate 
  77.    -- the names with commas.  For example, if Name is "f,filename,input" 
  78.    -- one can use on the command line, with the same effect  f=/tmp/a.txt or 
  79.    -- filename=/tmp/a.txt or input=/tmp/a.txt.  Spaces at both ends of 
  80.    -- the label name are trimmed, so that, for example, "f,filename,input" 
  81.    -- is equivalent to "f ,    filename  ,input " 
  82.    -- </description> 
  83.  
  84.  
  85.    type Parameter_Descriptor_Array is 
  86.      array (Natural range <>) of Parameter_Descriptor; 
  87.  
  88.    -- Main exported method.  It parses the command line and it writes 
  89.    -- the result in Result.  If some error is encountered, Bad_Command 
  90.    -- is raised with an explicative exception message.  Help_Line, 
  91.    -- if not empty, is written to Help_Output in case of error. 
  92.    procedure Parse_Command_Line 
  93.      (Parameters  : in     Parameter_Descriptor_Array; 
  94.       Result      :    out Config_Data; 
  95.       Help_Line   : in     String := ""; 
  96.       Help_Output : in     Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Error); 
  97.  
  98.    Bad_Command : exception; 
  99.  
  100.  
  101.    -- Convenient conversion function to Float that raise Bad_Command if 
  102.    -- the argument has not a valid syntax 
  103.    function To_Float (X : Unbounded_String) 
  104.                       return Float; 
  105.  
  106.    -- Convenient conversion function to Float that raise Bad_Command if 
  107.    -- the argument has not a valid syntax 
  108.    function To_Natural (X : Unbounded_String) 
  109.                         return Natural; 
  110.  
  111. end Generic_Line_Parser;