Sample requirements
Copy an objectFor example, CopyFiles-s "E: \ Framework \ Tenoner-copy (2)"-p "*. csproj "-t" E: \ Framework \ Tenoner-copy (2) \ Bak ", supports deep copy, copy of files in the specified mode, and overwrite or not.
Use CommandLineParser
CommandLineParser is a lightweight tool. It is easy to use and provides tutorials.
Option class
Copy codeThe Code is 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 = "target directory. ")]
Public string TargetPath {get; set ;}
[Option ('A', "all", DefaultValue = true,
HelpText = "does it contain all directories? ")]
Public bool AllDirectories {get; set ;}
[Option ('O', "overwrite", DefaultValue = true,
HelpText = "overwrite all files? ")]
Public bool Overwrite {get; set ;}
[Option ('V', "verbose", DefaultValue = true,
HelpText = "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
Copy codeThe Code is 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 );
}
}
}
}