Recent

Author Topic: CLI | Wildcard parameters proccessing  (Read 993 times)

Anton Shevtsov

  • New Member
  • *
  • Posts: 15
CLI | Wildcard parameters proccessing
« on: September 11, 2024, 11:03:57 am »
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][+][-]
  1. ./mytool -f *.png

If debug ParamStr

Code: Pascal  [Select][+][-]
  1.    writeln('ParamCount = ',ParamCount);
  2.   for x := 1 to ParamCount do writeln('ParamStr(',x,') = ',ParamStr(x));

 i see 4 parameters

Quote
ParamCount = 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][+][-]
  1. ./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

  • Hero Member
  • *****
  • Posts: 7649
Re: CLI | Wildcard parameters proccessing
« Reply #1 on: September 11, 2024, 11:17:50 am »
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Anton Shevtsov

  • New Member
  • *
  • Posts: 15
Re: CLI | Wildcard parameters proccessing
« Reply #2 on: September 11, 2024, 11:26:49 am »
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

https://wiki.freepascal.org/Command_line_parameters_and_environment_variables#Check_for_a_parameter

MarkMLl

  • Hero Member
  • *****
  • Posts: 7649
Re: CLI | Wildcard parameters proccessing
« Reply #3 on: September 11, 2024, 12:03:09 pm »
https://wiki.freepascal.org/Command_line_parameters_and_environment_variables#Check_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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bart

  • Hero Member
  • *****
  • Posts: 5357
    • Bart en Mariska's Webstek
Re: CLI | Wildcard parameters proccessing
« Reply #4 on: September 11, 2024, 03:03:07 pm »
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

  • Hero Member
  • *****
  • Posts: 7649
Re: CLI | Wildcard parameters proccessing
« Reply #5 on: September 11, 2024, 05:53:49 pm »
It'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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: CLI | Wildcard parameters proccessing
« Reply #6 on: September 12, 2024, 01:56:58 am »
if 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'.

How 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][+][-]
  1. count := ParamCount;
  2. files := TStringList.Create;
  3. x := 1;
  4. while x <= count do
  5. begin
  6.   ...
  7.   param := ParamStr(x); Inc(x);
  8.   if param = '-f' then
  9.   begin
  10.     param := ParamStr(x); Inc(x);
  11.     if (Pos('*', param) <> 0) or (Pos('?', param) <> 0) then
  12.     begin
  13.       folder := IncludeTrailingPathDelimiter(ExtractFilePath(param));
  14.       if FindFirst(param, faAnyFile, sr) = 0 then
  15.       try
  16.         repeat
  17.           if (sr.Name <> '.') and (sr.Name <> '..') and ((sr.Attr and faDirectory) = 0) then
  18.             files.Add(folder + sr.Name);
  19.         until FindNext(sr) = 0;
  20.       finally
  21.         FindClose(sr);
  22.       end;
  23.     end else
  24.     begin
  25.       files.Add(param);
  26.       while x <= count do
  27.       begin
  28.         param := ParamStr(x);
  29.         if param[1] = '-' then Break;
  30.         files.Add(param);
  31.         Inc(x);
  32.       end;
  33.     end;
  34.   end
  35.   ...
  36. end;
  37.  
  38. // use files as needed...
  39.  
  40. files.Free;
« Last Edit: September 12, 2024, 07:33:32 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 5357
    • Bart en Mariska's Webstek
Re: CLI | Wildcard parameters proccessing
« Reply #7 on: September 12, 2024, 11:21:42 am »
How 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

  • Hero Member
  • *****
  • Posts: 713
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: CLI | Wildcard parameters proccessing
« Reply #8 on: September 12, 2024, 07:18:40 pm »

Code: Pascal  [Select][+][-]
  1. ./mytool -f *.png

If debug ParamStr

Code: Pascal  [Select][+][-]
  1.    writeln('ParamCount = ',ParamCount);
  2.   for x := 1 to ParamCount do writeln('ParamStr(',x,') = ',ParamStr(x));

 i see 4 parameters

Quote
ParamCount = 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][+][-]
  1. ./mytool "-f *.png"

or


Code: Pascal  [Select][+][-]
  1. ./mytool -f "*.png"
-ASB: https://www.BrainWaveCC.com/

Lazarus v3.5.0.0 (2216170cde) / FPC v3.2.3-1387-g3795cadbc8
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: CLI | Wildcard parameters proccessing
« Reply #9 on: September 12, 2024, 07:36:52 pm »
What happens if you put quotes around the command?

Code: Pascal  [Select][+][-]
  1. ./mytool "-f *.png"

That would be passed to the app as a single parameter, not as separate parameters.

or

Code: Pascal  [Select][+][-]
  1. ./mytool -f "*.png"

The bash Shell doesn't expand wildcards inside of a quoted string.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

MarkMLl

  • Hero Member
  • *****
  • Posts: 7649
Re: CLI | Wildcard parameters proccessing
« Reply #10 on: September 12, 2024, 07:47:14 pm »
The bash Shell doesn't expand wildcards inside of a quoted string.

OP says

Quote
p.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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018