Cinchoo – Command Line Argument Parser, Part 12

Inside Command Line Argument Parser – Expression Evaluator

In some cases, you may want to pass some expression as command line arguments and assign them to command line arguments. Cinchoo framework covers this requirement as well.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Let say, you want to pass 100 + 50 as command line argument (N), you can do so as below

N:{{100 + 50}} /I -O:C:\Log -D:%NOW%

Expressions are given in { and } braces. Please note that the symbols needs to have spaces

Cinchoo framework looks into the {100+50} command line argument value, evaluates and assigns the outcome value to corresponding member (Count).

[ChoCommandLineArgObject(ApplicationName = "Hello World", Copyright = "Copyright (c) Cinchoo Inc.")]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [ChoCommandLineArg("D", DefaultValue = "%NOW%", Description = "Business Date.", ShortName = "Today")]
    public DateTime Today
    {
        get;
        set;
    }

    [ChoCommandLineArg("N", DefaultValue = 100, Description = "No of spaces for a TAB.")]
    public int Count
    {
        get;
        set;
    }

    [ChoCommandLineArg("I", DefaultValue = false, FallbackValue = true, Description = "Include Sub directory.")]
    public bool IncludeSubDir
    {
        get;
        set;
    }

    [ChoCommandLineArg("O", IsRequired = true, Description = "Output Directory.")]
    public string OutFolder
    {
        get;
        set;
    }

    [ChoDefaultCommandLineArg(IsRequired = true, Description = "Semi-colon separated list of folders")]
    [ChoTypeConverter(typeof(ChoStringToStringArrayConverter))]
    public string[] Folders
    {
        get;
        set;
    }
}

If the application containing above type executed with the below command line arguments

N:{{100+50}}/I -O:C:\Log -D:%NOW%

Or

N:"{{100 + 50}}" /I -O:C:\Log -D:%NOW%
When creating instance of MyCommandLineArgObject as below,
MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());

And run the application, the commandLineArgObject will contain the below values

Hello World [Version 1.0.0.0]
Copyright (c) Cinchoo Inc.

-- Cinchoo.Core.CommandLineArgs.Test.MyCommandLineArgObject Dump --
Today: 1/6/2012 12:32:00 PM
Count: 150
IncludeSubDir: True
OutFolder: C:\Log

Please visit ExpressionEvaluator section for more detailed information about expressions.

Advertisements

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