@@ -21,7 +21,6 @@ with Ada.Characters.Latin_1;
2121with Ada.Text_IO ;
2222with Ada.Exceptions ; use Ada.Exceptions;
2323with Ada.Strings.Unbounded ;
24- with GNAT.Command_Line ; use GNAT.Command_Line;
2524with GNAT.Traceback.Symbolic ; use GNAT.Traceback.Symbolic;
2625with GNAT.OS_Lib ;
2726with GNAT.Strings ;
@@ -31,8 +30,10 @@ with System.Soft_Links;
3130with System.Secondary_Stack ;
3231
3332with VSS.Application ;
33+ with VSS.Command_Line ;
3434with VSS.Standard_Paths ;
3535with VSS.Strings.Conversions ;
36+ with VSS.Text_Streams.Standadrs ;
3637
3738with GNATCOLL.JSON ;
3839with GNATCOLL.Memory ; use GNATCOLL.Memory;
@@ -82,6 +83,9 @@ procedure LSP.Ada_Driver is
8283 -- Quit the process when an uncaught exception reaches this. Used for
8384 -- fuzzing.
8485
86+ procedure Print_Help (Option : VSS.Command_Line.Named_Option'Class);
87+ -- Put option description to stdout
88+
8589 Server_Trace : constant Trace_Handle := Create (" ALS.MAIN" , From_Config);
8690 -- Main trace for the LSP.
8791
@@ -130,6 +134,31 @@ procedure LSP.Ada_Driver is
130134 GNAT.OS_Lib.OS_Exit (42 );
131135 end Die_On_Uncaught ;
132136
137+ -- --------------
138+ -- Print_Help --
139+ -- --------------
140+
141+ procedure Print_Help (Option : VSS.Command_Line.Named_Option'Class) is
142+ use type VSS.Strings.Character_Count;
143+ Ok : Boolean := True;
144+ Output : VSS.Text_Streams.Output_Text_Stream'Class :=
145+ VSS.Text_Streams.Standadrs.Standard_Output;
146+ Last : VSS.Strings.Character_Count :=
147+ 3 + Option.Long_Name.Character_Length;
148+ begin
149+ Output.Put (" --" , Ok);
150+ Output.Put (Option.Long_Name, Ok);
151+ if Option in VSS.Command_Line.Value_Option'Class then
152+ Output.Put (" =ARG" , Ok);
153+ Last := Last + 4 ;
154+ end if ;
155+
156+ for J in Last + 1 .. 17 loop
157+ Output.Put (' ' , Ok);
158+ end loop ;
159+ Output.Put_Line (Option.Description, Ok);
160+ end Print_Help ;
161+
133162 -- ---------------------
134163 -- Register_Commands --
135164 -- ---------------------
@@ -173,9 +202,7 @@ procedure LSP.Ada_Driver is
173202 Command'Tag);
174203 end Register_Commands ;
175204
176- use GNAT.Strings;
177-
178- Cmdline : Command_Line_Configuration;
205+ use type VSS.Strings.Virtual_String;
179206
180207 Fuzzing_Activated : constant Boolean :=
181208 not VSS.Application.System_Environment.Value (" ALS_FUZZING" ).Is_Empty;
@@ -198,64 +225,68 @@ procedure LSP.Ada_Driver is
198225 GNATdebug : constant Virtual_File := Create_From_Base
199226 (" .gnatdebug" );
200227
201- Traces_File_Path : aliased String_Access;
202- Config_File_Path : aliased String_Access;
203- Traces_File : Virtual_File;
228+ Trace_File_Option : constant VSS.Command_Line.Value_Option :=
229+ (Short_Name => " " ,
230+ Long_Name => " tracefile" ,
231+ Description => " Full path to a file containing traces configuration" ,
232+ Value_Name => " ARG" );
233+
234+ Config_Description : constant VSS.Strings.Virtual_String :=
235+ " Full path to a JSON file containing initialization "
236+ & " options for the server (i.e: all the settings that can be specified "
237+ & " through LSP 'initialize' request's initializattionOptions)" ;
238+
239+ Config_File_Option : constant VSS.Command_Line.Value_Option :=
240+ (Short_Name => " " ,
241+ Long_Name => " config" ,
242+ Description => Config_Description,
243+ Value_Name => " ARG" );
244+
245+ Language_GPR_Option : constant VSS.Command_Line.Binary_Option :=
246+ (Short_Name => " " ,
247+ Long_Name => " language-gpr" ,
248+ Description => " Handle GPR language instead of Ada" );
249+
250+ Version_Option : constant VSS.Command_Line.Binary_Option :=
251+ (Short_Name => " " ,
252+ Long_Name => " version" ,
253+ Description => " Display the program version" );
254+
255+ Help_Option : constant VSS.Command_Line.Binary_Option :=
256+ (Short_Name => " " ,
257+ Long_Name => " help" ,
258+ Description => " Display this help" );
259+
204260 Config_File : Virtual_File;
205- Help_Arg : aliased Boolean := False;
206- Version_Arg : aliased Boolean := False;
207- Language_GPR_Arg : aliased Boolean := False;
208261
209262 Memory_Monitor_Enabled : Boolean;
210263begin
211264 -- Handle the command line
212- Set_Usage
213- (Cmdline,
214- Help => " Command line interface for the Ada Language Server" );
215-
216- Define_Switch
217- (Cmdline,
218- Output => Traces_File_Path'Access ,
219- Long_Switch => " --tracefile=" ,
220- Help => " Full path to a file containing traces configuration" );
221-
222- Define_Switch
223- (Cmdline,
224- Output => Config_File_Path'Access ,
225- Long_Switch => " --config=" ,
226- Help => " Full path to a JSON file containing initialization "
227- & " options for the server (i.e: all the settings that can be specified "
228- & " through LSP 'initialize' request's initializattionOptions)" );
229-
230- Define_Switch
231- (Cmdline,
232- Output => Language_GPR_Arg'Access ,
233- Long_Switch => " --language-gpr" ,
234- Help => " Handle GPR language instead of Ada" );
235-
236- Define_Switch
237- (Cmdline,
238- Output => Version_Arg'Access ,
239- Long_Switch => " --version" ,
240- Help => " Display the program version" );
241-
242- Define_Switch
243- (Cmdline,
244- Output => Help_Arg'Access ,
245- Long_Switch => " --help" ,
246- Help => " Display this help" );
247-
248- begin
249- Getopt (Cmdline);
250- exception
251- when GNAT.Command_Line.Exit_From_Command_Line =>
252- Free (Cmdline);
253- GNAT.OS_Lib.OS_Exit (0 );
254- end ;
255-
256- Free (Cmdline);
265+ -- Help => "Command line interface for the Ada Language Server");
266+
267+ VSS.Command_Line.Add_Option (Trace_File_Option);
268+ VSS.Command_Line.Add_Option (Config_File_Option);
269+ VSS.Command_Line.Add_Option (Language_GPR_Option);
270+ VSS.Command_Line.Add_Option (Version_Option);
271+ VSS.Command_Line.Add_Option (Help_Option);
272+ VSS.Command_Line.Process; -- Will exit if errors
273+
274+ if VSS.Command_Line.Is_Specified (Help_Option) then
275+ Ada.Text_IO.Put_Line
276+ (" Language Server Ada and SPARK." );
277+ -- TBD: Print list of options using VSS
278+ Ada.Text_IO.Put_Line
279+ (" Usage: ada_language_server [switches] [arguments]" );
280+ Ada.Text_IO.New_Line;
281+
282+ Print_Help (Trace_File_Option);
283+ Print_Help (Config_File_Option);
284+ Print_Help (Language_GPR_Option);
285+ Print_Help (Version_Option);
286+ Print_Help (Help_Option);
257287
258- if Version_Arg then
288+ GNAT.OS_Lib.OS_Exit (0 );
289+ elsif VSS.Command_Line.Is_Specified (Version_Option) then
259290 Ada.Text_IO.Put_Line (" ALS version: " & $VERSION);
260291 GNAT.OS_Lib.OS_Exit (0 );
261292 end if ;
@@ -264,16 +295,19 @@ begin
264295 -- - passed on the command line via --tracefile,
265296 -- - in a .gnatdebug file locally
266297 -- - in "traces.cfg" in the ALS home directory
267- if Traces_File_Path /= null
268- and then Traces_File_Path.all /= " "
269- then
270- Traces_File := Create (+Traces_File_Path.all );
271- if not Traces_File.Is_Regular_File then
272- Ada.Text_IO.Put_Line (" Could not find the specified traces file" );
273- GNAT.OS_Lib.OS_Exit (1 );
274- end if ;
275- Parse_Config_File (Traces_File);
298+ if VSS.Command_Line.Is_Specified (Trace_File_Option) then
299+ declare
300+ Traces_File : constant Virtual_File := Create_From_UTF8
301+ (VSS.Strings.Conversions.To_UTF_8_String
302+ (VSS.Command_Line.Value (Trace_File_Option)));
303+ begin
304+ if not Traces_File.Is_Regular_File then
305+ Ada.Text_IO.Put_Line (" Could not find the specified traces file" );
306+ GNAT.OS_Lib.OS_Exit (1 );
307+ end if ;
276308
309+ Parse_Config_File (Traces_File);
310+ end ;
277311 elsif GNATdebug.Is_Regular_File then
278312 Parse_Config_File (GNATdebug);
279313
@@ -289,19 +323,14 @@ begin
289323 " .$T.$$.log:buffer_size=0" );
290324 end if ;
291325
292- if Traces_File_Path /= null then
293- Free (Traces_File_Path);
294- end if ;
295-
296326 -- Look for a config file, that contains the configuration for the server
297327 -- (i.e: the configuration that can be specified through the 'initialize'
298328 -- request initializationOptions).
299329
300- if Config_File_Path /= null
301- and then Config_File_Path.all /= " "
302- then
303- Config_File := Create (+Config_File_Path.all );
304-
330+ if VSS.Command_Line.Is_Specified (Config_File_Option) then
331+ Config_File := Create_From_UTF8
332+ (VSS.Strings.Conversions.To_UTF_8_String
333+ (VSS.Command_Line.Value (Config_File_Option)));
305334 if not Config_File.Is_Regular_File then
306335 Ada.Text_IO.Put_Line (" Could not find the specified config file" );
307336 GNAT.OS_Lib.OS_Exit (1 );
@@ -327,8 +356,6 @@ begin
327356
328357 Ada_Handler.Change_Configuration (Parse_Result.Value);
329358 end ;
330-
331- Free (Config_File_Path);
332359 end if ;
333360
334361 Server_Trace.Trace (" ALS version: " & $VERSION);
@@ -347,7 +374,7 @@ begin
347374 GNATCOLL.Memory.Configure (Activate_Monitor => True);
348375 end if ;
349376
350- if not Language_GPR_Arg then
377+ if not VSS.Command_Line.Is_Specified (Language_GPR_Option) then
351378 -- Load predefined completion items
352379 LSP.Predefined_Completion.Load_Predefined_Completion_Db (Server_Trace);
353380 Register_Commands;
@@ -356,7 +383,7 @@ begin
356383 Server.Initialize (Stream'Unchecked_Access);
357384
358385 begin
359- if Language_GPR_Arg then
386+ if VSS.Command_Line.Is_Specified (Language_GPR_Option) then
360387 Server.Run
361388 (GPR_Handler'Unchecked_Access,
362389 GPR_Handler'Unchecked_Access,
0 commit comments