Lukianol.Arguments. Dynamic Support

I continue to work with the Lukianol.Arguments API. If you might notice it is very ugly when you want to get typed arguments from a result:

result.GetValue<bool>("UseProxy")
result.GetValue<TimeSpan>("timeout")

A lot of letters as for me. What could we do here?

We can gain from C# 4.0 dynamic objects! I have simply inherited ArgumentParserResult from the DynamicObject and overridden the TryGetMember method where  a value is returned by a property name from an underlying arguments’ dictionary. Check this test with the described functional presentation:

[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);

            TimeSpan timeout = result.Timeout;

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

You can find the Lukianol.Arguments at CodePlex

Advertisements
This entry was posted in .NET, C#, Codeplex. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s