Objective
C # developed a console program that receives the string[] args parameter by default. If more than one parameter needs to be entered, it can be entered sequentially, but if some parameters are not required, or if some of the parameters need to have spaces such as time "2016-05-18 24:35:00", it is more troublesome to deal with them. Some common command-line tools provide a way to specify parameters, such as: curl
C:\users\administrator>curl--help
Usage:curl [Options ...] <url>
Options: (H) means Http/https only, (F) means FTP only
--anyauth Pick "Any" authentication Method (H)
-a/--append Append to target file when uploading (F)
--basic Use HTTP Basic authentication (H)
--cacert <file> CA Certificate to verify peer against (SSL)
--capath <directory> CA directory to verify peer against (SSL)
-e/--cert <cert[:p asswd]> Client certificate file and password (SSL)
The CommandLine to be introduced here is to help us to easily complete the parameter receiving and help output of the open source class library , and it can convert the received parameters into objects, to facilitate the processing of the program.
Tutorial
- Create a new console project and install CommandLine.
You can download, compile, reference CommandLine.dll, or use NuGet to install Install-package Commandlineparser
2. New parameter Description class Options
First, add a namespace
Using CommandLine;
Using Commandline.text;
Then, define the options class
1 classOptions2 {3[Option ('R',"Read", Metavalue ="FILE", Required =true, HelpText ="input Data File")]4 Public stringInputfile {Get;Set; }5 6[Option ('W',"Write", Metavalue ="FILE", Required =false, HelpText ="Output Data File")]7 Public stringOutputFile {Get;Set; }8 9 Ten[Option ('s',"Start-time", Metavalue ="STARTTIME", Required =true, HelpText ="Start Time")] One PublicDateTime StartTime {Get;Set; } A -[Option ('e',"End-time", Metavalue ="ENDTIME", Required =true, HelpText ="End Time")] - PublicDateTime EndTime {Get;Set; } the - - [Helpoption] - Public stringgetusage () + { - returnHelptext.autobuild ( This, current = Helptext.defaultparsingerrorshandler ( This, current)); + } A at}
3. Modify the main function of the console program
1 //header information when outputting information2 Private Static ReadOnlyHeadinginfo Headinginfo =NewHeadinginfo ("Demo Program","V1.8");3 4 Static voidMain (string[] args)5 {6 //This output will be preceded by a "demo program" Word7Headinginfo.writeerror ("error data that contains header information");8Headinginfo.writemessage ("message data that contains header information");9 TenConsole.WriteLine ("error data that does not contain header information"); OneConsole.WriteLine ("message data that does not contain header information"); A - varOptions =NewOptions (); - if(CommandLine.Parser.Default.ParseArguments (args, options)) the { -Console.WriteLine ("Input File:"+options. Inputfile); -Console.WriteLine ("Output File:"+options. OutputFile); - +Console.WriteLine ("Start time:"+ Options. Starttime.tostring ("yyyy mm month DD Day hh point mm min")); -Console.WriteLine ("End Time:"+ Options. Endtime.tostring ("yyyy mm month DD Day hh point mm min")); + Console.read (); A } at //Else - //{ - //Console.WriteLine (Options. Getusage ()); - //Console.read (); - //} - in Console.read (); -}
3. Test the console program
Do not enter any parameters, output the parameter description information, such as:
Input parameters, such as:
The time and string type of the field are obtained by the value.
C # Console program parameter resolution Class library commandline simple instructions for use