解析命令行

目录

背景示例备注

背景返回目录

经常需要开发一下小工具,之前都是自己解析命令行参数,接触过动态语言社区以后,发现命令行解析有特定的模式和框架可以利用,本文介绍一个 .NET 平台的类库。

示例返回目录

需求

拷贝文件,如:CopyFiles -s "E:FrameworkTenoner - 副本 (2)" -p "*.csproj" -t "E:FrameworkTenoner - 副本 (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,没法同时在三种语言中快速的切换。

原文地址:https://www.cnblogs.com/Leo_wl/p/3385097.html