By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,973 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,973 IT Pros & Developers. It's quick & easy.

Application Command Switches

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
John Wildes wrote:
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,

--

(O)enone
Nov 21 '05 #2

P: n/a
Oenone wrote:
John Wildes wrote:
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
Nov 21 '05 #3

P: n/a
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
Oenone wrote:
John Wildes wrote:
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,

Nov 21 '05 #4

P: n/a
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.

Nov 21 '05 #5

P: n/a
On Wed, 18 May 2005 14:28:43 -0500, John Wildes <jw*****@gmail.com>
wrote:
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

<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
Nov 21 '05 #6

P: n/a
Tibby wrote:
On Wed, 18 May 2005 14:28:43 -0500, John Wildes <jw*****@gmail.com>
wrote:

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


<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
Nov 21 '05 #7

P: n/a
John,

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

john w

jo*******@gmail.com wrote:
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.

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.