views:

221

answers:

5

I'm trying to build a command-line tool in C# with VS2010.

My question is: how do I debug this, like I would a winforms.
With winforms, I can step through the code, see the values at each individual step, etc...

Here however, the program only responds when I talk to it from the command line. I can't start a debug session, since that will first fully start the program. And due to the nature of the program, it will shut itself because there were no command-line arguments.

So no debugging can occur.

What do I need to do here?

EDIT: an example, since someone made a comment that makes me feel this explanation is needed.

C:\Path\To\File\file.exe -help

That is an example of how this program is adressed. The command, -help, is given in the same line that the program is started. In other words, I cannot first start the program, and then give it a command while it's running... because it won't be running anymore. It'll start, see that it had no arguments on startup, and because of that, shut down. That's how a command-line tool works.

  1. Activate
  2. Process possible arguments
  3. Output results
  4. Shut down automatically

It is not something that keeps running till you click the little x in the top right corner.

+1  A: 

You can use Visual Studio to attach a debugger to the command line application, once it is under way with the correct arguments. I'm not sure if your application will terminate quickly or give you any opportunity to attach the debugger, but if it will, this should work.

I'm using VS2008 but I'll bet the process is similar in 2010:

  1. In VS, go to Tools and click Attach to Process
  2. Choose your application from the list and press Attach

Now VS should be able to finagle its way into your application and break on an error.

SimpleCoder
Not likely in this scenario. The command line app would start up and shut down so quickly that he wouldn't have an opportunity to attach to it in this way. The suggestions above are far superior.
Mike Hofer
True. I was just being hopeful that maybe there would be a blocking input which would give them an opportunity to attach the debugger. But, being that it accepts command line arguments, I suppose that is unlikely.
SimpleCoder
If he has the source code, he can easily insert a `Console.Readline` to pause the program long enough for him to attach the debugger. Then press a key, and it's off to the races. This doesn't deserve a downvote in my opinion, it will work perfectly well with a trivial one-line change.
Joel Mueller
+16  A: 

In the Project properties, under Debug, you can enter any Command Line Arguments you would like, and then run the app with F5, the debugger will be attached automatically.

davisoa
Sadly, everything is grayed out.
WebDevHobo
@WebDevHobo: really? that seems very odd. For me command arguments are one of only two things *not* greyed out.
Matt Ellen
They'll be grayed out if you're *already* debugging. If you drop down the Debug menu, is "Stop Debugging" enabled? If so, select it, and then you'll be able to set the command-line arguments normally.
Joe White
Yes, that was it.
WebDevHobo
+1  A: 

You just need to add a breakpoint to the first line of the main function (you can do this by clicking on the line in the Visual Studio editor and hitting F9) and hit F5 to start a debug session.

Adrian Grigore
Or you could just step into the program, no need to set a breakpoint explicitly.
hydrogen
That's what I originally did. I repeat: this is a program that expects arguments from the command line. I can't give it input multiple times. It shuts down after 1 run. The giving of a command and the starting of the program is simultaneous, which is inherent to the nature of command-line programs. I'll put an example in the main post
WebDevHobo
A: 

Either add a breakpoint to the opening { of Main, or step into the program (Debug menu). At that point set a watch on the parameter for main (the command line arguments) by selecting it and either right-click/Add Watch or drag the parameter to to watch window if it is already open. Double-click the Value column in the Watch window and set it to whatever you want it to be.

Note: the value added must be valid code -- that is, to add a -help to the string[] you would have to type new string[] {"-help"} or new [] {"-help"} depending on the version you are using.

This has the advantage over setting the parameter in the Debug tab of the Properties window by allowing different parameters for each run without having to return to the Properties window.

David Culp
+3  A: 

You could add a call to Debugger.Launch to your startup code. Then you can compile, and start your app from the command line. You'll get a prompt asking you which debugger you want to attach (typically this will be a list of the different versions of Visual Studio you have installed), and away you go.

(But really, setting command-line parameters in Project properties > Debug tab is the better way to go most of the time. If that's not working for you, you should figure out why.)

Joe White
System.Diagnostics.Debugger.Break() also has similar effect.
stephbu