Nammed Arguments in VB.NET??

G

Guest

Hello All,

I onced used a very efficient method in VBScript to retrive nammed arguments
passed on the commanline using:
WScript.Arguments.Named.Item ("ArgumenName").

That is, the call to the script would be somthing like:
script.wsf /ArgName:"Value of argument"

Does anyone know if there is an equivalent in VB.NET?

BTW: I am currently using System.Environment.GetCommandLineArgs and I have
also used the Main (args()) constructor...

Any comments are greatly appreciated!
Hanika
 
J

Jay B. Harlow [MVP - Outlook]

Hanika,
I don't know of any pre-built functionality, however it should not be that
hard to create something:

For example you could use a regular expression to parse each parameter.

Something like:

Imports System.Text.RegularExpressions

Const pattern As String = "^/(?<name>\w+)(?:\:(?<value>.+)$|$)"
Dim argRegEx As New Regex(pattern, RegexOptions.Compiled)
For Each arg As String In Environment.GetCommandLineArgs()
Debug.WriteLine(Nothing, arg)
Debug.Indent()
With argRegEx.Match(arg)
If .Success Then
Debug.WriteLine(.Groups("name"), "name")
Debug.WriteLine(.Groups("value"), "value")
End If
End With
Debug.Unindent()
Next

I would consider encapsulating the above in a class where I populate a
HashTable with the name/value pairs.

Of course you could use standard String functions to parse each parameter
also:

For Each arg As String In Environment.GetCommandLineArgs()
Debug.WriteLine(Nothing, arg)
Debug.Indent()
If arg.StartsWith("/") Then
Dim index As Integer = arg.IndexOf(":"c)
Dim name As String
Dim value As String
If index <> -1 Then
name = arg.Substring(1, index - 1)
value = arg.Substring(index + 1)
Else
name = arg.Substring(1)
value = String.Empty
End If
Debug.WriteLine(name, "name")
Debug.WriteLine(value, "value")
End If
Debug.Unindent()
Next

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Hanika,
Here's an improved Pattern to replace the one from my earlier post:

Const pattern As String = "^/(?<name>\w+)(?:\:(?<value>.+)$|\:$|$)"

This pattern will match the following:

/ArgName:"Value of argument"
/ArgName
/ArgName:

Note the regular command line parser will interpret the quotes on the first
one, making it appear as:

/ArgName:Value of argument

In the collection returned from GetCommandLineArgs.

Hope this helps
Jay
 
G

Guest

This great thanks!
Hanika

Jay B. Harlow said:
Hanika,
Here's an improved Pattern to replace the one from my earlier post:

Const pattern As String = "^/(?<name>\w+)(?:\:(?<value>.+)$|\:$|$)"

This pattern will match the following:

/ArgName:"Value of argument"
/ArgName
/ArgName:

Note the regular command line parser will interpret the quotes on the first
one, making it appear as:

/ArgName:Value of argument

In the collection returned from GetCommandLineArgs.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top