<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
  <channel>
    <title>NConsoler Blog</title>
    <description>News and articles about NConsoler - an open source library for building .net console applications</description>
    <link>http://nconsoler.csharpus.com/rss.xml</link>
    <lastBuildDate>Sun, 03 Aug 2008 13:02:40 GMT</lastBuildDate>
    <docs>http://backend.userland.com/rss</docs>
    <generator>RSS.NET: http://www.rssdotnet.com/</generator>
    <item>
      <title>NConsoler announce</title>
      <description>&lt;p&gt;Usually, parsing of arguments in console applications takes a lot of time. I the internet you can find a few solutions that make it easier to implement this task, however they are not simple and flexible, therefore I decided to design a new system based on metadata and call it NConsoler.
&lt;/p&gt;

&lt;p&gt;
The main idea is to find a way to transform a set of arguments to call a particular method. In case when arguments are wrong, an application must return human understandable error message with error details to provide user friendly behavior of an application. Also it is easy to provide help information using metainfo.
&lt;/p&gt;

&lt;p&gt;
Well, to get necessary method by set of arguments, method should be marked. For this functionality we could use attributes that suit ideally for this job. 
I implemented an &lt;code&gt;Action&lt;/code&gt; attribute:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
[&lt;span class="type"&gt;Action&lt;/span&gt;]
&lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;static&lt;/span&gt; &lt;span 
	class="keyword"&gt;void&lt;/span&gt; Method(...)...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Also, methods could have necessary and unnecessary arguments to add flexibility in development process. This could be realized including attributes as well as attribute Action for a method:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
[&lt;span class="type"&gt;Action&lt;/span&gt;]
&lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;static&lt;/span&gt; &lt;span 
	class="keyword"&gt;void&lt;/span&gt; Method(
    [&lt;span class="type"&gt;Required&lt;/span&gt;] &lt;span class="keyword"&gt;string&lt;/span&gt; name,
    [&lt;span class="type"&gt;Optional&lt;/span&gt;(&lt;span class="keyword"&gt;true&lt;/span&gt;)] &lt;span 
	class="keyword"&gt;bool&lt;/span&gt; flag)...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
As far as we know, method, must take all set of In parameters, so for unnecessary parameters we have to set optional values by default&lt;/p&gt;

&lt;p&gt;
The last thing to do before run an application, is to design Main method:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;static&lt;/span&gt; &lt;span 
	class="keyword"&gt;void&lt;/span&gt; Main(&lt;span class="keyword"&gt;params&lt;/span&gt; &lt;span 
	class="keyword"&gt;string&lt;/span&gt;[] args) {
    &lt;span class="type"&gt;Consolery&lt;/span&gt;.Run(&lt;span class="keyword"&gt;typeof&lt;/span&gt;(&lt;span 
	class="type"&gt;Program&lt;/span&gt;), args);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Type that has method marked as &lt;code&gt;Action&lt;/code&gt;, should be passed into &lt;code&gt;Run&lt;/code&gt; method and arguments must be passed as well.
&lt;/p&gt;

&lt;p&gt;
Complete source code:
&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
&lt;span class="keyword"&gt;using&lt;/span&gt; System;
&lt;span class="keyword"&gt;using&lt;/span&gt; NConsoler;

&lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;class&lt;/span&gt; Program {
    &lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;static&lt;/span&gt; &lt;span 
	class="keyword"&gt;void&lt;/span&gt; Main(&lt;span class="keyword"&gt;params&lt;/span&gt; &lt;span 
	class="keyword"&gt;string&lt;/span&gt;[] args) {
        &lt;span class="type"&gt;Consolery&lt;/span&gt;.Run(&lt;span class="keyword"&gt;typeof&lt;/span&gt;(&lt;span 
	class="type"&gt;Program&lt;/span&gt;), args);
    }

    [&lt;span class="type"&gt;Action&lt;/span&gt;]
    &lt;span class="keyword"&gt;public&lt;/span&gt; &lt;span class="keyword"&gt;static&lt;/span&gt; &lt;span 
	class="keyword"&gt;void&lt;/span&gt; Method(
        [&lt;span class="type"&gt;Required&lt;/span&gt;] &lt;span class="keyword"&gt;string&lt;/span&gt; name,
        [&lt;span class="type"&gt;Optional&lt;/span&gt;(&lt;span class="keyword"&gt;true&lt;/span&gt;)] &lt;span 
	class="keyword"&gt;bool&lt;/span&gt; flag) {
        &lt;span class="type"&gt;Console&lt;/span&gt;.WriteLine(&lt;span class="string"&gt;"name: {0}, flag: {1}"&lt;/span&gt;, name, flag);
    }
}
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;
Let's run our application:&lt;/p&gt;
&lt;pre class="console"&gt;
&lt;kbd&gt;&amp;gt; program.exe "Max"&lt;/kbd&gt;  
&lt;samp&gt;name: Max, flag: true&lt;/samp&gt;
&lt;/pre&gt;

&lt;p&gt;
or with inversed flag:&lt;/p&gt;
&lt;pre class="console"&gt;
&lt;kbd&gt;&amp;gt; program.exe "Max" /-flag&lt;/kbd&gt;
&lt;samp&gt;name: Max, flag: false&lt;/samp&gt;
&lt;/pre&gt;

&lt;p&gt;
Certainly, in definition of Action method, user could make a mistake and in most libraries source of error is not obvious therefore we feel that adequate error message is extremely important, I has pointed this at the begin of the article. Before start an action method, NConsoler checks metainformation as well as parameters of command line and displays detail error message with eventual cause of error. This is continuation of strategy "Design by contract" that fall back usage to open help in time you work with library.
&lt;/p&gt;</description>
      <link>http://nconsoler.csharpus.com/blog/2008/8/3/intro/</link>
      <pubDate>Sun, 03 Aug 2008 13:02:40 GMT</pubDate>
    </item>
  </channel>
</rss>