Recent

Author Topic: Parsing command line string  (Read 772 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 388
Parsing command line string
« on: November 07, 2025, 10:22:11 pm »
I have a command line string, for example:
Quote
xcopy C:\Users\YourUser\Projects\ "D:\Archives\Projects Backup\" /E /H /Y
I need to split it into array of strings, so
Quote
Arr[0] = 'xcopy'
Arr[1] = 'C:\Users\YourUser\Projects\'
Arr[2] = 'D:\Archives\Projects Backup\'
...
Is there a ready solution for that?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2505
    • http://www.cdbc.dk
Re: Parsing command line string
« Reply #1 on: November 07, 2025, 10:44:54 pm »
Hi
String.Split
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

LemonParty

  • Sr. Member
  • ****
  • Posts: 388
Re: Parsing command line string
« Reply #2 on: November 07, 2025, 11:38:27 pm »
Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

simone

  • Hero Member
  • *****
  • Posts: 681
Re: Parsing command line string
« Reply #3 on: November 07, 2025, 11:51:15 pm »
In this case, keep in mind that if you use TStringHelper.Split (or SplitString), specifying the space as character separator, when parameters have spaces inside them (as in the path in your example), you will not get a correct result (in your example, the path parameter between double quotes will be split into two strings, at the space).
« Last Edit: November 08, 2025, 12:50:45 am by simone »
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5640
    • Bart en Mariska's Webstek
Re: Parsing command line string
« Reply #4 on: November 08, 2025, 12:09:26 am »
Doesn't ParamStr() do what you want?

Bart

simone

  • Hero Member
  • *****
  • Posts: 681
Re: Parsing command line string
« Reply #5 on: November 08, 2025, 12:16:24 am »
I think the OP wants to process a string containing a command line, not the parameters supplied to the program itself, when launched from the prompt.
« Last Edit: November 08, 2025, 12:18:33 am by simone »
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

Thausand

  • Sr. Member
  • ****
  • Posts: 437
Re: Parsing command line string
« Reply #6 on: November 08, 2025, 02:46:56 am »
In this case, keep in mind that if you use TStringHelper.Split (or SplitString), specifying the space as character separator, when parameters have spaces inside them (as in the path in your example), you will not get a correct result (in your example, the path parameter between double quotes will be split into two strings, at the space).
That why is invent overload and can have AQuoteStart/AQuoteEnd/AQuote (https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.split.html)

Thausand

  • Sr. Member
  • ****
  • Posts: 437
Re: Parsing command line string
« Reply #7 on: November 08, 2025, 07:01:40 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 18475
  • Here stood a man who saw the Elbe and jumped it.
Re: Parsing command line string
« Reply #8 on: November 08, 2025, 07:21:13 am »
We have the getopts unit in the standard rtl:
https://www.freepascal.org/docs-html/current/rtl/getopts/index.html
Used by the compiler itself and several of the standard tools. Example included in the docs.

Furthermore, check out the console application template in Lazarus: New|Console Application
That already comes with command line parsing logic:
https://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.html

Also have a look at:
https://github.com/benibela/rcmdline?tab=readme-ov-file
The latter has more options and is easy to use.
« Last Edit: November 08, 2025, 08:15:15 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

simone

  • Hero Member
  • *****
  • Posts: 681
Re: Parsing command line string
« Reply #9 on: November 08, 2025, 09:39:31 am »
In this case, keep in mind that if you use TStringHelper.Split (or SplitString), specifying the space as character separator, when parameters have spaces inside them (as in the path in your example), you will not get a correct result (in your example, the path parameter between double quotes will be split into two strings, at the space).
That why is invent overload and can have AQuoteStart/AQuoteEnd/AQuote (https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.split.html)

You're right, I forgot the following:

"If AQuoteStart and AQuoteEnd are supplied, then no splitting will be performed between AQuoteStart and AQuoteEnd characters."
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

jcmontherock

  • Sr. Member
  • ****
  • Posts: 320
Re: Parsing command line string
« Reply #10 on: November 08, 2025, 11:16:38 am »
As well you can use TStringList.
Windows 11 UTF8-64 - Lazarus 4.4-64 - FPC 3.2.2

LemonParty

  • Sr. Member
  • ****
  • Posts: 388
Re: Parsing command line string
« Reply #11 on: November 08, 2025, 01:21:47 pm »
I used SplitCommandLine, works fine for my needs.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018