Recent

Author Topic: <solved> command line  (Read 2212 times)

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
<solved> command line
« on: May 10, 2024, 06:16:03 pm »
I am just beginning but i want to make an application with a command line option string (path name) that can be used by the program. How has the FormCreate be made?
« Last Edit: May 16, 2024, 11:19:25 am by Sprek Skik »

RayoGlauco

  • Full Member
  • ***
  • Posts: 194
  • Beers: 1567
Re: command line
« Reply #1 on: May 10, 2024, 06:35:17 pm »
I'm not sure if this is what you are asking for. You can pass some parameters to your program, and check them in any part of your program: https://wiki.freepascal.org/Command_line_parameters_and_environment_variables
To err is human, but to really mess things up, you need a computer.

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: command line
« Reply #2 on: May 10, 2024, 09:05:27 pm »
well that's what i am looking for. But a test doesn't work.
unit unit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs;//, StdDlg;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure Create(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Create(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to paramCount() do
begin
  showmessage(paramStr(i));
end;
end;


end.

cdbc

  • Hero Member
  • *****
  • Posts: 1806
    • http://www.cdbc.dk
Re: command line
« Reply #3 on: May 10, 2024, 09:21:02 pm »
Hi
This works for me in a button click-handler:
Code: Pascal  [Select][+][-]
  1. procedure procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4. begin
  5.   for i := 0 to paramCount() do
  6.   begin
  7.     showmessage(paramStr(i));
  8.   end;
  9. end;
...dunno 'bout 'FormCreate' though.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3923
Re: command line
« Reply #4 on: May 10, 2024, 10:02:16 pm »
...dunno 'bout 'FormCreate' though.
Works like a charm /everywhere/ (project file, unit initialization, unit finalization, form creation, form destruction, onidle, etc etc...)

My guess would be that the problem originates from supplying the parameter(s). Either that or the event(s) are not 'linked' because the object inspector was not used to create them.
« Last Edit: May 10, 2024, 10:04:22 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: command line
« Reply #5 on: May 11, 2024, 05:09:19 am »
Done.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormActivate(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     Data: array of string;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.FormActivate(Sender: TObject);
  31. const
  32.   RunOnce: Boolean = False;
  33. var
  34.   S: string;
  35.   i: Integer;
  36. begin
  37.   if RunOnce then Exit;
  38.   RunOnce := True;
  39.   S       := 'Command line parameters:' + LineEnding;
  40.   for i := 0 to Length(Data)-1 do
  41.     S := S + 'Param' + i.ToString + ': ' + Data[i] + LineEnding;
  42.   ShowMessage(S);
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. var
  47.   i: Integer;
  48. begin
  49.   SetLength(Data, ParamCount+1);
  50.   for i := 0 to ParamCount do
  51.     Data[i] := ParamStr(i);
  52. end;
  53.  
  54. end.

  • We should not put any screen/display related operations in FormCreate event because OnCreate event is just for initializating data, many screen objects haven't really prepared for displaying. So I put ShowMessage command in the FormActivate event, see line #42.
  • A small trick I used for make it runs only once, see line #32.
  • Because ParamStr starts from 0, ParamCount needs to be added with 1, see line #49.
  • No need to free the Data, because dynamic arrays are finalized automatically when program comes to end.
To supply command line parameters in Lazarus:
Lazarus main menu > Run > Run Parameters

You can download the source code below for testing.
« Last Edit: May 11, 2024, 05:26:44 am by Handoko »

ASerge

  • Hero Member
  • *****
  • Posts: 2373
Re: command line
« Reply #6 on: May 11, 2024, 06:18:56 am »
A small trick I used for make it runs only once, see line #32.
Another version of the trick:
Code: Pascal  [Select][+][-]
  1. ...
  2.     procedure AppInit(Data: PtrInt);
  3. ...
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   Application.QueueAsyncCall(@AppInit, 0);
  7. end;
  8.  
The AppInit will be called after all OnActivate, OnShow and before the first OnIdle.

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: command line
« Reply #7 on: May 11, 2024, 06:31:22 am »
A new trick. I haven't known about. I will surely use it next time. Thank you for sharing it.

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: command line
« Reply #8 on: May 14, 2024, 07:55:06 am »
Thanks for all the replies!

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: command line
« Reply #9 on: May 14, 2024, 08:08:49 am »
Done.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormActivate(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     Data: array of string;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.FormActivate(Sender: TObject);
  31. const
  32.   RunOnce: Boolean = False;
  33. var
  34.   S: string;
  35.   i: Integer;
  36. begin
  37.   if RunOnce then Exit;
  38.   RunOnce := True;
  39.   S       := 'Command line parameters:' + LineEnding;
  40.   for i := 0 to Length(Data)-1 do
  41.     S := S + 'Param' + i.ToString + ': ' + Data[i] + LineEnding;
  42.   ShowMessage(S);
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. var
  47.   i: Integer;
  48. begin
  49.   SetLength(Data, ParamCount+1);
  50.   for i := 0 to ParamCount do
  51.     Data[i] := ParamStr(i);
  52. end;
  53.  
  54. end.

  • We should not put any screen/display related operations in FormCreate event because OnCreate event is just for initializating data, many screen objects haven't really prepared for displaying. So I put ShowMessage command in the FormActivate event, see line #42.
  • A small trick I used for make it runs only once, see line #32.
  • Because ParamStr starts from 0, ParamCount needs to be added with 1, see line #49.
  • No need to free the Data, because dynamic arrays are finalized automatically when program comes to end.
To supply command line parameters in Lazarus:
Lazarus main menu > Run > Run Parameters

You can download the source code below for testing.

I will test it, because I have the experience that FormActivate doesn't work in my code...( example program klokje from https://wiskunst.nl/index.php/programmeren1/delphi-lazarus/lazarus-klokje-intro/)

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: command line
« Reply #10 on: May 14, 2024, 08:13:37 am »
Hi
This works for me in a button click-handler:
Code: Pascal  [Select][+][-]
  1. procedure procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4. begin
  5.   for i := 0 to paramCount() do
  6.   begin
  7.     showmessage(paramStr(i));
  8.   end;
  9. end;
...dunno 'bout 'FormCreate' though.
Regards Benny

Nice Code! Thanks...

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: command line
« Reply #11 on: May 14, 2024, 08:25:39 am »
I will test it, because I have the experience that FormActivate doesn't work in my code...( example program klokje from https://wiskunst.nl/index.php/programmeren1/delphi-lazarus/lazarus-klokje-intro/)

The link you provided does not have any code for us to test.

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: command line
« Reply #12 on: May 16, 2024, 11:18:15 am »
I will test it, because I have the experience that FormActivate doesn't work in my code...( example program klokje from https://wiskunst.nl/index.php/programmeren1/delphi-lazarus/lazarus-klokje-intro/)

The link you provided does not have any code for us to test.

go to the last document on the site on this page you will see an index of source code you will need all to compile klokje.
hope you will find the  code.

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: <solved> command line
« Reply #13 on: May 16, 2024, 02:02:16 pm »
I think I found what you meant. Unfortunately the website doesn't provide a download button.

If you're new in Lazarus/Pascal I recommend this page, which has a list of short demos for you to download, try and learn:

https://wiki.freepascal.org/Portal:HowTo_Demos

 

TinyPortal © 2005-2018