BizArk Feature Spotlight – Command-line Parsing

by Brian Brewder February 20, 2010 20:39

Command-line parsing can be a tedious chore when you are building an application that accepts command-line arguments. But what if all you had to do was create a class with the properties that you want to accept? The BizArk framework offers a simple to use command-line parsing utility that allows you to do exactly that.

Command-line parsing in the BizArk framework has these key features:

  • Automatic initialization: Class properties are automatically set based on the command-line arguments.
  • Default properties: Send in a value without specifying the property name.
  • Value conversion: Uses the powerful ConvertEx class also included in BizArk to convert values to the proper type.
  • Boolean flags. Flags can be specified by simply using the argument (ex, /b for true and /b- for false) or by adding the value true/false, yes/no, etc.
  • Argument arrays. Simply add multiple values after the command-line name to set a property that is defined as an array. Ex, /x 1 2 3 will populate x with the array { 1, 2, 3 } (assuming x is defined as an array of integers).
  • Command-line aliases: A property can support multiple command-line aliases for it. For example, Help uses the alias ?.
  • Partial name recognition. You don’t need to spell out the full name or alias, just spell enough for the parser to disambiguate the property/alias from the others.
  • Supports ClickOnce: Can initialize properties even when they are specified as the query string in a URL for ClickOnce deployed applications. The command-line initialization method will detect if it is running as ClickOnce or not so your code doesn’t need to change when using it.
  • Automatically creates /? help: This includes nice formatting that takes into account the width of the console.
  • Load/Save command-line arguments to a file: This is especially useful if you have multiple large, complex sets of command-line arguments that you want to run multiple times.

So how do you use the command-line parsing? Start by creating a class that is derived from CmdLineObject. Add properties with data types that support conversion from a string using the BizArk ConvertEx class. In main, instantiate your class and call Initialize. That’s it.

If you want to validate your command-line or display help, you will need to check a couple of CmdLineObject properties. Other than that, you can just use your object as any other class.

Here’s an example class that uses it (you can also view an example in the RedwerbEntry project, Program.cs):

    static void Main(string[] args)
{
RedwerbCmdLine cmdLine = new RedwerbCmdLine();
cmdLine.Initialize();
// Validate only after checking to see if they requested help
// in order to prevent displaying errors when they request help.
if (cmdLine.Help || !cmdLine.IsValid())
{
DlgMgr.ShowCmdLineHelp(cmdLine);
return;
}
// do your stuff
}
    [CmdLineDefaultArg("SettingsPath")]
public class RedwerbCmdLine
: CmdLineObject
{
public RedwerbCmdLine()
{
SettingsPath = @"C:\Garb\Settings.rdb";
FadeInTime = 2000;
FadeOutTime = 1000;
RunTime = 5000;
}
[CmdLineArg(Alias="S", ShowInUsage=DefaultBoolean.True, Required=true)]
[Description("If false the splash screen is not displayed during startup.")]
public bool ShowSplashScreen { get; set; }
[CmdLineArg(Alias = "I")]
[Description("Determines how long it takes for the splash screen to fade in.")]
public int FadeInTime { get; set; }
[CmdLineArg(Alias = "O")]
[Description("Determines how long it takes for the splash screen to fade out.")]
public int FadeOutTime { get; set; }
[CmdLineArg(Alias = "R")]
[Description("Determines how long Redwerb will run.")]
public int RunTime { get; set; }
[CmdLineArg(Alias = "P", Usage = "Settings Path")]
[Description("Path to the settings file.")]
public string SettingsPath { get; set; }
[CmdLineArg(Alias = "M", Usage = "Message")]
[Description("Message to display.")]
public string Message { get; set; }
}

BizArk is available for download on the CodePlex website, http://bizark.codeplex.com.

Powered by BlogEngine.NET 1.6.0.0

About the author

I've been a software developer since 1999 and have been working with .Net since 2002. I love creating software, playing with productivity tools, and improving the process of software development. I hope you enjoy my blog. Please feel free to leave comments or contact me, I would love to hear from you.