背景
經常需要開發一下小工具,之前都是自己解析命令行參數,接觸過動態語言社區以后,發現命令行解析有特定的模式和框架可以利用,本文介紹一個 .NET 平台的類庫。
示例
需求
拷貝文件,如:CopyFiles -s "E:\Framework\Tenoner - 副本 (2)" -p "*.csproj" -t "E:\Framework\Tenoner - 副本 (2)\Bak",可以支持:深度拷貝、拷貝符合指定模式的文件、是否覆蓋等選秀。
使用 CommandLineParser
CommandLineParser 是一個輕量級的工具,使用非常簡答,官方也有教程。
選項類
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using CommandLine; 8 using CommandLine.Text; 9 10 namespace CopyFiles 11 { 12 class Options 13 { 14 [Option( 15 's', "source", Required = true, 16 HelpText = "源目錄。")] 17 public string SourcePath { get; set; } 18 19 [Option( 20 'p', "pattern", Required = true, 21 HelpText = "文件模式。")] 22 public string SearchPattern { get; set; } 23 24 [Option( 25 't', "target", Required = true, 26 HelpText = "目標目錄。")] 27 public string TargetPath { get; set; } 28 29 [Option('a', "all", DefaultValue = true, 30 HelpText = "是否包含所有目錄?")] 31 public bool AllDirectories { get; set; } 32 33 [Option('o', "overwrite", DefaultValue = true, 34 HelpText = "是否覆蓋所有文件?")] 35 public bool Overwrite { get; set; } 36 37 [Option('v', "verbose", DefaultValue = true, 38 HelpText = "是否打印消息?")] 39 public bool Verbose { get; set; } 40 41 [HelpOption] 42 public string GetUsage() 43 { 44 return HelpText.AutoBuild(this); 45 } 46 47 public void WriteLine(string format, params object[] args) 48 { 49 if (this.Verbose) 50 { 51 Console.WriteLine(string.Format(format, args)); 52 } 53 } 54 } 55 }
工具類
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using CommandLine; 8 using Happy.Utils; 9 10 namespace CopyFiles 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 var options = new Options(); 17 if (Parser.Default.ParseArguments(args, options)) 18 { 19 FileUtil.Copy( 20 options.SourcePath, 21 options.SearchPattern, 22 options.TargetPath, 23 (sourceFile, targetFile) => 24 { 25 options.WriteLine("拷貝文件:{0} 到 {1}", sourceFile, targetFile); 26 }, 27 (exceptionInfo) => 28 { 29 options.WriteLine(exceptionInfo.Exception.Message); 30 31 exceptionInfo.ExceptionHandled = true; 32 }, 33 options.AllDirectories, 34 options.Overwrite); 35 } 36 } 37 } 38 }
運行效果
備注
用動態語言寫過幾個簡單的工具,沒有堅持下來,主要原因是對 API 的不熟悉,加上目前晚上要學習 Java,沒法同時在三種語言中快速的切換。