Monthly Archives: December 2011

Cinchoo – Command Line Argument Parser, Part 5

Inside Command Line Argument Parser – Required Value

In this section, I’ll explain about defining Required command-line  arguments members in command line argument object. You can specify required flag to any member of Command Line object.

Cinchoo framework will validate and throw an exception if a required command-line argument is missing.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

In a sample Command Line object below,

public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [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 OutputDir
    {
        get;
        set;
    }
}

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

/N:200 /I

Or

N:200 /I -O:

When creating instance of MyCommandLineArgObject, the framework automatically parses the command line arguments, sees there is no value passed for “O” argument and throws an exception with the below message

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());
In this sample, framework throws a ChoCommandLineArgException with the below message
Found exception while loading `O` command line argument.

Missing arg value for 'O' required command line switch.

Cinchoo.Core.CommandLineArgs.Test.exe -N:<int> -I:{True|False} -O:[<string>]

Where
        -D      Business Date.
        -N      No of spaces for a TAB.
        -I      Include Sub directory.
        -O      Output Directory.

Cinchoo – Command Line Argument Parser, Part 4

Inside Command Line Argument Parser – Fallback value

In this section, I’ll explain about defining and using Fallback Value in the members of command line argument object. You can specify optional Fallback Value for each member of Command Line object.

Cinchoo framework will set these Fallback values to members if the corresponding command line argument switch value is null/empty.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

In a sample Command Line object below,

[ChoCommandLineArgObject]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [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 OutputDir
    {
        get;
        set;
    }
}

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

/I -O:C:\output

In here, the command-line switch “I” is specified with no value. In this case, the framework uses the FallbackValue for “I” command-line switch.

When creating instance of MyCommandLineArgObject, the framework automatically parses the command line arguments and sets the Fallback value for“I” command-line switch to true, as no value is passed.

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());
In this sample, the Count parameter will be defaulted to 100, as it is missing from command line arguments.
Output will be as below
-- Cinchoo.Core.CommandLineArgs.Test.MyCommandLineArgObject Dump --
 Count: 100
 IncludeSubDir: True
 OutputDir: C:\output

The difference between DefaultValue and FallbackValue is that, the DefaultValue will be set irrespective of the command-line switch passed or not. FallbackValue will be set only when the corresponding command-line switch passed with  null/empty value.


Cinchoo – Command Line Argument Parser, Part 3

Inside Command Line Argument Parser – Default Value

In this section, I’ll talk about defining and using DefaultValue in the members of command line argument object. You can specify DefaultValue for each members of Command Line object.

Cinchoo framework will set the default values to members if the corresponding command line argument switch is missing.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

In a sample Command Line object below,

[ChoCommandLineArgObject]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [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 OutputDir
    {
        get;
        set;
    }
}

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

/I -O:C:\output

When creating instance of MyCommandLineArgObject, the framework automatically parses the command line arguments and sets the values appropriately to the members of this object.

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());
In this sample, the Count parameter will be defaulted to 100, as it is missing from command line arguments.
Output will be as below
-- Cinchoo.Core.CommandLineArgs.Test.MyCommandLineArgObject Dump --
 Count: 100
 IncludeSubDir: True
 OutputDir: C:\output

Cinchoo – Command Line Argument Parser, Part 2

Inside Command Line Argument Parser

In this section, I’ll talk about some of the Command Line argument Parser features

  • Automatic initialization: Class members are automatically set based on the command-line arguments.
  • Default Value: Set in a value when the command line switch is not specified.
  • Fallback Value: Set in a value when the command line switch value is not specified.
  • Value conversion: Uses the powerful .NET converter and ChoConverter classes for value conversion.
  • Boolean flags: Flags can be specified by simply using the argument (ex, /b for true, otherwise false) or by adding the value /b:true /b:false.
  • Auto Help Text: Automatically framework creates usage text.
  • Load command-line arguments from a file: If you have multiple large, complex sets of command-line arguments that you want to run multiple times.
  • Event Handlers: Provides better event handler mechanism to carryout validation, value conversion, inspection etc

Automatic Initialization

Cinchoo framework automatically set the members of Command Line argument object while creating instance of it. For a below sample command line argument object

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

[ChoCommandLineArgObject]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [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 OutputDir
    {
        get;
        set;
    }
}

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

-N:100 /I -O:C:\output

When creating instance of MyCommandLineArgObject, the framework automatically parses the command line arguments and sets the values appropriately to the members of this object.

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());
Output will be as below
-- Cinchoo.Core.CommandLineArgs.Test.MyCommandLineArgObject Dump --
 Count: 100
 IncludeSubDir: True
 OutputDir: C:\output

During the course of command line parsing, any failure will be thrown as ChoCommandLineArgException.


Cinchoo – Command Line Argument Parser, Part 1

Simplified Command Line Argument Parser

Cinchoo framework offers an another feature, simplified command line argument parser. It provides the below set of features

  • Auto-generation of usage
  • Able to check required and optional parameters
  • Type safe command line argument object
  • Support flag parameters
  • Read command line arguments from file etc

Overview

The command line input, read from the Environment.GetCommandLineArg as string array, is then pushed to an instance of a custom type decorated with ChoCmdLineArgumentObjectAttrribue and derived from ChoCmdLineArgumntObject by the framework. You just have to create this class with one field for each command line value decorated with ChoCmdLineArgumentAttribute.

