2

我使用Command Line Parser Library來解析命令行參數。如何使用Command Line Parser Library來防止錯誤發生?

我宣佈類Options

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 

回答

1

我使用命令行解析器。

聲明一個公用包裝紙多個項目

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); 
} 

該應用程序創建應用程序的命令行類

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) 
{ 
    ... 
} 
+0

類CommandLineOptions不是有效類。這是否意味着讀者會知道「明顯」的項目要取代/填充? –

+0

CommandLineOptions是我作爲CommandLineParser類的功能封裝創建的類。 –

+0

對不起,我的意思是說,當我將源代碼放入並將其放入Microsoft Visual Studio 2013時,它會識別源代碼不完整。我可以更具體。我也可能會誤解使用情況。 –