显示命令行实用程序的帮助
我有一个命令行实用程序,可以从我的网站获得大量下载。 我试图显示用户使用 / 时的用法? 或 /help 参数。 我有一个名为 ShowUsage() 的函数,它在可用参数上有格式良好的文本。
当我在项目属性中使用命令行参数时,我看到 ShowUsage() 从 Visual Studio 2008 中正常调用。 但是,从命令行运行时,exe 不显示帮助文本。 这是 ShowUsage() 的缩短版本:
private static void ShowUsage()
{
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Text File Splitter v1.4.1 released: December 14, 2008");
Console.WriteLine("Copyright (C) 2007-2008 Hector Sosa, Jr");
Console.WriteLine("http://www.systemwidgets.com");
Console.WriteLine("");
}
我在 Google 搜索中尝试了很多不同的方法,但没有一个有效。 我知道这一定是一件简单/容易的事情,但就我的一生而言,我无法弄清楚这一点。
编辑:
调用 ShowUsage() 的代码:
if (!Equals(cmdargs["help"], null) || !Equals(cmdargs["?"], null))
{
ShowUsage();
}
我有一个类将参数解析到 cmdargs 数组中。 我确认参数在数组中。 这是 ShowUsage() 内部的某些内容阻止显示文本。
我将尝试调试技巧,看看我发现了什么。
我没有在任何地方使用 Console.Out。
d03boy - 只是个人喜好。 它使屏幕上的文字不再那么混乱,至少对我来说是这样。
编辑 #2
好的,更多信息...我在 Vista Ultimate 64 位中运行 VS2008。 我只是检查了项目属性,它被设置为“Windows 应用程序”。 该实用程序是 32 位的。
我将尝试在解决方案中创建一个单独的项目,这是一个真正的控制台程序,正如你们中的一些人建议的那样。
I have a command-line utility that gets quite a bit of downloads from my site. I'm trying to show the usage when a user uses the /? or /help parameters. I have a function called ShowUsage() that has nicely formatted text on the parameters available.
I see that ShowUsage() gets called fine from Visual Studio 2008, when I'm using the command-line parameters in the project properties. However the exe does not display the help text when run from the command-line. Here's a shortened version of ShowUsage():
private static void ShowUsage()
{
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Text File Splitter v1.4.1 released: December 14, 2008");
Console.WriteLine("Copyright (C) 2007-2008 Hector Sosa, Jr");
Console.WriteLine("http://www.systemwidgets.com");
Console.WriteLine("");
}
I tried a bunch of different things from my searches in Google, but none are working. I know this has to be something simple/easy, but for the life of me, I can't figure this one out.
EDIT:
The code that calls ShowUsage():
if (!Equals(cmdargs["help"], null) || !Equals(cmdargs["?"], null))
{
ShowUsage();
}
I have a class that parses the parameters into the cmdargs array. I confirmed that the parameters are in the array. It's something inside ShowUsage() that is preventing from showing the text.
I'm going to try the debug trick, and see what I find.
I'm not using Console.Out anywhere.
d03boy - Just personal preference. It makes the text less cluttered onscreen, at least to me.
EDIT #2
Ok, some more information... I'm running VS2008 in Vista Ultimate 64 bit. I just check the project properties and it is set to "Windows Application." This utility is 32 bit.
I'm going to experiment with creating a separate project in the solution that is a true console program, as some of you have advised.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能定义“不工作”吗? 就是单纯的什么都不做吗? 抛出异常? 我希望问题出在
Main(...)
方法中 - 即ShowUsage()
没有被调用。 另一个常见问题是没有以正确的配置重建它,因此 bin/release (或其他)不会更新。您是否已将应用程序构建为控制台可执行文件?即项目属性中的“输出类型”=“控制台应用程序”吗? 它需要是为了访问控制台...
有关信息,我发现执行控制台帮助屏幕的最简单方法(一旦超出几行)是将 .txt 文件嵌入到 exe 中; 然后我只写出文本文件(如果我想替换tokes,也许仍然使用
string.Format
)。或者,还有另一种字符串格式:
Can you define "not working"? Simply not doing anything? Throwing an exception? I would expect the problem to be in the
Main(...)
method - i.e.ShowUsage()
isn't being called. Another common issue is not rebuilding it in the correct configuration, sobin/release
(or whatever) isn't updated.Have you built the app as a console exe? i.e. is the "Output Type" = "Console Application" in project properties? It needs to be in order to access the console...
For info, I find the easiest way to do a console help screen (once it gets beyond a handful of lines) is to embed a .txt file into the exe; then I just write out the text file (perhaps still using
string.Format
if I want to replace tokes).Alternatively, there is the alternative string format:
您是否将 Console.Out 重定向到其他地方?
尝试在 ShowUsage 方法中抛出 System.Diagnostics.Debugger.Launch() ,以便您可以查看它是否在运行时被命中。
您可以使用一个简单的 exe 重现该问题,只接受这些帮助参数吗?
Are you redirecting Console.Out to somewhere else?
Try throwing a System.Diagnostics.Debugger.Launch() in the ShowUsage method so you can see if it's getting hit at runtime.
Can you reproduce the issue with a simple exe, only accepting those help parameters?