Recent

Author Topic: passing command line arguments  (Read 3636 times)

gozo97

  • Newbie
  • Posts: 5
passing command line arguments
« on: November 19, 2017, 01:43:55 pm »
Hello.
My first posting on this forum.
I am trying to open the get_iplayer program (which runs in a terminal) and displaying the output in a TListBox from within Lazarus.  I have been successful in opening the basic program, but if I try to pass a string argument using Parameters nothing changes, it just opens the program as before.  I used the following

procedure TForm1.DownloadSelItem(PID: String);
   var  AProcess    : TProcess;
       
begin
    AProcess:= TProcess.Create(nil);
    AProcess.Executable := ('get_iplayer');
    AProcess.Parameters.Add(' --get 8');
    AProcess.Options:= AProcess.Options + [poUsePipes];
    Aprocess.Execute;
     ListBox3.Items.LoadFromStream(AProcess.Output);
    Aprocess.Free;
 
end;

If I include the argument ' --get 8'  in the executable  (i.e.  ' get_iplayer --get 8')  I receive the error Executable not found which is understandable.  Is this a problem with calling  terminal programs, or is it specific to get_iplayer ?
Any help would be very welcome. 

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: passing command line arguments
« Reply #1 on: November 19, 2017, 01:54:49 pm »
The way TProcess handles Parameters might not be what you expect:
Code: Pascal  [Select][+][-]
  1.     For I:=0 to Parameters.Count-1 do
  2.       Cmd:=Cmd+' '+MaybeQuoteIfNotQuoted(Parameters[i]);
This turns your command from:
Quote
get_iplayer --get 8
to:
Quote
get_iplayer "--get 8"

Try:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DownloadSelItem(PID: String);
  2.    var  AProcess    : TProcess;
  3.        
  4. begin
  5.     AProcess:= TProcess.Create(nil);
  6.     AProcess.Executable := ('get_iplayer');
  7.     AProcess.Parameters.Add('--get');
  8.     AProcess.Parameters.Add('8');
  9.     AProcess.Options:= AProcess.Options + [poUsePipes];
  10.     Aprocess.Execute;
  11.      ListBox3.Items.LoadFromStream(AProcess.Output);
  12.     Aprocess.Free;
  13.  
  14. end;
« Last Edit: November 19, 2017, 10:30:57 pm by engkin »

gozo97

  • Newbie
  • Posts: 5
Re: passing command line arguments
« Reply #2 on: November 20, 2017, 09:00:28 pm »
Hello engkin.
Thank you for your suggestion.  It did not quite work as you had posted, but when I slightly altered it from

     aProcess.Parameters.Add('--get');
     aProcess.Parameters.Add('8');
to
     aProcess.Parameters.Add(' --get');
     aProcess.Parameters.Add(' 8');
in other words, I added spaces before the two arguments, the program accepted it!
Splitting the Parameters and adding them separately was the key to the problem.

get_iplayer is very strict on it's syntax and needed the spaces to work.  Also, if you include quotes, it interprets it as something quite different, for example, a URL), so I am rather confused about your first block of code and how you would implement it, but then I am a bit of a beginner!

Thank you very much for putting me on the right path.

 

TinyPortal © 2005-2018