开发者

What steps shall I take before building a command line application using C#

I want to develop a command line tool. I didn't build one before, but I used a few, (like mercurial). What steps do I 开发者_StackOverflow中文版need to take to know how to do that? So what is the problem:

  1. Regular console application need to be invoked from the command line only from it's directory. Like: C:\Projects\CommanLineProject\MyProjectConsole.exe. I want to use it from the command line from any directory, like mercurial.

  2. I don't sure how to parse and how to use the command-line arguments. There are programs that taking this arguments: c:\>MyProject "c:\point to this path" /xml:"here is the another path" /r.


A command line tool is just a regular console application. You can use the string[] args parameter from the Main method to retrieve any arguments that are passed to the application.


I would go look at this stack overflow question as it has links to some good command line parsing libraries in the answers.


One thing you'll need to do, if the application is to take any but the most basic arguments, is select a cmd line parsing library, as there is not one provided by the Framework.


I would look into the following link before making a significant command line application.

http://commandline.codeplex.com/


For question/edit #1:

If you add the path to the executable to your operating system's 'Path' environment variable, you can run the application from any directory. More info on setting it for Windows XP.

Also, you may need to retrieve the current working directory when the application is started. I believe you can use Environment.CurrentDirectory to get this. I'd suggest saving it in case you need to do something that might change the current directory for your process.

For question/edit #2:

You can use something like this to parse the actual arguments that are passed into the application via the args string array. However, what you do with the arguments after that point is completely up to you. There's nothing stopping you from just going through the array and dealing with the arguments manually (foreach (string arg in args) {...}) if you prefer.

If you want to make the first command line argument the path to a file to write to, so be it. If you want to have an optional argument like /verbose then that works as well. You are really only limited to your imagination (and the limits of what can be legally typed on the command line :) )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