Lazarus

Free Pascal => General => Topic started by: Blade on July 06, 2020, 05:25:37 pm

Title: Why is CommandLine From TProcess Deprecated?
Post by: Blade 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.
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: Handoko on July 06, 2020, 05:44:40 pm
You can find the information here:
https://www.freepascal.org/docs-html/fcl/process/tprocess.commandline.html
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: marcov 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.
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: Blade 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.  
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: winni 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
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: Jurassic Pork 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
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: Blade on July 07, 2020, 07:52:39 am
Jurassic Pork, indeed.  Thank you for the correction.
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: marcov 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.
Title: Re: Why is CommandLine From TProcess Deprecated?
Post by: Blade 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