Answered by:
Parsing command line parameters

Question
-
Hi all,
I am using VB 2010 to develop a console app which accepts parameters using the following format:
cmd1=value1<sp>cmd2=value2...
If my app accepts these parameters:
s - source folder specification
f - binary representation of flags
Then a valid command line could be
MyConAppEXE s=c:\work f=10001
I have the following code to parse the command line:
Dim fields() As String
Using ms As New System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(Environment.CommandLine))
Using parser As New Microsoft.VisualBasic.FileIO.TextFieldParser(ms, System.Text.Encoding.Default)
parser.SetDelimiters(" ", "=")
parser.HasFieldsEnclosedInQuotes = True
fields = parser.ReadFields
End Using
End Using
The above produces an array in the following format:
(0) Executable path & name
(1) First switch
(2) First value
(3) Second switch
(4) Second value
And so on. The issue I have is that when the app is run without any parameters it produces the following array:
(0) Executable path & name
(1) Empty string
Although not a deal breaker, it is a bit of a nuisance having to test for 1 or 2 elements to determine whether no parameters were passed. I've tried determining why the parse code creates this empty element, but I have not been successful. As a kluge I tried scanning the array from last to first element with a FOR loop and removing the empty element by using a redim preserve statement but this does not work. The code does not produce any kind of error or message but when I check the array's length it still is 2 even though in theory it should be 1. Here is that code, just for completeness, although I would be looking for a better solution (if there is one):
'Scan the array to eliminate empty elements. For i As Integer = fields.Length - 1 To 0 Step -1 'If this element is blank/empty eliminate it. If fields(i).Trim.Length() = 0 Then ReDim Preserve fields(fields.Length() - 1) End If Next i
Another attempt I made was setting the TrimWhiteSpace property to True without any difference in the outcome.
Does anyone have any remarks or suggestions? Perhaps a better way of doing the parsing or how to eliminate the empty array element, if at all possible. As usual, thank you for your time and assistance. Saga
Insanity is the prelude to discovery
Friday, April 4, 2014 8:41 PM
Answers
-
Haha,
I already ran into this.
The reason in simple, Windows add a space after the file name ...
so, just Trim the string and you won't have this empty field anymore:
....GetBytes(Environment.CommandLine.Trim))
- Edited by Crazypennie Sunday, April 6, 2014 1:40 PM 213456
- Proposed as answer by Mr. Monkeyboy Sunday, April 6, 2014 2:40 PM
- Marked as answer by SagaV9 Monday, April 7, 2014 7:54 PM
Sunday, April 6, 2014 1:33 PM
All replies
-
I'm not sure what the issue is.
1. I tested the code in a WinForm app. I launched the WinForm app using the command prompt and the label displayed the count for the array as 1. Then I launched the same app using the Run application which I did after I created the following image. The count for the array became 2 for some reason.
2. I created a console app with the code. I launched the console app using the command prompt. I never saw the console app launch but for some reason the number 1 displayed on the next line of the command prompt app. Maybe when a command prompt is running and a console app is launched by it then it becomes the console app or something weird. I'm not familiar with console apps. Anyhow I then launched the console app with the Run application like in the bottom pic in the image below and the number 2 appeared for the count of the array.
So I've zero idea why launching an app using the command prompt is different then when using the Run application.
Also when I use task manager it shows Run as an app running on my desktop when I launch Run but when I right click on it and go to process task manager shows it as explorer.exe/Windows Explorer. And if I execute the console app by clicking on it when it is displayed in Windows Explorer a 2 is displayed for the arrays count again.
Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.
- Edited by Mr. Monkeyboy Saturday, April 5, 2014 5:28 AM
Saturday, April 5, 2014 5:05 AM -
The issue I have is that when the app is run without any parameters it produces the following array:
(0) Executable path & name
(1) Empty stringProbably the simplest test for this case is
If My.Application.CommandLineArgs.Count > 0 Then ...
Certainly that is simpler than any code that processes the array.- Proposed as answer by Reed KimbleMVP Saturday, April 5, 2014 4:24 PM
Saturday, April 5, 2014 5:19 AM -
First of, thanks for giving this issue time. I did all my testing in the IDE. I am going to build a small test app like you did, buld it and test from there. Yeah, it is very unusual and would like to do more research and experimenting before I move on. I'll post any worthwhile findings here. Saga
Insanity is the prelude to discovery
Saturday, April 5, 2014 11:26 PM -
This is very interesting. Yes, if I check whether there are any parameters to begin with I can ether go ahead and get if there are or simply return a more indicative data structure. Let me look at this further and will follow up. Thanks! Saga
Insanity is the prelude to discovery
Saturday, April 5, 2014 11:29 PM -
Haha,
I already ran into this.
The reason in simple, Windows add a space after the file name ...
so, just Trim the string and you won't have this empty field anymore:
....GetBytes(Environment.CommandLine.Trim))
- Edited by Crazypennie Sunday, April 6, 2014 1:40 PM 213456
- Proposed as answer by Mr. Monkeyboy Sunday, April 6, 2014 2:40 PM
- Marked as answer by SagaV9 Monday, April 7, 2014 7:54 PM
Sunday, April 6, 2014 1:33 PM -
Haha,
I already ran into this.
The reason in simple, Windows add a space after the file name ...
so, just Trim the string and you won't have this empty field anymore:
....GetBytes(Environment.CommandLine.Trim))
Man CP. That's really so simple it's funny!Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.
Sunday, April 6, 2014 3:14 PM -
Ouch! Now I can't wait till Monday to get to the office to try this. Saga
Insanity is the prelude to discovery
Monday, April 7, 2014 3:20 AM -
Crazypennie, thanks again. For my given situation yours was the most practical solution.
I will also keep Acamar's solution in mind as it makes sense to test whether there are any switches provided even before I go ahead and attempt to process them with code. Saga
Insanity is the prelude to discovery
Monday, April 7, 2014 7:59 PM