Overview

Lukianol.Arguments is a small assembly to work with your application arguments in a nice way. Using this API a developer can define his mapping of arguments, make them required, add custom validation rules, get a report of failed validations and more. An alpha version of this assembly has been released. Please use at your own risk.

Lukianol.Arguments By Example

#1 Fluent mapping using the API:

          var map = new ArgumentsMap()                                   
                         .AddKnownArgument("address")
                              .Required()
                         .AddKnownArgument("useProxy")
                              .Switch()
                         .AddKnownArgument("proxyUsername")
                              .RequiredWhenSwitched("useProxy", "Provide a username for the proxy")
                         .AddKnownArgument("proxyPassword")
                              .RequiredWhenSwitched("useProxy", "Provide a password for the proxy")
                         .AddKnownArgument("timeout")
                              .Type<TimeSpan>();


#2 Dynamic result support:

        [Test]
        public void DynamicArgumentsParserResultShouldWork()
        {
            var args = new List<string>
            {
                  "--timeout",
                  "00:00:05"
            };

            var map = new ArgumentsMap()
                .AddKnownArgument("timeout")
                .Type<TimeSpan>();

            var parser = new ArgumentParser(map);

            var result = parser.ParseAsDynamicResult(args);

            Assert.AreEqual(TimeSpan.FromSeconds(5), result.Timeout);
            Assert.IsNull(result.doesNotExists);
        }


#3 Coerce Value Support

This is useful for example when you may ask a user to input data on parsing. Check the test below

        [Test]
        public void CoerceValueWorks()
        {
            var map = new ArgumentsMap().AddKnownArgument("password")
                .CoerceValue((v, name) =>
                                 {
                                     Assert.AreEqual("password", name);
                                     Assert.IsNull(v);
                                     return "entered_from_console";

                                 })
                .Required();

            var result = new ArgumentParser(map).Parse(null);

            Assert.IsTrue(result.IsValid);
            Assert.AreEqual("entered_from_console", result["password"]);
        }


P.S.:
Experimental - here you can find experimental functions which are not included in Downloads but exists in the head revision of the source code

Last edited Apr 28, 2011 at 8:09 PM by ljuk, version 11

Comments

No comments yet.