command line

B

Bill Cunningham

I want to do some things that look complicated with the command line for
example.

int main(int argc, char *argv[])

This is the only other main based parameter I know of other than int
main(void). Now if I wanted to use switches at the command line such as :

real 14 54 -help

-help would display help but I want -help to be recognized anywhere in the
command line input.

real -help 14
real 14 -help

Would this coding involve using argc or *argv[] ?

Bill
 
I

Ian Collins

Bill said:
I want to do some things that look complicated with the command line for
example.

int main(int argc, char *argv[])

This is the only other main based parameter I know of other than int
main(void). Now if I wanted to use switches at the command line such as :

real 14 54 -help

-help would display help but I want -help to be recognized anywhere in the
command line input.

real -help 14
real 14 -help

Would this coding involve using argc or *argv[] ?

Both.

2/10 this time.
 
R

Richard

Bill Cunningham said:
I want to do some things that look complicated with the command line for
example.

int main(int argc, char *argv[])

This is the only other main based parameter I know of other than int
main(void). Now if I wanted to use switches at the command line such as :

real 14 54 -help

-help would display help but I want -help to be recognized anywhere in the
command line input.

real -help 14
real 14 -help

Would this coding involve using argc or *argv[] ?

Bill

Hmm. Normally I would give you about 4/10. But I see you smelt Falconer
running around 4 hours late reading other peoples replies and then
posting them as his own so I'll give you another 2 points for baiting.

6/10.
 
G

Guest

    I want to do some things that look complicated with the command line for
example.

int main(int argc, char *argv[])

This is the only other main based parameter I know of other than int
main(void). Now if I wanted to use switches at the command line such as :

real 14 54 -help

-help would display help but I want -help to be recognized anywhere in the
command line input.

real -help 14
real 14 -help

Would this coding involve using argc or *argv[]  ?

K&R section 5.10 p114
 
R

Richard

Han from China said:
Bill said:
Would this coding involve using argc or *argv[] ?

In one sense of "using", here's something simple and straightforward
that certainly uses both argc and argv:

---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>

void show_help(void)
{
puts("replace me!");
}

int main(int argc, char *argv[])
{
int i;

for(i = 1; i < argc; i++) {
if(!strcmp(argv, "-help")) {
show_help();
return 0;
}
}

return 0;
}
---------------------------------------------------------------------------

But that can be rewritten to use argv only, since the standard requires
that argv[argc] be a null pointer, so that null pointer can be used as a
sentinel value for processing argv without the help of argc. If you're
just starting out with argc and argv, I recommend sticking to something
simple and straightforward for a while.

I have, of course, assumed a hosted environment in this post.


I saw Keith throw this in recently to assert his power level and to
confuse a nOOb. Possibly you could expand, for Bill ..., exactly what is
mean by this "hosted" term and why it is relevant to this solution?
 
B

Bill Cunningham

---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>

void show_help(void)
{
puts("replace me!");
}

int main(int argc, char *argv[])
{
int i;

for(i = 1; i < argc; i++) {
if(!strcmp(argv, "-help")) {
show_help();
return 0;
}
}

return 0;
}
---------------------------------------------------------------------------

But that can be rewritten to use argv only, since the standard requires
that argv[argc] be a null pointer, so that null pointer can be used as a
sentinel value for processing argv without the help of argc. If you're
just starting out with argc and argv, I recommend sticking to something
simple and straightforward for a while.

I have, of course, assumed a hosted environment in this post.


I'll try it thanks.

Bill
 
A

Antoninus Twink

I want to do some things that look complicated with the command
line for example.

Parsing command-line options properly is an annoyingly intricate
business. I'd recommend making use of a library - the authors of the
library will already have discovered and debugged all the anomalies and
corner cases that can arise, leaving you free to spend more time
discovering and debugging all the anomalies and corner cases in the rest
of your code.

Two good free option-parsing libraries are GNU getopt and popt.
 
B

Bill Cunningham

K&R section 5.10 p114

That's hard to read. What exactly is the ? : for?

Bill
 
B

Bill Cunningham

[snip Richard]
Good point. It sucks that my desire to preempt "corrections" that
would only confuse Bill has led me to write something that would
confuse Bill all the same.

The standard defines two environments, a freestanding environment
and a hosted environment.

Informally, it's helpful to think of a freestanding environment
as "shit from the standard library may be missing", such as in
an embedded environment -- you know, if you're programming for
a toaster or something.

Informally, it's helpful to think of a hosted environment as
"your typical personal computer on which you can write C programs".

The only reason the assumption of a hosted environment matters
in this post is that some of the statements made about main(),
including that argv[argc] be a null pointer, are required only
of hosted environments.

My libc.a and libm.a seem to be up to C standard and more.
 
R

Richard

Bill Cunningham said:
[snip Richard]
Good point. It sucks that my desire to preempt "corrections" that
would only confuse Bill has led me to write something that would
confuse Bill all the same.

The standard defines two environments, a freestanding environment
and a hosted environment.

Informally, it's helpful to think of a freestanding environment
as "shit from the standard library may be missing", such as in
an embedded environment -- you know, if you're programming for
a toaster or something.

Informally, it's helpful to think of a hosted environment as
"your typical personal computer on which you can write C programs".

The only reason the assumption of a hosted environment matters
in this post is that some of the statements made about main(),
including that argv[argc] be a null pointer, are required only
of hosted environments.

My libc.a and libm.a seem to be up to C standard and more.

7/10

How is your + operator?
 
R

Richard

Bill Cunningham said:
K&R section 5.10 p114

That's hard to read. What exactly is the ? : for?

Bill

It's for when the program wants to ask the user a question.
 
K

Keith Thompson

Bill Cunningham said:
K&R section 5.10 p114

That's hard to read. What exactly is the ? : for?

I believe k&R explains the conditional operator (? :). What don't you
understand about the explanation?
 
R

Richard

Keith Thompson said:
I believe k&R explains the conditional operator (? :). What don't you
understand about the explanation?

Please do not use smileys in technical posts. Chuck will tell you off.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top