Hello,
I'm writing a little C program and I need to get the arguments passed on
the commandline.
So I thought about using the getopt function. It seems to work, but there
are some annoyances that I would like to solve. Maybe more experienced
programmers can help me out here. This is actually the first time that I
use getopt in a C program. So any help appreciated.
Also, getopt just gets the arguments. I need to check myself if for example
I want to make some arguments compulsory, right?
Below I included the example code and some test cases I'm wondering about.
Thanks.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
char
lca_directory[50],
lca_printer[50];
int
liv_debug_mode = 0,
opt;
/* initialization */
strcpy(lca_directory, "");
strcpy(lca_printer, "");
while ((opt = getopt(argc, argv, "d:p:D")) != -1){
switch(opt){
case 'd':
strcpy(lca_directory, optarg);
break;
case 'p':
strcpy(lca_printer, optarg);
case 'D':
liv_debug_mode = 1;
break;
case ':':
fprintf(stderr, "option needs a value\n");
break;
case '?':
/* getopt writes an err msg for unknow options */
/* just exit with err when unknow options are given */
/* uncomment if you want to exit on an invalid option */
/* exit(1); */
break;
}
}
/* check if directory is specified */
if (strlen(lca_directory) == 0){
fprintf(stderr, "Syntax Error: you must specify a directory\n");
exit(1);
}
printf("directory: %s\n", lca_directory);
printf(" printer: %s\n", lca_printer);
printf(" debug: %d\n", liv_debug_mode);
exit(0);
Now some tests.Quote:}
1/
Tomsk:~/progs$ ./args -d directory -p printer -D
directory: directory
printer: printer
debug: 1
::::
No problem
2/
Tomsk:~/progs$ ./args -foo
./args: invalid option -- f
./args: invalid option -- o
./args: invalid option -- o
Syntax Error: you must specify a directory
::::
No problem
3/
Tomsk:~/progs$ ./args foo
Syntax Error: you must specify a directory
::::
Why isn't there an error message like above, invalid option.
4/
Tomsk:~/progs$ ./args -d -D
directory: -D
printer:
debug: 0
::::
This is not the behaviour I wished for!!!
-D is an option and is now processed as an argument for -d
--
ICQ# 30116911 Home page: http://www.valcke.com/jeroen
Phone +32(0)56 32 91 37 Mobile +32(0)486 88 21 26
Sleep is for the weak and sickly.