CmdArgsExtractor
There are many ways of getting the values of arguments from Main(string[] args)
. When you write a console type application that expects a user to pass some parameters, for example...
copyFile /source:c:\test.txt /destination:g:\new_test.txt
... you'll need to grab:
- c:\test.txt as the source file, and
- g:\new_test.txt as destination
CmdArgsExtrator.dll can help you achieve this in just a few lines of code.
Step 1 - Preparation
- Download and extract CmdArgsExtractor.dll
- Open an existing or create a new "Console Application" project
- Add a reference to CmdArgsExtractor.dll
Step 2 - Using Constructors
Option 1
Let's say, we expect a user to pass arguments in the following format:
"/a:alpha", "/b:beta", "/t:theta"
First, declare your valid prefixes.
For the above example, you'll code this as follows:
string[] prefixes = new string[] { "/a:", "/b:", "/t:" };
Now instantiate the CmdArgsExtractor
class, where:
- '/' is the argument separator, and
- ':' is the argument value separator
CmdArgExtractor cae = new CmdArgExtractor(prefixes, '/', ':');
Option 2
If your program takes arguments in the following format...
... then you would instantiate the class in this way:
CmdArgExtractor cae = new CmdArgExtractor('/');
Now you are ready to use the class methods.
Step 3 - Validation
There are a couple of things you may want to check before extracting arguments specified by the user.
1. Do We Have Any Arguments At All?
if ((args.Length != 0) && (args[0].Contains("?"))) {
return;
}
2. Did the User Supply Valid Prefixes?
if (!cae.ValidArgsPrefixes(args)) {
return;
}
Step 4 - Extracting the Arguments
The class implements three methods:
GetArgValues(string[] args)
This returns an array of argument values, and can be used when arguments are expected to be in either "/alpha /beta" or "/a:alpha /b:beta" format.
GetArgsTwoDimArray(string[] args)
This is useful when the arguments are in the format "/a:alpha /b:beta" and you want to code different options for each argument value.
It returns a two-dimensional array where, the first dimension elements are "qualifiers" - for the above example, they will be "a" and "b", and the second dimension elements are the argument values - "alpha" and "beta"
GetArgValuesSimple(string[] args)
This is the same as GetArgValues
but is used when you expect arguments in the format "/alpha /beta". This method again will return an array containing argument values, e.g. "alpha" and "beta".
Example
using System;
namespace Xealcom.Utils {
class Program {
static void Main(string[] args) {
args = new string[] { "/a:alpha", "/b:beta", "/t:theta" };
if (args.Length != 3) {
return;
}
if ((args.Length != 0) && (args[0].Contains("?"))) {
return;
}
string[] prefixes = new string[] { "/a:", "/b:", "/t:" };
CmdArgExtractor cae = new CmdArgExtractor(prefixes, '/', ':');
if (!cae.ValidArgsPrefixes(args)) {
return;
}
string[] myArgs = cae.GetArgValues(args);
Console.WriteLine("---- GetArgValues output ----");
foreach (string arg in myArgs) {
Console.WriteLine("{0}",arg);
}
string[,] my2dArr = cae.GetArgsTwoDimArray(args);
Console.WriteLine("---- GetArgsTwoDimArray output ----");
for (int i = 0; i < my2dArr.GetLength(1); i++) {
Console.WriteLine("Arg qualifier: {0}\tValue: {1}",
my2dArr[0, i], my2dArr[1, i]);
}
}
}}
Hope this helps.
History
- 28th May, 2006: Initial post
Alex B. Clarke is a professioinal software developer specialising in .Net, C#, Asp.Net, SQL, ADSI, CDO, and other technolgies. He writes applications that automate business processes.