need help to read command line arguments in C

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Hi guys. I know that I'm supposed to use argc and *argv[] to find the number and contents of the command line.

But help me think this through... if i have a program executable named "prog1" and I have options such as -v and -p , both of which are supposed to be followed by integers telling me how large of an array I should make, how should I go about reading this input?

example command line:
% prog1 -v 32 -p 16


thanks. I haven't programmed in C in a while now.
 

IaPuP

Golden Member
Mar 3, 2000
1,186
0
0
argc is the number of arguments

argv is the array that holds the arguments


so you specify:

program -v32 -p21

argc is 2
argv[0] is "-v32"
arg[1] is "-p21"

parse them from there however you like. :)

Eric
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
This little snippet of code will pick out any "-p" or "-v" from the suppiled argument list ( order is irrelevant ) and assign the values immediatly following their position as intergers to appropriate variables. It also tests if the numbers provided make sense


const char* ARGNAMES[] = { "p", "v", NULL };

char isnum(char* arg)
{
int len = strlen(arg), k;
for(k=0; k<len; k++)
if(!isdigit(arg[k]))
return 0;
return 1;
}

int main(int argc, char* argv[])
{
int k, j, p = -1, v = -1;
for(k=1; k<argc-1; k++) {
j = 0;
if(*argv[k] == '-' && isnum(argv[k+1]))
while(ARGNAMES[j])
if(!strcmp(ARGNAMES[j++], argv[k]+1)) {
switch(j-1) {
case 0: p = atoi(argv[++k]); break;
case 1: v = atoi(argv[++k]); break;
}
break;
}
}

p != -1 ? printf("p = %d\n",p) : printf("missing or invalid argument [p]\n");
v != -1 ? printf("v = %d\n",v) : printf("missing or invalid argument [v]\n");

return 0;
}
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
you might also have a look at the popt or getopt libraries. They do a nice job of automating argument parsing, and handle things like long and short versions of arguments (-h, --help).

You did't mention your platform ... I've only used them on linux.
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Ahh. thanks for the tips and code everyone. I think i'm gonna go with getopt() because it seems to take the options no matter what order they're given in.

I took a look at the man page for getopt and I'm a bit confused on how to work it. Here's part of what it gave:
------------------------------------------------------------------
EXAMPLES
The following code fragment shows how one might process the
arguments for a command that can take the options a or b, as
well as the option o, which requires an argument:

set -- `getopt abo: $*`
if [ $? != 0 ]
then
echo $USAGE
exit 2
fi
for i in $*
do
case $i in
-a | -b) FLAG=$i; shift;;
-o) OARG=$2; shift 2;;
--) shift; break;;
esac
done

This code accepts any of the following as equivalent:

cmd -aoarg filename1 filename2
cmd -a -o arg filename1 filename2
cmd -oarg -a filename1 filename2
cmd -a -oarg -- filename1 filename2
---------------------------------------------------------------------------------

Anyhow... my options will be something like " -b 32 -p 10 "

I can see how to detect the -b and -p, however, how would I access the 32 and 10 to save them as local variables?

as always- thanks for the help!

p.s - I'm running this in a Unix Solaris OS
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
nevermind. I think I got this thing down now. getopt() with the optarg variable.

Thanks for your help. this thread can DIE now!