CLI | Wildcard parameters proccessing lazarus / 21-General ---------------------------------------------------------------------- Anton Shevtsov · 2024-09-11 09:03 Hi, I want get wildcard value for my prog, but... Step-by-step Quote[anton@dell cli]$ ls -l /tmp/*.png -rw-r--r-- 1 anton anton  89658 сен  9 14:30 /tmp/1_2024-09-09_14-29.png -rw-r--r-- 1 anton anton 138242 сен  9 14:30 /tmp/2_2024-09-09_14-30.png -rw-r--r-- 1 anton anton  70511 сен 10 16:20 /tmp/lpd_2024-09-10_16-20.png Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool -f *.png If debug ParamStr Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    writeln('ParamCount = ',ParamCount);  for x := 1 to ParamCount do writeln('ParamStr(',x,') = ',ParamStr(x));  i see 4 parameters QuoteParamCount = 4 ParamStr(1) = -f ParamStr(2) = /tmp/1_2024-09-09_14-29.png ParamStr(3) = /tmp/2_2024-09-09_14-30.png ParamStr(4) = /tmp/lpd_2024-09-10_16-20.png if i try getting with GetOptionValue('f', 'file') i get only first filename, if GetOptionValues('f', 'file') i get array with first filename too.. How i can get all filelist or full wildcard *.txt (for search files in code later)? p.s. i know about quotes Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool -f '*.png'but i want like md5summ and other tool way Something like this Quote[anton@dell cli]$ md5sum /tmp/*png d3074aae48ffd9115562c52ab1e05960  /tmp/1_2024-09-09_14-29.png dcecf3c78ab2468d5affedbd0c58e9d8  /tmp/2_2024-09-09_14-30.png 7b11b94b02cbc80efb3cd942a22994ad  /tmp/lpd_2024-09-10_16-20.png ---------------------------------------------------------------------- MarkMLl · 2024-09-11 09:17 Get each one using ParamStr(). What is this GetOptionValue() of which you speak? Where are you getting it from, and where is the compilable test program that shows us what you're doing? MarkMLl ---------------------------------------------------------------------- Anton Shevtsov · 2024-09-11 09:26 Quote from: MarkMLl on September 11, 2024, 11:17:50 amGet each one using ParamStr(). What is this GetOptionValue() of which you speak? Where are you getting it from, and where is the compilable test program that shows us what you're doing? MarkMLl https://wiki.freepascal.org/Command_line_parameters_and_environment_va riables#Check_for_a_parameter ---------------------------------------------------------------------- MarkMLl · 2024-09-11 10:03 Quote from: Anton Shevtsov on September 11, 2024, 11:26:49 amhttps://w iki.freepascal.org/Command_line_parameters_and_environment_variables#C heck_for_a_parameter That's for Lazarus, not general FPC etc., for which the available facilities are documented at https://www.freepascal.org/docs.html Post a program demonstrating the problem. MarkMLl ---------------------------------------------------------------------- Bart · 2024-09-11 13:03 It's the shell (Bash?) that expands the wildcards. If you would run the same program on Windows you would get: Code: [Select]ParamCount = 2 ParamStr(1) = -f ParamStr(2) = *.png This is especially annoying if e.g. your program wants to perform an action on each .png whilst recuring directories. Bart ---------------------------------------------------------------------- MarkMLl · 2024-09-11 15:53 Quote from: Bart on September 11, 2024, 03:03:07 pmIt's the shell (Bash?) that expands the wildcards. I assumed he knew that because he said that he was aware of the quoting issue. MarkMLl ---------------------------------------------------------------------- Remy Lebeau · 2024-09-11 23:56 Quote from: Anton Shevtsov on September 11, 2024, 11:03:57 amif i try getting with GetOptionValue('f', 'file') i get only first filename, if GetOptionValues('f', 'file') i get array with first filename too.. Makes sense, because the Shell is expanding the wildcard before the parameters are sent to your app.  So GetOptionValue/s have no idea that a wildcard was ever used, they are just pulling whatever the next parameter is after '-f'. Quote from: Anton Shevtsov on September 11, 2024, 11:03:57 amHow i can get all filelist or full wildcard *.txt (for search files in code later)? Use ParamStr() directly.  Loop through ParamCount(), and when you encounter the '-f' parameter then look at the next parameter.  If it has a wildcard then the Shell didn't expand it so you can use that parameter value with FindFirst/Next() (or equivalent) to loop through the file paths yourself.  Otherwise, the Shell did expand the wildcard (or no wildcard was provided) so look at all of the parameters following '-f' until you reach the next option or the parameter list is exhausted. For example, something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i count := ParamCount;files := TStringList.Create;x := 1;while x <= count dobegin  ...  param := ParamStr(x); Inc(x);  if param = '-f' then  begin     param := ParamStr(x); Inc(x);    if (Pos('*', param) <> 0) or (Pos('?', param) <> 0) then    begin      folder := IncludeTrailingPathDelimiter(ExtractFilePath(param));      if FindFirst(param, faAnyFile, sr) = 0 then      try        repeat        if (sr.Name <> '.') and (sr.Name <> '..') and ((sr.Attr and faDirectory) = 0) then            files.Add(folder + sr.Name);        until FindNext(sr) = 0;      finally        FindClose(sr);      end;  end else    begin      files.Add(param);      while x <= count do    begin        param := ParamStr(x);        if param[1] = '-' then Break;        files.Add(param);        Inc(x);      end;    end;  end  ...end; // use files as needed... files.Free; ---------------------------------------------------------------------- Bart · 2024-09-12 09:21 Quote from: Remy Lebeau on September 12, 2024, 01:56:58 amQuote from: Anton Shevtsov on September 11, 2024, 11:03:57 amHow i can get all filelist or full wildcard *.txt (for search files in code later)? Use ParamStr() directly. That won't help, as the shell already expanded the wildcard (see my reply #4 in this thread). Bart ---------------------------------------------------------------------- ASBzone · 2024-09-12 17:18 Quote from: Anton Shevtsov on September 11, 2024, 11:03:57 am Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool -f *.png If debug ParamStr Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    writeln('ParamCount = ',ParamCount);  for x := 1 to ParamCount do writeln('ParamStr(',x,') = ',ParamStr(x));  i see 4 parameters QuoteParamCount = 4 ParamStr(1) = -f ParamStr(2) = /tmp/1_2024-09-09_14-29.png ParamStr(3) = /tmp/2_2024-09-09_14-30.png ParamStr(4) = /tmp/lpd_2024-09-10_16-20.png What happens if you put quotes around the command? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool "-f *.png" or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool -f "*.png" ---------------------------------------------------------------------- Remy Lebeau · 2024-09-12 17:36 Quote from: ASBzone on September 12, 2024, 07:18:40 pmWhat happens if you put quotes around the command? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool "-f *.png" That would be passed to the app as a single parameter, not as separate parameters. Quote from: ASBzone on September 12, 2024, 07:18:40 pmor Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./mytool -f "*.png" The bash Shell doesn't expand wildcards inside of a quoted string. ---------------------------------------------------------------------- MarkMLl · 2024-09-12 17:47 Quote from: Remy Lebeau on September 12, 2024, 07:36:52 pmThe bash Shell doesn't expand wildcards inside of a quoted string. OP says Quotep.s. i know about quotes so I think we really need to see the code that he says is failing, and know his precise expectations. MarkMLl