command line interface programming question

D

djc

I'm new to this and was wondering what the options are for interpreting the
command line using a CLI program. Specifically methods for interpreting the
parameters passed to the program on the command line.

I noticed that visual studio adds the following for you when creating a new
CLI program:

void main(string[] args)
{
}

so obviously there is an array of strings to use.

1) is this the standard method or are there other options? (i know thats
probably a dumb question)
2) is the args array populated by each parameter passed to the program using
a 'space' as the delimiter?
3) any recomendations for a beginner on ways to parse a command line? for
example, here are some typical structures:
command.exe param1 param2 -easy enough, simple usage of the args array
command.exe -h param1 -z param2 -using switches with a parameter for the
specific switch.
command.exe /h param1 /z param2
command.exe /h:param1 /z:param2

how to implement these different structures? I think I prefer the second way
I listed but would also want it to be flexible enough so that the order of
the switches did not matter.

I think I'm posting too early here and I don't seem to be asking any good
specific questions. Any recomendations or links to where I could find
specific information on implementing these things would be appreciated.

thanks.
 
M

Michael Nemtsev

Hello djc,

d> I'm new to this and was wondering what the options are for
d> interpreting the command line using a CLI program. Specifically
d> methods for interpreting the parameters passed to the program on the
d> command line.
d> I noticed that visual studio adds the following for you when creating
d> a new CLI program:
d>
d> void main(string[] args)
d> {
d> }
d> so obviously there is an array of strings to use.d> 1) is this the standard method or are there other options? (i know
d> thats
d> probably a dumb question)

Yep, it is. but u may even don't specify string[] args

d> 2) is the args array populated by each parameter passed to the
d> program using a 'space' as the delimiter?

yes

d> 3) any recomendations for a beginner on ways to parse a command line?
d> for
d> example, here are some typical structures:
d> command.exe param1 param2 -easy enough, simple usage of the args
d> array
d> command.exe -h param1 -z param2 -using switches with a parameter
d> for the
d> specific switch.
d> command.exe /h param1 /z param2
d> command.exe /h:param1 /z:param2

You need not parse it, because all your parameters are already parsed, and
located in args array.
To get access to them use args[<number>]

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
M

Mohammad Shalabi

Hi,
here is a sample code which will answer most of your questions. for the
alst questrion, i generaly prefere /h:param1 /p:param2 because it gives some
meaning to the params. I recomend to add a help param like /? or -h
namespace CommandLine

{

class Program

{

static void Main(string[] args)

{

foreach (string s in args)

{

Console.WriteLine(s);

}

}

}

}

thnak you.

now for your question
 
D

djc

Thanks for the input. I appreciate it.

Michael Nemtsev said:
Hello djc,

d> I'm new to this and was wondering what the options are for
d> interpreting the command line using a CLI program. Specifically
d> methods for interpreting the parameters passed to the program on the
d> command line.
d> I noticed that visual studio adds the following for you when creating
d> a new CLI program:
d>
d> void main(string[] args)
d> {
d> }
d> so obviously there is an array of strings to use.d> 1) is this the standard method or are there other options? (i know
d> thats
d> probably a dumb question)

Yep, it is. but u may even don't specify string[] args

d> 2) is the args array populated by each parameter passed to the
d> program using a 'space' as the delimiter?

yes

d> 3) any recomendations for a beginner on ways to parse a command line?
d> for
d> example, here are some typical structures:
d> command.exe param1 param2 -easy enough, simple usage of the args
d> array
d> command.exe -h param1 -z param2 -using switches with a parameter
d> for the
d> specific switch.
d> command.exe /h param1 /z param2
d> command.exe /h:param1 /z:param2

You need not parse it, because all your parameters are already parsed, and
located in args array.
To get access to them use args[<number>]

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
D

djc

Thanks for the input. I appreciate it.

Mohammad Shalabi said:
Hi,
here is a sample code which will answer most of your questions. for the
alst questrion, i generaly prefere /h:param1 /p:param2 because it gives some
meaning to the params. I recomend to add a help param like /? or -h
namespace CommandLine

{

class Program

{

static void Main(string[] args)

{

foreach (string s in args)

{

Console.WriteLine(s);

}

}

}

}

thnak you.

now for your question





djc said:
I'm new to this and was wondering what the options are for interpreting
the
command line using a CLI program. Specifically methods for interpreting
the
parameters passed to the program on the command line.

