如果发生错误,如何使用命令行解析器库?
How to use Command Line Parser Library in case Error happend?
我使用Command Line Parser Library解析命令行参数。
我已经声明了classOptions
internal class Options
{
[Option('r', "read", Required = true,
HelpText = "Input file to be processed.")]
public string InputFile { get; set; }
[Option('f', "date from", Required = false,
HelpText = "Date from which get statistic.")]
public string DateFrom { get; set; }
[Option('t', "date to", Required = false,
HelpText = "Date to which get statistic.")]
public string DateTo { get; set; }
[Option('v', "verbose", DefaultValue = true,
HelpText = "Prints all messages to standard output.")]
public bool Verbose { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
这就是我尝试使用解析器的方式:
private static void Main(string[] args)
{
Console.WriteLine("Hello and welcome to a test application!");
string filePath = string.Empty;
string fromDate = string.Empty, toDate = string.Empty;
DateTime dateTo, dateFrom;
Console.ReadLine();
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
// Values are available here
if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
if (string.IsNullOrWhiteSpace(options.InputFile))
{
filePath = ConfigurationManager.AppSettings["FilePath"];
if (string.IsNullOrWhiteSpace(filePath))
{
filePath = Directory.GetCurrentDirectory() + @"\Report.xlsx";
}
}
else
{
filePath = options.InputFile;
}
fromDate = string.IsNullOrWhiteSpace(options.DateFrom)
? ConfigurationManager.AppSettings["DateFrom"]
: options.DateFrom;
toDate = string.IsNullOrWhiteSpace(options.DateTo) ? ConfigurationManager.AppSettings["DateTo"] : options.DateTo;
}
else
{
return;
}
//其他代码
}
但如果出现某些错误,应用程序就会停止工作。
所以我想知道如何在出错的情况下重复输入值的第一步。
while (!CommandLine.Parser.Default.ParseArguments(args, options)){...} - makes loop
我使用命令行解析器。
为多个项目声明一个公共包装器
public class CommandLineOptions
{
public const bool CASE_INSENSITIVE = false;
public const bool CASE_SENSITIVE = true;
public const bool MUTUALLY_EXCLUSIVE = true;
public const bool MUTUALLY_NONEXCLUSIVE = false;
public const bool UNKNOWN_OPTIONS_ERROR = false;
public const bool UNKNOWN_OPTIONS_IGNORE = true;
public CommandLineOptions();
public string[] AboutText { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption(HelpText = "Display this Help Screen")]
public virtual string GetUsage();
public bool ParseCommandLine(string[] Args);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive, bool UseCaseSensitive);
}
为此应用程序创建应用程序命令行class
public class ApplicationCommandLine : CommandLineOptions
{
[Option('d', "download", HelpText = "Download Items before running")]
virtual public bool Download { get; set; }
[Option('g', "generate", HelpText = "Generate Mode (Generate New Test Results)", MutuallyExclusiveSet = "Run-Mode")]
virtual public bool GenerateMode { get; set; }
[Option('r', "replay", HelpText = "Replay Mode (Run Test)", MutuallyExclusiveSet = "Run-Mode")]
virtual public bool ReplayMode { get; set; }
}
主程序:
ApplicationCommandLine AppCommandLine = new ApplicationCommandLine();
try
{
// Parsers and sets the variables in AppCommandLine
if (AppCommandLine.ParseCommandLine(args))
{
// Use the Download option from the command line.
if (AppCommandLine.Download) {
DataFileDownload();
}
if (AppCommandLine.GenerateMode) {
GenerateProcessingData();
}
...
}
}
catch (Exception e)
{
...
}
我使用Command Line Parser Library解析命令行参数。
我已经声明了classOptions
internal class Options
{
[Option('r', "read", Required = true,
HelpText = "Input file to be processed.")]
public string InputFile { get; set; }
[Option('f', "date from", Required = false,
HelpText = "Date from which get statistic.")]
public string DateFrom { get; set; }
[Option('t', "date to", Required = false,
HelpText = "Date to which get statistic.")]
public string DateTo { get; set; }
[Option('v', "verbose", DefaultValue = true,
HelpText = "Prints all messages to standard output.")]
public bool Verbose { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
这就是我尝试使用解析器的方式:
private static void Main(string[] args)
{
Console.WriteLine("Hello and welcome to a test application!");
string filePath = string.Empty;
string fromDate = string.Empty, toDate = string.Empty;
DateTime dateTo, dateFrom;
Console.ReadLine();
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
// Values are available here
if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
if (string.IsNullOrWhiteSpace(options.InputFile))
{
filePath = ConfigurationManager.AppSettings["FilePath"];
if (string.IsNullOrWhiteSpace(filePath))
{
filePath = Directory.GetCurrentDirectory() + @"\Report.xlsx";
}
}
else
{
filePath = options.InputFile;
}
fromDate = string.IsNullOrWhiteSpace(options.DateFrom)
? ConfigurationManager.AppSettings["DateFrom"]
: options.DateFrom;
toDate = string.IsNullOrWhiteSpace(options.DateTo) ? ConfigurationManager.AppSettings["DateTo"] : options.DateTo;
}
else
{
return;
}
//其他代码 }
但如果出现某些错误,应用程序就会停止工作。
所以我想知道如何在出错的情况下重复输入值的第一步。
while (!CommandLine.Parser.Default.ParseArguments(args, options)){...} - makes loop
我使用命令行解析器。
为多个项目声明一个公共包装器
public class CommandLineOptions
{
public const bool CASE_INSENSITIVE = false;
public const bool CASE_SENSITIVE = true;
public const bool MUTUALLY_EXCLUSIVE = true;
public const bool MUTUALLY_NONEXCLUSIVE = false;
public const bool UNKNOWN_OPTIONS_ERROR = false;
public const bool UNKNOWN_OPTIONS_IGNORE = true;
public CommandLineOptions();
public string[] AboutText { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption(HelpText = "Display this Help Screen")]
public virtual string GetUsage();
public bool ParseCommandLine(string[] Args);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive);
public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive, bool UseCaseSensitive);
}
为此应用程序创建应用程序命令行class
public class ApplicationCommandLine : CommandLineOptions
{
[Option('d', "download", HelpText = "Download Items before running")]
virtual public bool Download { get; set; }
[Option('g', "generate", HelpText = "Generate Mode (Generate New Test Results)", MutuallyExclusiveSet = "Run-Mode")]
virtual public bool GenerateMode { get; set; }
[Option('r', "replay", HelpText = "Replay Mode (Run Test)", MutuallyExclusiveSet = "Run-Mode")]
virtual public bool ReplayMode { get; set; }
}
主程序:
ApplicationCommandLine AppCommandLine = new ApplicationCommandLine();
try
{
// Parsers and sets the variables in AppCommandLine
if (AppCommandLine.ParseCommandLine(args))
{
// Use the Download option from the command line.
if (AppCommandLine.Download) {
DataFileDownload();
}
if (AppCommandLine.GenerateMode) {
GenerateProcessingData();
}
...
}
}
catch (Exception e)
{
...
}