Recent

Author Topic: Why is CommandLine From TProcess Deprecated?  (Read 831 times)

Blade

  • New Member
  • *
  • Posts: 47
Why is CommandLine From TProcess Deprecated?
« on: July 06, 2020, 05:25:37 pm »
The Windows OS Command Line is still very much a thing.  The TProcess CommandLine allows taking commands from Windows Batch scripts and using them in Free Pascal.  Don't understand the logic in deprecating it versus just leaving it be, and maybe just leaving a warning that it is for Windows only or something like that.

Handoko

  • Hero Member
  • *****
  • Posts: 3906
  • My goal: build my own game engine using Lazarus
Re: Why is CommandLine From TProcess Deprecated?
« Reply #1 on: July 06, 2020, 05:44:40 pm »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8896
  • FPC developer.
Re: Why is CommandLine From TProcess Deprecated?
« Reply #2 on: July 06, 2020, 06:41:01 pm »
Basically some OSes accept the info (exe name, parameters) as cmdline, and some as arrays.

Splitting the cmdlines into arrays was very system dependent (quoting options etc), while changing arrays to cmdlines is fairly trivial. This means that the parsing rules constantly changed and thus were not very backwards compatible.

So it was deemed best to phase cmdlines as api out.

Blade

  • New Member
  • *
  • Posts: 47
Re: Why is CommandLine From TProcess Deprecated?
« Reply #3 on: July 07, 2020, 12:05:26 am »
I think there is a disconnect somewhere.  Using TProcess Parameters.Add does not seem to act as an equivalent to TProcess CommandLine in all cases.  With CommandLine you can more likely take a command that would run from a Windows Command Prompt, and it will often run the same in CommandLine.  Where with Parameters.Add, things become more tricky.  There is also the simplicity of usage for one versus the other.

Of course there are other ways of running files, like with ExecuteProcess or RunCommand, but I'm just saying that keeping around CommandLine doesn't look like it would hurt.

For example, the 2nd version will not work properly as the first.

Code: Pascal  [Select][+][-]
  1. p := TProcess.Create(nil);
  2. p.CommandLine := 'notepad.exe C:\folder\selectedfile.txt';
  3. p.execute;
  4. p.Free;
  5.  

Code: Pascal  [Select][+][-]
  1. p := TProcess.Create(nil);
  2. p.Executable := 'notepad.exe';
  3. p.Parameters.add := 'C:\folder\selectedfile.txt';
  4. p.execute;
  5. p.Free;
  6.  
« Last Edit: July 07, 2020, 12:08:37 am by Blade »

winni

  • Hero Member
  • *****
  • Posts: 1996
Re: Why is CommandLine From TProcess Deprecated?
« Reply #4 on: July 07, 2020, 12:10:20 am »
 Hi!

As one can see from the example you are talking about Windows.

In Linux it is vice versa:

Example 1 does not work,
example 2 is fine

Winni

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 857
Re: Why is CommandLine From TProcess Deprecated?
« Reply #5 on: July 07, 2020, 02:00:43 am »
hello,
Code: Pascal  [Select][+][-]
  1. p := TProcess.Create(nil);
  2. p.Executable := 'notepad.exe';
  3. p.Parameters.add := 'C:\folder\selectedfile.txt';
  4. p.execute;
  5. p.Free;
  6.  

syntax is wrong with add method , that must be :
Code: Pascal  [Select][+][-]
  1. p.Parameters.add('C:\folder\selectedfile.txt');
  2.  

i have no problem with Lazarus 2.0.8  on windows 10 with this code :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var p: tprocess;
  3. begin
  4. p := TProcess.Create(nil);
  5. p.Executable := 'notepad.exe';
  6. p.Parameters.add('C:\temp\selectedfile.txt');
  7. p.execute;
  8. p.Free;
  9. end;

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Blade

  • New Member
  • *
  • Posts: 47
Re: Why is CommandLine From TProcess Deprecated?
« Reply #6 on: July 07, 2020, 07:52:39 am »
Jurassic Pork, indeed.  Thank you for the correction.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8896
  • FPC developer.
Re: Why is CommandLine From TProcess Deprecated?
« Reply #7 on: July 07, 2020, 08:45:22 am »
There are no real plans to remove commandline at short notice. It is just that it is advised to use the parameters.add way as much as possible, because it is better.

Bugreports for quoting, or supporting more scenarios for commandline are no longer accepted, but immediately set as "won't" fix.  The deprecated status is more or less meant to reflect that status, so that users don't accidentally make new code with commandline thinking it is supported.

Note that TProcess had quite some changes internally in 3.2.0.

Blade

  • New Member
  • *
  • Posts: 47
Re: Why is CommandLine From TProcess Deprecated?
« Reply #8 on: July 08, 2020, 09:48:40 am »
Thanks for giving a status about this.  Plus Free Pascal does address this in a number of ways.  Playing around with different options, ShellExecute will also correlate to many commands from Windows Batch scripts. 

 

TinyPortal © 2005-2018