473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Parsing required and optional command line parameters


Hi,

I'm writing a program that should accept the following parameters:

XMLfile (required)
Logfile (required)
/A (append flag, optional)
/D 123 (delay value, optional, but # should follow /D immediately)
/V 123 (version number, optional, but # should follow /V immediately)

I want the user to have the capability of specifying the optional
parameters in any order (except the required ones - XMLfile and Logfile
which should be 1st and 2nd). What's a simple way of writing a parser
for this?

Thanks!

Nov 17 '05 #1
2 2621
Bry
Can I assume that you pass your parameters as follows

/D:123
/V:123

(i.e. no spaces between the option and value) If so, try something like
this

class Program
{
static void Main(string[] args)
{
int d;
int v;
bool error = false;

foreach (string param in args)
{
switch (param.Substring(0, 3).ToLower())
{
case "/d:":
case "-d:":
d = Convert.ToInt32(param.Substring(3));
break;
case "/v:":
case "-v:":
v = Convert.ToInt32(param.Substring(3));
break;
default:
error = true;
break;
}
}
if (error)
{
Console.WriteLine("Unknown option");
return;
}

// More code
}
}

Nov 17 '05 #2
Bry
You will need to adapt the code to ignore the first parameters that
aren't supplied in the format "/x:123", you could deal with this by
replacing the foreach statement with a for loop, and read from the
thrid parameter

e.g.

for (int loop = 2; loop < args.length; loop++)
{
switch (args[loop].SubString(0,3).ToLower())
// etc.

Don't forget to catch any exceptions, because your user might not
supply any parameters.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Adam Hartshorne | last post by:
Hi All, I am looking for a c++ class or library that will handle command line parsing for me. Any such library is required to be able to work on both windows and linux hopefully without...
8
by: Darius Fatakia | last post by:
Hello, I have a file that I have opened for reading and this file contains lines with several different types of constraint information. For example, here are a few lines: length(0) = 10...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
1
by: mriedel | last post by:
I'm using the InstallContext class to parse the command-line arguments of a console application. The arguments are in the form of "-file=myFile.txt -flag", and the InstallContext object gives me what...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.