星期五, 7月 06, 2007

[.Net]How to use Mono.GetOptions?

What is Mono.GetOptions?
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:
  1. Install Mono.
  2. 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.*")]
  3. Reference Mono.GetOptions. If you use SharpDevelop, you can select "Add mono reference".
  4. Remember using Mono.GetOptions.
    using Mono.GetOptions;
  5. 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();
    }
    }
  6. Use the class in Main():
    ProgramOptions options = new ProgramOptions(); 
    options.ProcessArgs(args);
    foreach( string s in options.TemplateFolders )
    Console.WriteLine( "Template folder = {0}", s );
    Console.WriteLine( "Output folder = {0}", options.OutputFolder );
    That's all.
  7. 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

沒有留言: