How to get anything useful from system.commandline?

this is a desktop app, but I still need to be able to parse out some command line switches. The problem seems to be that as soon as any parameter has a space in it, escaped or in quotes or otherwise, it is impossible to parse out what is sent.

If I just messageBox( system.commandLine) in the app.opening event i get a string of what is passed. But anything that is escaped is already unescaped when it is placed in system.command line. So a path like: /Users/XojoGuy/Projects\ that\ have\ a\ space/mything.whatever just becomes the flat string with the spaces unescaped: /Users/XojoGuy/Projects that have a space/mything.whatever

Which is great that it’s unescaped but I can’t parse it then into individual parts. So the next attempt is to put quotes around them but those too are removed prior to it being placed in system.commandLine.

Is it actually possible to parse out the individual parts of the command line? Did I miss something obvious? There seems to be no way to split the string on spaces since there is no way to know what spaces belong in which individual parameter being passed.

A command line app sends me an array and so there is no problem, I want to make an array from the same as passed by system.commandLine in a desktop app but it appears to be impossible? Any suggestions or pointers to other ways of doing this would be greatly appreciated, otherwise it’s off to make a bug report I go.

Does app.ExecutableFile.NativePath match the first part of the commandline? If so, look for the first switch and anything in between is a file name/path. Although I would expect to get an OpenDocument event with that info. Then parse out the switches.

1 Like

On Windows I use

Var args As String = System.CommandLine.TrimLeft(Chr(34) + App.ExecutableFile.NativePath + Chr(34) + " ")

Which leaves me with just the args as a string.

I think in pure Xojo code you have to parse (which is terrible since the command line rules are quite complex).

You can also do it with plugin like in our UtilsLib:

https://www.einhugur.com/Html/UtilsLib/EinhugurEventsCommandLineArguments2.html

I am sure Christian has something for it as well. So if you got either of the packs then it will probably solve the problem for you.

3 Likes

The problem was that you can’t just parse them as a string which I was trying to make clear in the OP. Because the text is unescaped before you get it, and yet not split into an array by that process there is no way to do so. It does not appear possible to get anything useful out of system.command line. If it was passed unescaped then I could escape and parse it myself, but because it’s in a half state of parsing any spaces anywhere in it even if they were escaped or in quotes when they were passed, you cannot figure it out.

Barring Xojo changing to send it before unescaping it the solution is as Björn suggested. It appears both his and Christians plugins have ways to get the command line as an array, the same way that it is sent to console applications. Thanks!

On macOS you can use the NSProcessInfo class to get the arguments already as an array.

// -- Collect the launch arguments.
Dim inArgumentArray as integer = NSProcessInfo_arguments( NSProcessInfoCurrent )
Dim argsAsString(-1) as string

Dim n as integer = NSArray_count( inArgumentArray ) -1
redim argsAsString( n )

For l as integer = 0 to n
  argsAsString( l ) = NSArray_stringAtIndex( inArgumentArray, l )
Next

“system.commandLine” make no sense, “app.commandLineArguments” is a more appropriate name as they’re for the application NOT the system… Oh it’s a API 2.0 name… fffssss…

Pure Xojo implementation, cross platform: