ExecOptions.Net

by sandreo Email

Two weeks ago, I released the library ExecOptions.Net on CodePlex.

ExecOptions.Net is a library providing an easy handling of console application arguments.

Every developer will have to deal a day or an other with Console arguments, like checking a file existence, some string patterns matching (I hope that there is not only GUI developers :>> ).
And because writing such a code it is really boring I developed this library.

Currently there is a minimal set of features:

  • Defining an options as mandatory or optional
  • Checking file existence
  • Validating an XML file against its XML schema
  • Defining regular expression matching

A cool feature is the possibility to define your options as .Net attributes (like Mono.Options.dll ). The code should be something like that

[OptionDelimiter('-')]
internal class TestOption
{
    private bool m_Usage;
    [Option("h",
            OptionCategory.Optional,
            "print program usage"
            OptionType.Usage)]
    public bool Usage
    {
        get { return m_Usage; }
        set { m_Usage = value; }
    }
    private bool m_Verbose;
    [Option("v", 
            OptionCategory.Optional,
            OptionType.Empty,
            "activate verbose mode")]
    public bool Verbose
    {
        get { return m_Verbose; }
        set { m_Verbose = value; }
    }
    private String m_XmlFile;
    [Option("input", 
            OptionCategory.Optional, 
            OptionType.Xml, 
            "input file", 
            "..\\..\\test.u\\schema.xsd")]
    public String XmlFile
    {
        get { return m_XmlFile; }
        set { m_XmlFile = value; }
    }
    private String m_RegEx;
    [Option("pattern", 
            OptionCategory.Mandatoy, 
            OptionType.RegEx, 
            "regular ex", "[a-z]{0,}")]
    public String RegEx
    {
        get { return m_RegEx; }
        set { m_RegEx = value; }
    }    
}

Your application Main will be really simple.

class Program
{
    [STAThread]
    static int Main(string[] args)
    {
        OptionManager mgr = new OptionManager();
        TestOption opt = new TestOption();
        mgr.ProcessArgs<TestOption>(args, opt);
        if(opt.Usage)
        {
            Console.WriteLine(mgr.Usage());
            return 0;
        }
        /* Your Business Code */
    }
}

I hope that such a library can help you.

Feedbacks ,bugs or features are welcomed !

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

4 comments

Comment from: sami [Member] Email
Sébastien,

Tu t'es inspiré de Apache CLI pour ce Framework ?

http://commons.apache.org/cli/introduction.html

Il est assez complet et un portage "tel quel" en .NET aurait été aussi sympa. D'autant plus qu'à priori ce n'est pas le genre d'outil qui utilise des API liées à la plateforme..

Sami
12/07/07 @ 09:25
Comment from: amethyste [Member] Email
c'est marrant, il y a deux semaines on a justement passé un peu de temps à développer un truc de ce genre pour un projet

On devrait aller plus souvent sur CodePlex....
12/07/07 @ 10:44
Comment from: Julien Brunet [Member] Email · http://www.julienbrunet.com
Je crois qu'il existe aussi quelquechose de similaire chez Mono (MonoOptions il me semble).
C'est en tout cas un besoin récurrent en effet.

Ca me fait aussi penser à PowerShell et ses Cmdlets - framework pratique mais les gars de MS n'ont pas songé à le généraliser à un usage classique .exe - C'eut pourtant été trivial (De même qu'ils ne se sont pas intéressés aux redondances aves les tasks MSBuild. Ah, le développement en mode Silo quand tu nous tiens !)
12/07/07 @ 11:01
Comment from: sandreo [Member] Email
En fait je me suis inspiré au niveau de la fonctionalité d'une library perl (oui oui oui iln'y a pas de faute frappe :o)) que j'avais developpé et de Mono.Options pour les attributs.

Mais je vais jetter un coup d'oeil à Apache CLI et pourquoi pas faire un portage.

Seb
PS: Si vous trouvez des trucs à améliorer ou à ajouter un petit mail.

12/07/07 @ 21:22

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)