我有一个C#命令行程序意外地以消息结束:The program '[2072] csgrep.exe' has exited with code 1 (0x1)
。
缩短的代码如下:
static void Main(string[] args)
{
try
{
var parser = new CommandLine.Parser(s =>
{
s.CaseSensitive = true;
});
// the application unexpectedly stops when calling this method.
var isValid = parser.ParseArgumentsStrict(args, options);
if (isValid)
{
... run my application here
}
else
{
Console.WriteLine("Input error.");
}
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
CommandLine.Parser
和ParseArgumentsStrict
来自nuget包“Command Line Parser Library1.9.74”(https://www.nuget.org/packages/CommandLineParser19/)
当我使用调试器“进入”时,应用程序将停在var isValid = parser.ParseArgumentsStrict(args, options);
我的问题是我真的不知道如何解决问题。我怎样才能找出问题所在?
答案 0 :(得分:0)
代码可以按照以下方式更改:不使用代码1意外退出:
static void OnCommandLineParseFail()
{
Console.WriteLine("Command line parse failure");
Console.WriteLine("Help: " + parser.Settings.HelpWriter.ToString());
}
static void Main(string[] args)
{
try
{
StringWriter t = new StringWriter();
var parser = new CommandLine.Parser(s =>
{
s.CaseSensitive = true;
s.HelpWriter = t;
s.IgnoreUnknownArguments = false;
});
var isValid = parser.ParseArgumentsStrict(args, options, OnCommandLineParseFail);
if (isValid)
{
... run my application here
}
else
{
Console.WriteLine("Input error.");
}
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}