How to use

First, add a reference to Cinchoo.Core.dll to your project.

Namespace: Cinchoo.Core.Shell

Then, create your custom class to consume  -I -O:\OutDir -N:4 C:\InputDir1;C:\InputDir2 command line arguments,

where

-N   Number of spaces for TAB character (integer type)

-I    Include sub directory

-O  Output directory

-F   Semi-Colon separated list of Folders

Here is the custom class reflects the above specification

[ChoCommandLineArgObject]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
	[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 OutputDir
	{
		get;
		set;
	}

	[ChoCommandLineArg("F", IsRequired = true, Description = "Semi-colon seperated list of folders")]
	public string Folders
	{
		get;
		set;
	}
}

Finally instantiate and use the object from the above custom command line class

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();

Please note the instance created above is the singleton. The Cinchoo framework always return the same instance no matter how many times you instantiate using new operator.

That’s all folks, please try it out.


Cinchoo – Files & Folders cleanup command line tool

ChoFileFolderCleanupConsoleTool

Download Binary (Zip)

Download Source Files (Zip)

This command line tool to remove matching files and folders from disk. It is written in using C#/VS.NET 2010.

You require .NET Framework in order to run this tool. You can download it from here.

Features

  • Clean up files and folders matching regular expression pattern
  • Works with single file as well as files in a folder
  • Runs on Windows 95/98/ME/NT4/2k/2k3/2k8/XP/Vista/7

How to use

  • Run the ChoFileFolderCleanupConsoleTool.exe file in command line with the following command line parameters

/P                   Turn on preview mode.
/I                     Include subdirectry.
/R                   Remove directory if empty.
/MD               Match folder names.
/DMF            Don’t Match file names.
/F                   File name matching regular expression.
<default>      Semicolon(;) seperated Files or Folders path.

Example

ChoFileFolderCleanupConsoleTool.exe  /I  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"
ChoFileFolderCleanupConsoleTool.exe  /I /MD  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"
ChoFileFolderCleanupConsoleTool.exe  /DMF /I /P  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"

PS
Please visit Regular Expression Cheat Sheet for help in creating regular expression


Cinchoo – Files & Folders Cleanup tool

ChoFileFolderCleanupTool

Download Binary (Zip)

Download Source (Zip)

This tool helps to remove matching files and folders from disk. It was written in using WPF/C#/VS.NET 2010.

Features

  • Clean up files and folders matching regular expression pattern
  • Works with single file as well as files in a folder
  • Runs on Windows 95/98/ME/NT4/2k/2k3/2k8/XP/Vista/7

How to use

  • Click ‘Start’ whenever you are ready to clean up files. Any time you can can start this cleanup process.
  • Type a full path of the file or folder in ‘Choose a File / Folder’ text box, then click ‘Add’ button to add it to ‘File(s) / Folder(s)’ list box. Or you can click ‘Browse’ button, to choose the file/folder for cleanup.
  • At any time, you can select an item in ‘File(s) / Folder(s)’ list box, remove it using ‘Remove’ button.
  • In ‘Matching Expr’ text box, you can specify the regular expression to identify files and folders for cleanup. (Please visit Regular Expression Cheat Sheet for help in creating regular expression)
  • Check ‘Match Files’ or ‘Match Folders’ check boxes, in order to include them for cleanup process respectively.
  • Check ‘Preview Mode’ option, if you just want to see the files and folders are cleaned in the ‘Status’ box or in the log file under ‘Logs’ folder. This option will not delete files or folders.
  • Check ‘Rm Dir If Empty’ option, if you want to remove empty directory during this process.
  • Check ‘Include Sub Dir’ option, to include all sub directories for clean up process.
  • In ‘Status’ box, you can view the statues of the file conversion.
  • Click ‘Clear’ button anytime to clear the ‘Status’ box.

Tips

  • You can drag and drop file(s) or folder(s) to this tool for easy cleanup process.

Cinchoo – TABS to Spaces converter command line tool

ChoTABSToSpacesConversionConsoleTool

Download Binary (Zip)

Download Source (Zip)

This command line tool converts tab characters to space characters and vice verse in text files. It is written in using C#/VS.NET 2010.

You require .NET Framework in order to run this tool. You can download it from here.

Features

  • Converts any text files
  • Works with single file as well as files in a folder
  • Number of Tab Size can be customized
  • Runs on Windows 95/98/ME/NT4/2k/2k3/2k8/XP/Vista/7

How to use

  • Run the ChoTabsToSpacesConversionConsoleTool.exe file in command line with the following command line parameters

/SI                   Turn on Spaces to TABS, otherwise converts TABS to Spaces.
/FX                 Comma seperated file extensions.
/I                     Include subdirectry.
/O                   Output directory.
/N                   TAB size in spaces.
<default>      Semicolon(;) seperated Files or Folders path.

Example

ChoTabsToSpacesConversionConsoleTool.exe  /I  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"
ChoTabsToSpacesConversionConsoleTool.exe  /I /N:4  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"
ChoTabsToSpacesConversionConsoleTool.exe  /SI /I /N:4  "C:\SampleFileOrFolder1,C:\SampleFileOrFolder2"

Follow

Get every new post delivered to your Inbox.