Recent

Author Topic: [Solved] Parse a string like a command-line  (Read 1549 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2387
    • UVviewsoft
[Solved] Parse a string like a command-line
« on: September 19, 2021, 10:18:26 pm »
I want to add to CudaText parsing of custom string (will be set in the toolbar button by user) as command line.
Ie I need to split a string to N params like ParamCount/ParamStr do.
How to do it?
« Last Edit: September 19, 2021, 10:41:55 pm by Alextp »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Parse a string like a command-line
« Reply #1 on: September 19, 2021, 10:25:26 pm »
 * on Unix, the shell splits, and the FPC application gets the separated values directly. There are some routines that TProcess uses for some minor splitting in now deprecated routines, but that will never be a full shell compatible. That realization is pretty much why they were deprecated
 * for windows see procedure setup_arguments in syswin.inc:219.


Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Parse a string like a command-line
« Reply #2 on: September 19, 2021, 10:40:01 pm »
Hey Alex,

From my understanding, on any command line interpreter, be it on Windows, Linux or even MacOS, the params are splitted by white chars, or in other words, spaces and/or tabs.

I'm assuming here that you wouldn't need to parse multiline strings, cuz that would have a higher level of complexity.

If you want to do it yourself, just split a string by spaces or tabs with one of the string manipulation functions that FPC has.

If I'm not mistaken, there are functions that accept more than one character to look for in terms of the delimiters.
I'm not quite sure if String.Split type helper does it like this, but I'll leave it for you to explore.

This will give you an array of strings you can then use. This will not associate values with params unless in the format --longparam=value since this has no spaces.
If you want to associate params with values, you'll have to add a bit more code to make sense of each param and see if it has/needs a value.

You will also have to parse stuff inside single or double quotes as one single param even if it contains spaces, that is if you're allowing a single or double quotes params.

So all, in all, it would be more practical to use a custom made function because of the possibility of single or double quoted params.

Hope this helps in any way!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

AlexTP

  • Hero Member
  • *****
  • Posts: 2387
    • UVviewsoft
Re: Parse a string like a command-line
« Reply #3 on: September 19, 2021, 10:40:16 pm »
jamie, thanks. It works:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   s: string;
  4.   ar: TStringArray;
  5. begin
  6.   s:= 'tt   "ee ee2 ee3"   tt2    tt3';
  7.   ar:= s.Split(' ', '"', '"', TStringSplitOptions.ExcludeEmpty);
  8.   writeln;
  9. end;
  10.  

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Parse a string like a command-line
« Reply #4 on: September 19, 2021, 10:56:51 pm »
Hey Alex,

jamie, thanks. It works:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   s: string;
  4.   ar: TStringArray;
  5. begin
  6.   s:= 'tt   "ee ee2 ee3"   tt2    tt3';
  7.   ar:= s.Split(' ', '"', '"', TStringSplitOptions.ExcludeEmpty);
  8.   writeln;
  9. end;
  10.  

Sorry to be a bit picky( :) ), but that doesn't account for tabs as separators nor does it account for single quotes as string aggregators.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

AlexTP

  • Hero Member
  • *****
  • Posts: 2387
    • UVviewsoft
Re: [Solved] Parse a string like a command-line
« Reply #5 on: September 19, 2021, 11:18:05 pm »
Gus, that is like in FPC handling of command line, as I see the FPC code now.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: [Solved] Parse a string like a command-line
« Reply #6 on: September 19, 2021, 11:31:59 pm »
Hey Alex,

Gus, that is like in FPC handling of command line, as I see the FPC code now.

I'm not gonna fight you on this, cuz you didn't mention if you're gonna allow tabs and single quotes, so if that satisfies you, I'm all for it and woosh away ;)

On a side note, Linux BASH does not allow you to type in tabs, but that is because it triggers the completion when you're at a terminal. Still need to test it on a shell script...

On another side note (*-please excuse me being so pedantic), this is a perfectly valid command line:
Code: Pascal  [Select][+][-]
  1. var
  2.   s: String;
  3. begin
  4.   s:= 'aaa "bbb ccc ddd" --long-format=''value with spaces in between single quotes'' -v -h';
  5. end;
But your split solution will not parse the mix of single and double quotes.

Cheers,
Gus

* - It's not my intention to criticise you, I'm just trying to chase completion of options when you have a user input the command string.
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

AlexTP

  • Hero Member
  • *****
  • Posts: 2387
    • UVviewsoft
Re: [Solved] Parse a string like a command-line
« Reply #7 on: September 20, 2021, 12:17:29 am »
Gus,
such quotes are not supported by CudaText, so for my application it's OK.
For FPC this parsing is not implemented at least for Windows AFAIK.


Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: [Solved] Parse a string like a command-line
« Reply #9 on: September 20, 2021, 12:31:29 am »
Hey Alex,

such quotes are not supported by CudaText, so for my application it's OK.
For FPC this parsing is not implemented at least for Windows AFAIK.

That's quite Ok and thanks for the clarification!! :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018