If you were/are familar with linux development, maybe you had heard/used getopt.
Mono.GetOptions is an assembly/library like getopt.
Here I will introduce how to use it, the following are the steps:
- Install Mono.
- Add the following attribute in AssemblyInfo.cs:
//Simple usage, it will display in help
[assembly: Mono.UsageComplement("[-t:param] [-o] [-h] [-v] [-u]")]
//Description about this program
[assembly: Mono.About("A sample about how to use Mono.GetOptions")]
//Author
[assembly: Mono.Author("your_name")]
//If this program has bugs, user can report bugs to whom. This is optional.
[assembly: Mono.ReportBugsTo("your_name@your_domain.com")]
//More detail description. This is optional.
[assembly: Mono.AdditionalInfo("This is a sample about how to use Mono.GetOptions. It's only a small program. If you want to know more about Mono.GetOptions, you can hack Mono.GetOptions source code.")]
//Belong to which package. This is optional.
[assembly: Mono.IsPartOfPackage("Samples")]
Then you should modify these attribute:[assembly: AssemblyTitle("Mono.GetOptions Sample")]
[assembly: AssemblyProduct("getoptions_sample")]
[assembly: AssemblyCopyright("your_name or your_organization")]
[assembly: AssemblyVersion("1.0.*")] - Reference Mono.GetOptions. If you use SharpDevelop, you can select "Add mono reference".
- Remember using Mono.GetOptions.
using Mono.GetOptions;
- New a class to inherit Options.
using System.Collections.Generic;
class ProgramOptions : Options
{
private string _outputFolder = "output";
public string OutputFolder
{
get { return _outputFolder; }
set { _outputFolder = value; }
}
private List_templateFolders;
// Max occurance are 2, you can use -t or --template
// Usage: -t:test or --template:test
[Option(2, "Template folder, default is 'default'", 't', "template")]
public WhatToDoNext DoTemplate( string s )
{
//Console.WriteLine( "template={0}", s );
_templateFolders.Add( s );
return WhatToDoNext.GoAhead;
}
// Default allow occurs once, option is -o or --output
[Option("Output folder, default is 'output'", 'o', "output")]
public WhatToDoNext DoOutput( string s )
{
//Console.WriteLine( "output={0}", s );
_outputFolder = s;
return WhatToDoNext.GoAhead;
}
public ProgramOptions()
{
// Accept Linux / Windows
this.ParsingMode = OptionsParsingMode.Both;
_templateFolders = new List();
}
} - Use the class in Main():
ProgramOptions options = new ProgramOptions();
That's all.
options.ProcessArgs(args);
foreach( string s in options.TemplateFolders )
Console.WriteLine( "Template folder = {0}", s );
Console.WriteLine( "Output folder = {0}", options.OutputFolder ); - After you built, you can use it in console mode, I suppose your target is "your_program":
#your_program -h
#your_program --help
#your_program --template:t1 --template:t2 --output:o1
#your_program -t:t1 -t:t2 -o:o1
#your_program -u
#your_program -V
#your_program --version
If you are interested in getopt, please visit here: getopt.
This post is another tutorial: The PumaCode.org Blog :: GetOpt style command line processing in C# with Mono.GetOptions
沒有留言:
張貼留言