Re: how to write a program that takes arguments from commandline?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sun, 12 Feb 2006 23:04:06 GMT
Martin Jørgensen <unoder.spam@xxxxxxxxxxxx> writes:
Keith Thompson wrote:
Martin Jørgensen <unoder.spam@xxxxxxxxxxxx> writes:
stathis gotsis wrote:The arguments aren't necessarily space-separated; that depends on the
-snip-
Maybe this is relevant:
http://c-faq.com/misc/argv.html
Thanks. That looks exactly like the place to start. Also thanks to the
other answers. I assume argc contains the number of (space-separated)
arguments.
mechanism (for example, a shell) used to invoke your program.
If the arguments isn't space-separated, how else would they be separated?
Who knows? A C program gets zero or more arguments when it's
executed; it can retrieve them via argc and argv. The way those
arguments were generated is outside the scope of the C language.
Wandering a little off-topic ...
In a Unix-like system, programs are often invoked from a shell
command line. Shells *can* use spaces to delimit arguments, but the
syntax is more complex; for example:
my_program 'foo bar' baz
will invoke my_program with two arguments: "foo bar" and "baz".
Programs can also be invoked directly using one of the exec*() family
of functions. For example:
execlp("my_program", "foo bar", "baz", (char*)NULL);
Here, the arguments are provided directly as separate strings; they're
not delimited at all.
As I said, the details are off-topic; I discuss them here merely to
provide an example of the kind of thing that can happen before your
program is executed.
If you're writing a shell yourself, you can pick any mechanism you
like for constructing the arguments for the programs you invoke.
Delimiting the arguments with spaces (or, more generally, with
whitespace) is a reasonable approach, but it's not the only one.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- how to write a program that takes arguments from commandline?
- From: Martin Jørgensen
- Re: how to write a program that takes arguments from commandline?
- From: stathis gotsis
- Re: how to write a program that takes arguments from commandline?
- From: Martin Jørgensen
- Re: how to write a program that takes arguments from commandline?
- From: Keith Thompson
- Re: how to write a program that takes arguments from commandline?
- From: Martin Jørgensen
- how to write a program that takes arguments from commandline?
- Prev by Date: Re: problem opening a file
- Next by Date: Re: Command Line Interface (CLI): your recommendations
- Previous by thread: Re: how to write a program that takes arguments from commandline?
- Next by thread: Re: how to write a program that takes arguments from commandline?
- Index(es):
Relevant Pages
|
|