tags:

views:

205

answers:

9

For learning purposes i'm developing a Class generation application in c# and winforms. I think It could be fine to include a command-line mode that allow to use the application in scripts.

It's a good practice to include a command-line mode in my applications? It would be better to have two different programs, one with GUI in one for the command-line?

+2  A: 

Yes. If you think the program will be useful in a scripted environment then include a command line mode (without UI) so it can be used in scripts.

It doesn't have to be a separate application, but it can be. Whether you want to do that or not is entirely up to you. I'd imagine that if you had two applications they'd share the same logic assemblies but the interface (one a GUI the other a command line) would just be different.

Colin Mackay
+6  A: 

Actually having a C# application be both console and GUI is problematic. Console applications (/t:exe) are launched and then the command prompt waits for them to finish. GUI applications (/t:winexe) the command shell launches them and then returns immediately. While you can create and run forms from a 'console' application, it will always have a background console displayed. On the other hand 'Forms' application don't have the stdin, stdout and stderr connected and, while they can behave as command line tools and process command arguments, they have problems when embedded in scripts (because the standard input/output is not hooked up).

If you want to expose the functionality from both GUI driven applications and scriptable/pipe-able batch processing too the best way is to compile your functionality into a class library, then built two separate applications (one GUI one console) that leverage that library.

Remus Rusanu
You can also create a console output window in a winexe application using some win32-api stuff (AllocConsole/AttachConsole), however that still has some issues because it doesn't have the correct subsystem specified.
Jasper Bekkers
@Jasper: sure, I think you can even go after the FCBs at index 0,1 and 2, but I'd really stay away from that, just go with the flow rather than fight the current upstream ;)
Remus Rusanu
I have never had the need to write an app that ran both GUI and had a command line, I never realized it was so problematic to achieve. Very informative response.
Chris Marisic
+1  A: 

Creating an application on the windows platform that behaves correctly as a console application can be problematic it's an issue with the windows kernel architecture as they're considered two different types of application (they have a different subsystem that you generally specify in the compiler or linker options). You can still manually redirect the IO and open a console from a win32 application by the win32 function AllocConsole() and friends but this also has some issues. See This Old New Thing post for more information.

Jasper Bekkers
I should had though Raymond has post about this already lol
Remus Rusanu
+1  A: 

If you want your utility/prgram run in scripts you can expose it as COM.

Many script languages for windows had the hability to use COM objects directly.

Aragorn
+3  A: 

I'm not a C# programmer, but when I program in C++, I find it most useful to:

1.) Create both a shared library with a C as well as C++ API for performing core app functionality.

2.) Create one or more commandline binaries accessible to the shell interpreter.

3.) Create a GUI application for typical end users, implemented with the library (not by invoking the binaries).

This separates the logic of the application from the interface to the application, and enables thirdparty developers to create alternative interfaces for the same application functionality. It also makes it easy to script, while at the same time catering to typical end users who want a nice, shiny GUI.

Michael Aaron Safyan
+1  A: 

I agree with michaelsafyan about creating a library with core functionality.

What I would add is that you should check out powershell cmdlets as well.

Much command line activity will be migrating to powershell and it brings a lot to the table.

http://en.wikipedia.org/wiki/Windows_PowerShell

jeffa00
+2  A: 

I very often create such a utility as an API. If I need to use it from a simple command-line utility, that's easy - it just calls the API. If the command-line gets too complex, maybe it's time for a Winforms application - which can also call the API. If I wanted to use it from PowerShell, or from an MSBUILD task, those are still easy - they just call the API.

John Saunders
A: 

I agree with @Remus Rusanu.

you should create a class library of your core functionality and then build GUI app(wrapper) for that.

and one other benefit of it is you might not even need to create a command line app as you can access your .net dll features using powershell..

you can find one example over here

N30
A: 

Another great idea is to embed a scripting language. Then your program can be controlled by a script, and you get all the logic, branching, etc from the scripting language "for free."

There are many choices of what you can embed. Lua is one of the most popular and intended for just that purpose and is an excellent choice.

However, for a general purpose app, I'd take a hard look at embedding Python. Python is so popular, you'd have a larger group of people willing to take the effort to write a script for your app.

Wade Williams