Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have limited very limited knowledge on parsing data in vb.net 2013 and hope someone with more experience can advise:

Overview
I would like to add three command line switches and values to my console application and then parse them into variables, for onward use within my program. The command line options would hopefully look like this:

my.exe /d dateserial /p plaintext

or
my.exe /d dateserial /c cyphertext


These options depend on whether the program was encrypting or de-crypting any given string. I would also like in incorporate some basic error handling, like if /c /p switches exist on the same command line, stop the program and display usage.


My project currently looks like this:

VB
Dim dtext As String = String.Empty 'date serial string 
Dim ptext As String = String.Empty 'plain text string
Dim ctext As String = String.Empty 'cypher text string 

Dim clArgs() As String = Environment.GetCommandLineArgs()

If clArgs.Count() = 5 Then
   For index As Integer = 1 To 3 Step 1
       If clArgs(index) = "d/"; Then
          dtext = clArgs(index + 1)
          MsgBox(dtext) ' To ensure variables get populated correctly.  
       Else
           Console.WriteLine("Usage: my.exe /d dateserial /p plaintext or my.exe /d dateserial /c cyphertext")
       End If
   Next
End If


The code snip-it above only partly completes the task, so thanks in advance to anyone that can advise on a more complete and robust solution.
Posted
Updated 25-Feb-15 8:55am
v2

Have a look at Regex.Split():

https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.split%28v=vs.110%29.aspx[^]

I think that's what you'll need.

You can do stuff like:

VB
Dim yourArgs() as String = Regex.Split(clArgs, "/")

For Each arg as String in yourArgs
   if arg.Contains("c ") Then
     ' Your code here
   End If
Next


This is just a quick and dirty example of how to use regex.split() and .Contains(). There are more elegant ways of doing this, and you should also have a look at String.Substring() and String.Replace().

- Pete
 
Share this answer
 
v3
Please see my article, and also another article I reference and recommend in it: Enumeration-based Command Line Utility[^].

I provide extremely easy-to-use library. Now, just in case: please don't tell me "this is C#, not VB.NET". First, in .NET, this is absolutely not important; C# is always better because it is standardized, in contrast to VB.NET. Besides, you can automatically translate any assembly to VB.NET with very good quality (use ILSpy, in particular).

—SA
 
Share this answer
 
Thank you for the ideas guys, they were very much appreciated. This is a vb.net app. However after further googling and tweaking of the parameters, I achieved the desired result with the following code. I also wanted to incorporate some basic error handling, so as my console program would only ever have 5 command line args and these fields needed to be fixed, I came up with this primitive, but effective solution:
VB
Dim dtext As String = String.Empty
Dim ptext As String = String.Empty
Dim ctext As String = String.Empty

Dim clArgs() As String = Environment.GetCommandLineArgs()

If clArgs.Count() <> 5 Then
   Console.WriteLine("Correct usage:""/p plaintext /d dateserial""or")
   Console.WriteLine("Correct usage:""/c cyphertext /d dateserial")
End If

If Not (clArgs.Contains("/p") Or clArgs.Contains("/c")) And (clArgs.Contains("/d")) Then
   Exit Sub
End If

If clArgs.Contains("/p") And clArgs.Contains("/d") Then
   For index As Integer = 1 To 3 Step 2
      If clArgs(index) = "/p" Then
         ptext = clArgs(index + 1)
         dtext = clArgs(index + 3)
         Console.WriteLine(ptext)
         Console.WriteLine(dtext)
      End If
   Next
End If

If clArgs.Contains("/c") And clArgs.Contains("/d") Then
   For index As Integer = 1 To 3 Step 2
      If clArgs(index) = "/c" Then
         ctext = clArgs(index + 1)
         dtext = clArgs(index + 3)
         Console.WriteLine(ctext)
         Console.WriteLine(dtext)
      End If
   Next
End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900