<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intrepid Blog &#187; commandline</title>
	<atom:link href="http://blog.affien.com/archives/tag/commandline/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:47:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Command line parser for .net</title>
		<link>http://blog.affien.com/archives/2005/02/26/command-line-parser-for-net/</link>
		<comments>http://blog.affien.com/archives/2005/02/26/command-line-parser-for-net/#comments</comments>
		<pubDate>Sat, 26 Feb 2005 18:37:53 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[commandline]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2005/02/26/command-line-parser-for-net/</guid>
		<description><![CDATA[I just wrote a command line parser for .net which parse [...]]]></description>
			<content:encoded><![CDATA[<p>I just wrote a command line parser for .net which parses the command line similar to the way <a href="http://channel9.msdn.com/ShowPost.aspx?PostID=25915">Monad&#8217;s cmdlets</a> do it, with the key difference that this still is for the normal .exe executables.</p>
<p>It&#8217;s very simple, first get yourself a program class, which contains some properties you want the user able to set via the commandline and apply the <code>Parameter</code> attribute:</p>
<blockquote><pre style="font-size: 12px">class CopyProgram
{
    private string _Source;
    [Parameter(Position = 0, IsMandatory = true)]
    public string Source
    {
        get { return _Source;}
        set { _Source = value; }
    }
    private string _Target;

    [Parameter(Position = 1, IsMandatory = true)]
    public string Target
    {
        get { return _Target;}
        set { _Target= value; }
    }

    private bool _WalkRecursivly;
    [Parameter(Name = "recursivly")]
    public bool WalkRecursivly
    {
        get { return _WalkRecursivly;}
        set { _WalkRecursivly= value; }
    }   

    static void Main(string[] args)
    {
        new Program().Run(args);
    }

    void Run(string[] args)
    {
        CommandLine.ParseCommandLine(args, this);

        // Do stuff here
    }
}</pre>
</blockquote>
<p>The <code>CommandLine.ParseCommandLine</code> function takes care of parsing the command line string array and filling the required properties of the class provided.</p>
<p>You can call this program in a few different ways:</p>
<blockquote><pre style="font-size: 12px">copy.exe c:\source.txt c:\target.txt
copy.exe -s c:\source.txt -target c:\target.txt
copy.exe -targ c:\target.txt c:\source.txt  -r</pre>
</blockquote>
<p>Although it only required around 250 lines of code it certainly is an enourmous time safer and makes stuff a lot easier for the user.</p>
<p>At the moment I&#8217;m programming a few small utilities and will change the code a bit and fix some bugs if any.</p>
<p>For those interesting in testing it themselves (I encourage it), just download the <a href="http://w-nz.com/update/Intrepid.Corelib.zip">Intrepid.Corelib .net 2 assembly</a>*. The classes required are in the <code>Intrepid.Automation</code> namespace. Please send any bug reports to: <a href="mailto:bas.westerbaan@gmail.com">bas.westerbaan@gmail.com</a>.</p>
<p>(* You may use everything in the Intrepid.Corelib assembly freely for non-commercial open-source usage. Do not modify or reverse engineer the assembly in any way.)</p>
<p><strong>Update:</strong><br />
Got rid of some bugs and added the <code>RequiresValue</code> property to <code>ParameterAttribute</code>. Will replace lateron with <code>Type</code>, this allows you to use <code>-e bleh</code> where -e takes no value and bleh is interpreted as a nameless parameter. Will add alliases too.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2005/02/26/command-line-parser-for-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

