Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the .NET command line parser

Shulou Source: shulou.com Published: 2022-06-03 07:41:12 10月04日 Update

This article mainly introduces "how to understand .NET command line parser". In daily operation, I believe many people have doubts about how to understand .NET command line parser. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand the .NET command line parser"! Next, please follow the editor to study!

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 copy, copy files that match the specified pattern, whether to overwrite and other options.

Use CommandLineParser

CommandLineParser is a lightweight tool that is very easy to answer, and there are official tutorials.

Option class

The copy 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 (

'sworn, "source", Required = true

HelpText = "Source directory.")]

Public string SourcePath {get; set;}

[Option (

'paired, "pattern", Required = true

HelpText = "File mode.")]

Public string SearchPattern {get; set;}

[Option (

'tasking, "target", Required = true

HelpText = "destination directory.")]

Public string TargetPath {get; set;}

[Option ('averse, "all", DefaultValue = true)

HelpText = "does it include all directories?")]

Public bool AllDirectories {get; set;}

[Option ('oiled, "overwrite", DefaultValue = true)

HelpText = "overwrite all files?")]

Public bool Overwrite {get; set;}

[Option ('vested, "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

The copy 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 files: {0} to {1}", sourceFile, targetFile)

}

(exceptionInfo) = >

{

Options.WriteLine (exceptionInfo.Exception.Message)

ExceptionInfo.ExceptionHandled = true

}

Options.AllDirectories

Options.Overwrite)

}

}

}

}

At this point, the study on "how to understand the .NET command line parser" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Files commands code copy learning copies tools more patterns directories help utility next official tutorials articles methods messages depth source directory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi MariaDB NVidia Xiaomi