#1 2024-07-25 04:56:37

Uefi
Member
Registered: 2024-02-14
Posts: 34

How to parse command line parameters ?

Hello, can I use mORMot or something else to parse command line elements from a string, like string into an array ?

var
arg:Tarray<string>;
params:string
begin
params:='-o file.txt --y -v -t 1';
//parsing here
end;

Each element must be in an array

Offline

#2 2024-07-25 06:16:31

Chaa
Member
Registered: 2011-03-26
Posts: 248

Re: How to parse command line parameters ?

Offline

#3 2024-07-25 07:04:27

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

No, firstly, I still don’t understand how to use it, and secondly, it doesn’t seem to suit my task

Offline

#4 2024-07-25 07:23:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

You can parse your command line with it.

Offline

#5 2024-07-25 07:29:17

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

ab wrote:

You can parse your command line with it.

No, I need to parse it so that each parameter is in an array

Example:

params:string:='-o file.txt --y -v -t 1';

after parse

array[0]= -o file.txt
array[1]= --y
array[2]= -v
array[3]= -t 1

And I don’t really know how to do this sad

Last edited by Uefi (2024-07-25 07:38:06)

Offline

#6 2024-07-25 08:01:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

Why?
It does not make sense. You can't do anything with this array.

What you can do is actually parse the command line and get the flags and values.
I have added a regression test to show how it works:
https://github.com/synopse/mORMot2/commit/d3f0b36a

Offline

#7 2024-07-25 08:11:35

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

Okay I will explain why this is needed. There is a python4delphi library. I need to run the .py script through it, passing it launch parameters:

PythonEngine.PySys_SetArgv(ArgvCount, Argv);
PythonEngine.ExecFile(mypathtofile);

And here the launch parameters must be in an array and you also need to specify the length of the array

upd:
Thanks I tested the new method, it parses slightly incorrectly:

program Project2;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, mormot.core.os, mormot.core.text;

var
c:TExecutableCommandLine;
begin
c:=TExecutableCommandLine.Create;
c.Clear;
c.RawParams := CsvToRawUtf8DynArray('-o file.txt --y -v -t 1', ' ');
c.Parse(#10, '-', '--');
Writeln(c.Values[0]);
c.Free;
Readln;
end.

I got the result "file.txt" but it probably should be "-o file.txt"

Last edited by Uefi (2024-07-25 08:43:03)

Offline

#8 2024-07-25 09:44:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

No, it works as expected.
There are Names[] and Values[] properties.

You are trying to use a hammer on a screw.
It won't work.
Write your own parser.

Offline

#9 2024-07-25 10:31:27

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

I also noticed that when using c.Args[0]); an error is thrown (

Offline

#10 2024-07-25 12:16:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

Yes because there is no plain argument!

Offline

#11 2024-07-25 12:20:42

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

ab wrote:

Yes because there is no plain argument!

I still don’t understand how to parse it correctly smile

I think I did it, only the problem remains: I don't know how to properly convert to PPWideChar smile

function ParseCommandLine(const param: string): PPWideChar;
var
ar:TRawUtf8DynArray;
j:integer;
begin
ar:=CsvToRawUtf8DynArray(param, ' -');
for j:=1 to High(ar) do
ar[j]:='-'+ar[j];
Result:=ar;
end;

Last edited by Uefi (2024-07-25 13:07:03)

Offline

#12 2024-07-26 11:41:06

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

Yeah, I think I did it and it works well, but I don’t like that there are a lot of transformations sad

uses mormot.core.text, mormot.core.base;

function ParseCommandLine(const param: string; out acount: Integer): PPWideChar;
var
par:TRawUtf8DynArray;
arg:array of PWideChar;
j:integer;
begin
par:=CsvToRawUtf8DynArray(param, ' -');
acount:=Length(par);
SetLength(arg, acount);
arg[0]:=PWideChar(wideString(Trim(par[0])));
for j:=1 to acount-1 do
arg[j]:=PWideChar(wideString(Trim('-'+par[j])));
Result:=PPWideChar(arg);
end;

Maybe it will be useful to someone

Last edited by Uefi (2024-07-26 11:43:04)

Offline

#13 2024-07-26 14:35:38

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

This forum is not the right place to debug and give advice your own code, but using a temporary WideString() then returning a pointer to it is wrong because the WideString instance is released when the function exits.
You are likely to have wrong content in the arguments, or even random GPF.

Offline

#14 2024-07-26 14:47:54

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

Well, if you could tell me how to do it better, I’m not very quick-witted

Offline

#15 2024-07-26 18:10:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

Returns the result as TUnicodeStringDynArray: they could be mapped as PPWideChar directly, and the array will maintain the instances.

Offline

#16 2024-07-27 04:49:50

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

Strange, I tried direct conversion and it doesn’t work sad

uses mormot.core.text, mormot.core.base;

function ParseCommandLine(const param: string; out acount: Integer): PPWideChar;
var
par:TRawUtf8DynArray;
j:integer;
begin
par:=CsvToRawUtf8DynArray(param, ' -');
acount:=Length(par);
for j:=1 to acount-1 do
par[j]:=Trim('-'+par[j]));
Result:=PPWideChar(par);
end;

Offline

#17 2024-07-27 07:16:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: How to parse command line parameters ?

This is how pascal strings and pointers work.

Offline

#18 2024-07-27 07:26:55

Uefi
Member
Registered: 2024-02-14
Posts: 34

Re: How to parse command line parameters ?

ab wrote:

This is how pascal strings and pointers work.

I have already tried with pointers and all sorts of transformations, more than one option directly and it didn’t work, why I don’t know

Offline

Board footer

Powered by FluxBB