I noticed that visual studio adds the following for you when creating a
new
CLI program:

void main(string[] args)
{
}

so obviously there is an array of strings to use.

1) is this the standard method or are there other options? (i know thats
probably a dumb question)
2) is the args array populated by each parameter passed to the program
using
a 'space' as the delimiter?
3) any recomendations for a beginner on ways to parse a command line? for
example, here are some typical structures:
command.exe param1 param2 -easy enough, simple usage of the args array
command.exe -h param1 -z param2 -using switches with a parameter for
the
specific switch.
command.exe /h param1 /z param2
command.exe /h:param1 /z:param2

how to implement these different structures? I think I prefer the second
way
I listed but would also want it to be flexible enough so that the order of
the switches did not matter.

I think I'm posting too early here and I don't seem to be asking any good
specific questions. Any recomendations or links to where I could find
specific information on implementing these things would be appreciated.

thanks.
 
O

Otis Mukinfus

Thanks for the input. I appreciate it.

Michael Nemtsev said:
Hello djc,

d> I'm new to this and was wondering what the options are for
d> interpreting the command line using a CLI program. Specifically
d> methods for interpreting the parameters passed to the program on the
d> command line.
d> I noticed that visual studio adds the following for you when creating
d> a new CLI program:
d>
d> void main(string[] args)
d> {
d> }
d> so obviously there is an array of strings to use.d> 1) is this the standard method or are there other options? (i know
d> thats
d> probably a dumb question)

Yep, it is. but u may even don't specify string[] args

d> 2) is the args array populated by each parameter passed to the
d> program using a 'space' as the delimiter?

yes

d> 3) any recomendations for a beginner on ways to parse a command line?
d> for
d> example, here are some typical structures:
d> command.exe param1 param2 -easy enough, simple usage of the args
d> array
d> command.exe -h param1 -z param2 -using switches with a parameter
d> for the
d> specific switch.
d> command.exe /h param1 /z param2
d> command.exe /h:param1 /z:param2

You need not parse it, because all your parameters are already parsed, and
located in args array.
To get access to them use args[<number>]

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
By the way, you can also make the Main method return an integer (or I guess
anything else). I always change the code generated by the IDE to read:

static int Main(string[] args)
{
}

That helps loads when running a console app from the scheduler and you need to
have a return value.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
W

William Stacey [MVP]

Here is a MSH "style" parser I did a while back. Define your expected args
via attributes in a class and it parses your command line (ints, string,
arrays, etc). Your arg class is populated with the results or throws your
user defined error message for the type in error. Also has pretty help that
is generated from your arg types.
http://channel9.msdn.com/ShowPost.aspx?PostID=54420

--
William Stacey [MVP]

| I'm new to this and was wondering what the options are for interpreting
the
| command line using a CLI program. Specifically methods for interpreting
the
| parameters passed to the program on the command line.
|
| I noticed that visual studio adds the following for you when creating a
new
| CLI program:
|
| void main(string[] args)
| {
| }
|
| so obviously there is an array of strings to use.
|
| 1) is this the standard method or are there other options? (i know thats
| probably a dumb question)
| 2) is the args array populated by each parameter passed to the program
using
| a 'space' as the delimiter?
| 3) any recomendations for a beginner on ways to parse a command line? for
| example, here are some typical structures:
| command.exe param1 param2 -easy enough, simple usage of the args array
| command.exe -h param1 -z param2 -using switches with a parameter for
the
| specific switch.
| command.exe /h param1 /z param2
| command.exe /h:param1 /z:param2
|
| how to implement these different structures? I think I prefer the second
way
| I listed but would also want it to be flexible enough so that the order of
| the switches did not matter.
|
| I think I'm posting too early here and I don't seem to be asking any good
| specific questions. Any recomendations or links to where I could find
| specific information on implementing these things would be appreciated.
|
| thanks.
|
|
 
J

Jon Skeet [C# MVP]

By the way, you can also make the Main method return an integer (or I guess
anything else). I always change the code generated by the IDE to read:

No, you can't change it to return anything else (and still be an entry
point). Basically, the four valid Main method entry point signatures
are:

static void Main(string[] args)
static void Main()
static int Main(string[] args)
static int Main()

It's in section 10.1 (of the ECMA spec for C# 1.1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top