Sample Requirements
copy files , such as: Copyfiles-s "E:\Framework\Tenoner-copy (2)"-P "*.csproj"-T "E:\Framework\Tenoner-copy (2) \bak", can support: deep handcuffs The option of PayPal, copying files that match the specified pattern, overwriting, and so on.
Using Commandlineparser
Commandlineparser is a lightweight tool that uses a very simple answer and the official also has tutorials.
Option class
Copy Code code as follows:
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 = "Source directory. ")]
public string SourcePath {get; set;}
[Option (
' P ', ' pattern ', Required = True,
HelpText = "file mode. ")]
public string Searchpattern {get; set;}
[Option (
' t ', ' target ', Required = True,
HelpText = "Destination directory. ")]
public string TargetPath {get; set;}
[Option (' A ', ' all ', DefaultValue = True,
HelpText = "Do you want to include all directories?" ")]
public bool Alldirectories {get; set;}
[Option (' O ', ' overwrite ', DefaultValue = True,
HelpText = "Do you want to overwrite all files?" ")]
public bool Overwrite {get; set;}
[Option (' V ', ' verbose ', DefaultValue = True,
HelpText = "Do you want to print messages?" ")]
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));
}
}
}
}
Tool class
Copy Code code as follows:
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 ("Copy file: {0} to {1}", SourceFile, TargetFile);
},
(Exceptioninfo) =>
{
Options. WriteLine (ExceptionInfo.Exception.Message);
Exceptioninfo.exceptionhandled = true;
},
Options. Alldirectories,
Options. Overwrite);
}
}
}
}