You are not logged in.
Pages: 1
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
Maybe this helps:
https://blog.synopse.info/?post/2023/04 … n-mORMot-2
Offline
Maybe this helps:
https://blog.synopse.info/?post/2023/04 … n-mORMot-2
No, firstly, I still don’t understand how to use it, and secondly, it doesn’t seem to suit my task
Offline
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
Last edited by Uefi (2024-07-25 07:38:06)
Offline
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
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
I also noticed that when using c.Args[0]); an error is thrown (
Offline
Yes because there is no plain argument!
I still don’t understand how to parse it correctly
I think I did it, only the problem remains: I don't know how to properly convert to PPWideChar
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
Yeah, I think I did it and it works well, but I don’t like that there are a lot of transformations
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
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
Well, if you could tell me how to do it better, I’m not very quick-witted
Offline
Strange, I tried direct conversion and it doesn’t work
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
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
Pages: 1