Application Command Switches

J

John Wildes

Hello All,

I've got a VB.net windows form app that I have created to solve some
recurring maintenance tasks. What I would like to do is build
functionality into this application so that accepts command switches
like. C:\FooApp\Foobar.exe /quiet that the application will load and
process tasks with no user input, but when C:\FooApp\Foobar.exe is run
that it would load my windows form interface. Does that make sense?
Can anyone help me out with that, maybe point me in the right direction?

Thanks
john
 
O

Oenone

John said:
I've got a VB.net windows form app that I have created to solve some
recurring maintenance tasks. What I would like to do is build
functionality into this application so that accepts command switches

There are two ways to obtain the command switches that your program started
with.

The first is to modify your Sub Main() so that it looks like this:

\\\
Public Sub Main(ByVal CmdArgs() As String)
[...]
End Sub
///

When you run this (it works both for Windows and Console applications),
you'll find the CmdArgs() array is populated with all the values from your
command line, with each word (separated by spaces) in a different array
element. So if you run as follows:

MyApp.exe Hello World

....You'll find the CmdArgs() array has two elements, the first containing
the string "Hello" and the second containing "World".

The second way is to use the Microsoft.VisualBasic.Command function. This
will return you the entire command line as a single string. In the example
above, this would contain the string "Hello World".

Once you've checked to see whether or not your command line switch is
present, just change the flow of your application logic to respond to it as
appropriate.

Hope that helps,
 
J

John Wildes

Oenone said:
John said:
I've got a VB.net windows form app that I have created to solve some
recurring maintenance tasks. What I would like to do is build
functionality into this application so that accepts command switches


There are two ways to obtain the command switches that your program started
with.

The first is to modify your Sub Main() so that it looks like this:

\\\
Public Sub Main(ByVal CmdArgs() As String)
[...]
End Sub
///

When you run this (it works both for Windows and Console applications),
you'll find the CmdArgs() array is populated with all the values from your
command line, with each word (separated by spaces) in a different array
element. So if you run as follows:

MyApp.exe Hello World

...You'll find the CmdArgs() array has two elements, the first containing
the string "Hello" and the second containing "World".

The second way is to use the Microsoft.VisualBasic.Command function. This
will return you the entire command line as a single string. In the example
above, this would contain the string "Hello World".

Once you've checked to see whether or not your command line switch is
present, just change the flow of your application logic to respond to it as
appropriate.

Hope that helps,
Thanks (O)enone for the help I will try that.

john
 
J

John Wildes

Oenone,

I have looked into the CmdArgs,and it works great, but I am new to VB
and am running into problems. Here is my code below

Module Code
Public Sub Main(ByVal CmdArgs() As String)
If CmdArgs(0) = "-q" Then
MsgBox("Running in Quiet Mode", MsgBoxStyle.Critical,
"Command ArgsTest")
ElseIf CmdArgs(0) Then
MsgBox("Running in Active Mode", MsgBoxStyle.Information,
"Command ArgsTest")
End If
End Sub
End Module

My question now is how, do I account for no argument on the command
line. When I run the output C:\FooApp\FOOBAR.exe -q it works , but if I
just run C:\FooApp\FOOBAR.exe it fails and goes to debugger ?? Any help?

john

John said:
I've got a VB.net windows form app that I have created to solve some
recurring maintenance tasks. What I would like to do is build
functionality into this application so that accepts command switches


There are two ways to obtain the command switches that your program started
with.

The first is to modify your Sub Main() so that it looks like this:

\\\
Public Sub Main(ByVal CmdArgs() As String)
[...]
End Sub
///

When you run this (it works both for Windows and Console applications),
you'll find the CmdArgs() array is populated with all the values from your
command line, with each word (separated by spaces) in a different array
element. So if you run as follows:

MyApp.exe Hello World

...You'll find the CmdArgs() array has two elements, the first containing
the string "Hello" and the second containing "World".

The second way is to use the Microsoft.VisualBasic.Command function. This
will return you the entire command line as a single string. In the example
above, this would contain the string "Hello World".

Once you've checked to see whether or not your command line switch is
present, just change the flow of your application logic to respond to it as
appropriate.

Hope that helps,
 
J

johntarey

The reason it is throwing an exception is because when no command
arguments are given, CmdArgs is Nothing, and CmdArgs(0) would not be
accessable.

The correct way of checking it would be
If (CmdArgs is nothing) OrElse (CmdArgs.Length = 0) Then
MsgBox("Running in Active Mode", MsgBoxStyle.Information, "Command
ArgsTest")
ElseIf CmdArgs(0) = "-q" Then
MsgBox("Running in Quiet Mode", MsgBoxStyle.Critical, "Command
ArgsTest")
EndIf

The OrElse is vital, as it will prevent CmdArgs.Length from erroring
out if CmdArgs is nothing.

It is also important that you check to see if CmdArgs has anything in
it *before* you check to see what the first (or any other) element may
be.

Also it may be a good idea to get rid of any potential white space or
case problems by using CmdArgs(0).Trim.ToLower = "-q" just to prevent
any surprises from popping up.
 
T

Tibby

Oenone,

I have looked into the CmdArgs,and it works great, but I am new to VB
and am running into problems. Here is my code below

Module Code
Public Sub Main(ByVal CmdArgs() As String)
If CmdArgs(0) = "-q" Then
MsgBox("Running in Quiet Mode", MsgBoxStyle.Critical,
"Command ArgsTest")
ElseIf CmdArgs(0) Then
MsgBox("Running in Active Mode", MsgBoxStyle.Information,
"Command ArgsTest")
End If
End Sub
End Module

I would suggest something like this (Aircode, so it may not be 100%
bug free)

For Each s As String In CmdArgs
Select Case s
Case Is = "-q"
MessageBox.Show("Running in Quiet Mode",
"STATUS", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
Next

If there is no command line arguments, it just simply keeps going
wihtout needing to bother with the For..Next loop. Also, notice the
difference in the MessageBox object, I've noticed this one is a little
nicer.

HTH,
Tibby
 
J

John Wildes

Tibby said:
<SNIP>


I would suggest something like this (Aircode, so it may not be 100%
bug free)

For Each s As String In CmdArgs
Select Case s
Case Is = "-q"
MessageBox.Show("Running in Quiet Mode",
"STATUS", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
Next

If there is no command line arguments, it just simply keeps going
wihtout needing to bother with the For..Next loop. Also, notice the
difference in the MessageBox object, I've noticed this one is a little
nicer.

HTH,
Tibby
Tibby

Thanks for the help, I was only typing message box to create a
difference in boxes, I would never use a Critical box if it weren't a
critical error :)

john
 
J

John Wildes

John,

Thank you for your help, and explanation. Very clear and concise. This
really helps me a great deal.

john w
 

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