. NET command line parser example Program (command line option function)

Source: Internet
Author: User
Tags net command net command line

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 );
}
}
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.