Re: how to write a program that takes arguments from commandline?



Martin Jørgensen <unoder.spam@xxxxxxxxxxxx> writes:
Keith Thompson wrote:
Martin Jørgensen <unoder.spam@xxxxxxxxxxxx> writes:

stathis gotsis wrote:
-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.
The arguments aren't necessarily space-separated; that depends on the
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.
.



Relevant Pages

  • Looking for a extensible shell for debug purpose
    ... process using IPC. ... Now I want to add a thread to this program to start shell, ... extensible shell, to enter command, then invoke the corresponding ... like lua, can embedded into the program, and by running lua script to ...
    (comp.os.linux.development.apps)
  • Re: without shell
    ... >> These commands invoke shell indeed. ... > sequence rather than a single string. ... On UNIX, os.popen is posix.popen, is a simple wrapper ...
    (comp.lang.python)
  • Re: COM Interop
    ... use Reflection to invoke the methods/properties of the COM ... Shell MegaPack: GUI Controls For Drop-In Windows Explorer like Shell ... I am trying to reference a COM dll from my C# code. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Keep getting error with email validation script
    ... What is unsafe about the systemfunction? ... Using the shell is certainly unsafe, but you can use the system ... This is because systemunder *nix uses the fork/exec model. ... If you use the form of systemthat does not invoke a shell, ...
    (comp.lang.perl.misc)
  • Re: how to write a program that takes arguments from commandline?
    ... Mark McIntyre wrote: ... a shell) used to invoke your program. ... argv to come from a command line. ...
    (comp.lang.c)