.NET命令行解析器示例程序(命令行選項(xiàng)功能)
示例需求
拷貝文件,如:CopyFiles -s "E:\Framework\Tenoner - 副本 (2)" -p "*.csproj" -t "E:\Framework\Tenoner - 副本 (2)\Bak",可以支持:深度拷貝、拷貝符合指定模式的文件、是否覆蓋等選項(xiàng)。
使用 CommandLineParser
CommandLineParser 是一個(gè)輕量級(jí)的工具,使用非常簡(jiǎn)答,官方也有教程。
選項(xiàng)類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using CommandLine.Text;
namespace CopyFiles
{
class Options
{
[Option(
's', "source", Required = true,
HelpText = "源目錄。")]
public string SourcePath { get; set; }
[Option(
'p', "pattern", Required = true,
HelpText = "文件模式。")]
public string SearchPattern { get; set; }
[Option(
't', "target", Required = true,
HelpText = "目標(biāo)目錄。")]
public string TargetPath { get; set; }
[Option('a', "all", DefaultValue = true,
HelpText = "是否包含所有目錄?")]
public bool AllDirectories { get; set; }
[Option('o', "overwrite", DefaultValue = true,
HelpText = "是否覆蓋所有文件?")]
public bool Overwrite { get; set; }
[Option('v', "verbose", DefaultValue = true,
HelpText = "是否打印消息?")]
public bool Verbose { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this);
}
public void WriteLine(string format, params object[] args)
{
if (this.Verbose)
{
Console.WriteLine(string.Format(format, args));
}
}
}
}
工具類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using Happy.Utils;
namespace CopyFiles
{
class Program
{
static void Main(string[] args)
{
var options = new Options();
if (Parser.Default.ParseArguments(args, options))
{
FileUtil.Copy(
options.SourcePath,
options.SearchPattern,
options.TargetPath,
(sourceFile, targetFile) =>
{
options.WriteLine("拷貝文件:{0} 到 {1}", sourceFile, targetFile);
},
(exceptionInfo) =>
{
options.WriteLine(exceptionInfo.Exception.Message);
exceptionInfo.ExceptionHandled = true;
},
options.AllDirectories,
options.Overwrite);
}
}
}
}

相關(guān)文章
ASP.NET堆和棧三之引用類型對(duì)象拷貝和內(nèi)存分配
這篇文章介紹了ASP.NET堆和棧中引用類型對(duì)象的拷貝和內(nèi)存分配,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個(gè)網(wǎng)站的注冊(cè)的時(shí)候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個(gè)校驗(yàn)的代碼。呵呵 不知道對(duì)大家有沒有用。如果有用的話可以保存。沒有用就當(dāng)是看看以下了。2010-10-10MVC使用Log4Net進(jìn)行錯(cuò)誤日志記錄學(xué)習(xí)筆記4
這篇文章主要為大家詳細(xì)介紹了MVC使用Log4Net進(jìn)行錯(cuò)誤日志記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09使用Fiddler調(diào)試visual studion多個(gè)虛擬站點(diǎn)的問題分析
本篇文章小編為大家介紹,使用Fiddler調(diào)試visual studion多個(gè)虛擬站點(diǎn)的問題分析。需要的朋友參考下2013-04-04安裝.NET Framework進(jìn)度條卡住不動(dòng)的解決方案(推薦)
VS在安裝之前需要安裝.NET Framework,我安裝的是4.0版本。但是安裝進(jìn)度條到一半左右時(shí)就卡住不動(dòng)了。前前后后重試多次,還有幾次重新開機(jī),但都沒用,怎么解決呢,下面給大家分享下解決方案2016-12-12如何在.NET Core中為gRPC服務(wù)設(shè)計(jì)消息文件(Proto)
這篇文章主要介紹了如何在.NET Core中為gRPC服務(wù)設(shè)計(jì)消息文件(Proto),幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-05-05ASP.NET MVC @Helper輔助方法和@functons自定義函數(shù)的使用方法
本文主要介紹ASP.NET MVC中使用@Helper和@functons自定義一些代碼片段,方便視圖調(diào)用,從而達(dá)到減少重復(fù)代碼,快速開發(fā)的目的,希望對(duì)大家有所幫助。2016-04-04ASP.NET數(shù)據(jù)綁定之Repeater控件
這篇文章主要介紹了ASP.NET數(shù)據(jù)綁定中的Repeater控件,Repeater控件可以將數(shù)據(jù)庫中的信息加以綁定然后再在瀏覽器中顯示出來,感興趣的小伙伴們可以參考一下2016-01-01