控制台程序的參數解析類庫 CommandLine


C#控制台程序的參數解析類庫 CommandLine簡單使用說明

前言

C#開發的控制台程序,默認接收string[] args參數。如果有多個參數需要輸入時,可以按照順序依次輸入;但如果有些參數不是必選的,或者有些參數中間需要有空格比如時間“2016-05-18 24:35:00”,處理起來就比較麻煩了。一些常用的命令行工具都會提供指定參數的方式,比如:curl

C:\Users\Administrator>curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
--anyauth Pick "any" authentication method (H)
-a/--append Append to target file when uploading (F)
--basic Use HTTP Basic Authentication (H)
--cacert <file> CA certificate to verify peer against (SSL)
--capath <directory> CA directory to verify peer against (SSL)
-E/--cert <cert[:passwd]> Client certificate file and password (SSL)

這里要介紹的 CommandLine 就是幫助我們輕易完成參數接收和幫助輸出的開源類庫,同時它可以把接收到的參數轉換成對象,方便程序的處理。

教程

  1. 新建控制台項目,安裝CommandLine。

    可以下載、編譯、引用CommandLine.dll,也可以使用nuget安裝 Install-Package CommandLineParser

  2. 新建參數說明類 Options 

首先,添加命名空間  

using CommandLine;
using CommandLine.Text;

然后,定義Options 類

復制代碼
 1   class Options
 2     {
 3         [Option('r', "read", MetaValue = "FILE", Required = true, HelpText = "輸入數據文件")]
 4         public string InputFile { get; set; }
 5 
 6         [Option('w', "write", MetaValue = "FILE", Required = false, HelpText = "輸出數據文件")]
 7         public string OutputFile { get; set; }
 8 
 9 
10         [Option('s', "start-time", MetaValue = "STARTTIME", Required = true, HelpText = "開始時間")]
11         public DateTime StartTime { get; set; }
12 
13         [Option('e', "end-time", MetaValue = "ENDTIME", Required = true, HelpText = "結束時間")]
14         public DateTime EndTime { get; set; }
15 
16 
17         [HelpOption]
18         public string GetUsage()
19         {
20             return HelpText.AutoBuild(this, current => HelpText.DefaultParsingErrorsHandler(this, current));
21         }
22 
23     }
復制代碼

  3. 修改控制台主程序 Program的Main函數  

復制代碼
 1   //輸出信息時的頭信息
 2         private static readonly HeadingInfo HeadingInfo = new HeadingInfo("演示程序", "V1.8");
 3 
 4         static void Main(string[] args)
 5         {
 6             //這種輸出會在前面添加"演示程序"幾個字
 7             HeadingInfo.WriteError("包含頭信息的錯誤數據");
 8             HeadingInfo.WriteMessage("包含頭信息的消息數據");
 9  
10             Console.WriteLine("不包含頭信息的錯誤數據");
11             Console.WriteLine("不包含頭信息的消息數據");
12 
13             var options = new Options();
14             if (CommandLine.Parser.Default.ParseArguments(args, options))
15             {
16                 Console.WriteLine("Input File:" + options.InputFile);
17                 Console.WriteLine("Output File:" + options.OutputFile);
18 
19                 Console.WriteLine("開始時間:" + options.StartTime.ToString("yyyy年MM月dd日 HH點mm分"));
20                 Console.WriteLine("結束時間:" + options.EndTime.ToString("yyyy年MM月dd日 HH點mm分"));
21                 Console.Read();
22             }
23             //else
24             //{
25             //    Console.WriteLine(options.GetUsage());
26             //    Console.Read();
27             //}
28 
29             Console.Read();
30         }
復制代碼

3. 測試控制台程序

不輸入任何參數,輸出了參數的說明信息,如下圖:

輸入參數,如下圖:

時間和字符串類型的字段都獲取到了值。 

 

 
分類:  C#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM