. NET command-line Parser sample program (Command-Line Options feature) _ Practical Tips

Source: Internet
Author: User
Tags net command string format

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



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.