Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the .NET command line parser

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report