Parsing command line..

I am reading the System.commandline string. Is there a straight forward way to get parse the string into an array?

[code]dim commandArgs() as string

commandArgs = split(system.commandLine, " ")[/code]

That doesn’t work if you have a space in the app name. You would have to strip app.ExecutableFile.NativePath off of the front first.

It also wouldn’t work well if there are spaces within the parameters:

echo ‘This is one parameter’

This takes me back to my days writing a BASIC language parser…

The only sure way to do it, AFAIK, is to scan forward through the line until you reach the next token. (A token is the part of the command line that indicates to the command that the following is a parameter of the command.) Then everything from the newly found token back to the previous token is one array element. If you reach the end of the command before reaching a token, then what you just scanned is the target of the command. Unfortunately, some systems/commands allow totally unordered parameters so you can’t always depend on the target being the last element in the command line.

Spaces should never be used as a determinant for parsing command lines.

https://github.com/maxvalle
RBGetOpt

Thanks all! Seems like this would be something that is bundled into XOJO but I am so new to all of this.

What kind of app are you creating?

I do use System.Commandline and have some code for testing in debugmode (IDE). I’ve never seen a property which could simply be set in the propertybox to simulate a commandline-string during development. Or … ?

some of us have to make CLI apps that can be called by CI environments like Jenkins or Bamboo.

But CLI’s give the args as an array. Or am I misremembering?

they do. I was just answering the question what type of apps I am writing that need cli args on.

Oh, that was meant for the OP.

I am building a Desktop application that needs to take command line arguments…

On OS X , Windows or Linux ?
“Normal” users won’t know how to do that

Its Windows & Mac… Yeah its an interface for other agent code to send parameters.

Thanks for the help…

Dim args As String
// Remove the application name
args = Replace(System.CommandLine, app.ExecutableFile.NativePath, “”)
// remove the space between native path and args
args = args.Trim
// if there actually were arguments
if args <> “” then
dim commandArgs() as string
// split args into array
commandArgs = split(args, " ")
dim numArgs As Integer
numArgs = uBound(commandArgs)
dim arg As String
// only care about the first arg
if numArgs >= 0 Then
arg = commandArgs(0)
handleArg(arg)
End If
end if