Nested CommandLineParser

Aug 27, 2007 at 11:06 AM
Edited Aug 27, 2007 at 11:14 AM
How would one created a Nested CommandLineParser?

For example, the SharePoint Administration Command Line tool (STSADM.exe) has a syntax of:

stsadm.exe -o (operation) (parameters)

e.g. stsadm.exe -o deletesite -url http://site
stsadm.exe -o setconfigdb -databaseserver server

where (parameters) is an optional list of additional command-line parameters specific to the operation. This open-ended style fails as the CommandLineParser will fail on the unrecognised items in the (paramters), that also use the hyphen switch. Is there a way to ignore validation on these additional hyphened items and use an ArrayList type? Or perhaps a usage attribute called "clp.NestedUsage" which won't validate?

I was considering using multiple CommandLineParsers but its proving complicated, e.g.

static void Main(string[] args)
{
if (args != null && args.Length >= 2)
{
List<string> operationArgs = new List<string>(args);
List<string> operationParamsArgs = new List<string>(args);

operationArgs.RemoveRange(2, operationArgs.Count - 2);
operationParamsArgs.RemoveRange(0, 2);

Classes.CommandLineArgs cl = new Classes.CommandLineArgs();

if (!cl.ParseAndContinue(operationArgs.ToArray())) return;

string commandLineUsage = string.Empty;
if (!RunOperation(cl.operation, operationParamsArgs, out commandLineUsage))
{
Console.WriteLine(commandLineUsage);
}
}
else
{
Console.WriteLine(cl.GetUsage(CommandLineUsageBadOp));
}
}

Where the operation run in "RunOperation" will have it's own CommandLineParser. The problem with this appraoch is that the optional (parameters) item won't appear in the Usage Statement for the first parser and it's hard to manage errors to the console.

Thanks.