how to write a program that takes arguments from commandline?

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
T

tmp123

Martin said:
Hi,

I'm learning C-programming. I have a program which I would like to
modify so it takes arguments from the commandline. Let call the program:
program.exe.

Could somebody shortly explain how I get this behaviour:

C:>program -help or C:>program -h
printf("\nBla. bla. Here is some help and arguments\n").... etc.

C:>program -dt=0.1 -tend (etc. additional switches might be added).
In the program:

float (or double) dt should become 0.1. The integer variable named
"tend" should be assigned respectively to 10.

In case of any problems such as for instance C:>program dt­g, the
program should respond with something like "dt: Invalid syntax. Exiting.".

I suspect that one should change the program such that "int main(??
something goes in here, right?)" instead of int main(void), but I'm not
really sure of how to address this problem. If somebody has any sample
code in C to post, I would be very happy.

Thanks in advance for any hints...


Med venlig hilsen / Best regards
Martin Jørgensen

Hi,

The answers from other persons have been very complete, and fully
according to the objective of this group. However, I thing that the
subject, from the point of view of learning, can not be complete
without expend a few lines to reference the existence of the function
"getopt" (and, sometimes, "getopts"). Their usage and validity for one
specific operating system could be considered off-topic.
 
K

Keith Thompson

Martin Jørgensen said:
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.
 
M

Mark McIntyre

If the arguments isn't space-separated, how else would they be separated?

colons, tabs, semicolons, slashes, any number of possibities.
Different OSes do different things.
Mark McIntyre
 
A

Arndt Jonasson

The answers from other persons have been very complete, and fully
according to the objective of this group. However, I thing that the
subject, from the point of view of learning, can not be complete
without expend a few lines to reference the existence of the function
"getopt" (and, sometimes, "getopts"). Their usage and validity for one
specific operating system could be considered off-topic.

The manual page for 'getopt' on HP-UX lists the following:
STANDARDS CONFORMANCE
getopt(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
optarg: AES, SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
opterr: AES, SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
optind: AES, SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
optopt: AES, SVID3, XPG4, POSIX.2

There are most likely public domain implementations of it as well.
 
R

Richard Bos

Mark McIntyre said:
colons, tabs, semicolons, slashes, any number of possibities.
Different OSes do different things.

In fact, there's nothing in the Standard that requires the contents of
argv to come from a command line. One could have an implementation on an
all-WIMP environment that pops up a little dialog with an extensible
list of text entry fields each time a program is run.

Richard
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

tmp123 wrote:
-snip-
The answers from other persons have been very complete, and fully
according to the objective of this group. However, I thing that the

I fully agree.
subject, from the point of view of learning, can not be complete
without expend a few lines to reference the existence of the function
"getopt" (and, sometimes, "getopts"). Their usage and validity for one
specific operating system could be considered off-topic.

I will try google for that. Thanks for all the answers from everyone...
I'll get back if I run into trouble.


Med venlig hilsen / Best regards
Martin Jørgensen
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top