LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2007, 08:54 AM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
problem with getopt in C language


Hello all, I would like my C program.
I am using getopt, but it don't work as I would.
The program is:

....
....

main(int argc, char** argv) {

int c;
extern char* optarg;
extern int optind,opterr;

.....
.....

opterr = 0;

c = getopt(argc, argv, "ao");
switch (c) {
case 'a': printf(optarg);
break;
case 'o': printf(optarg);
break;
default: printf("opzione non valida");
break;
}

.....
.....
.....

I want use my program in this way:

#myprogram -a argument1 -o argument2

and I want that when

case 'a': then the program must print argument1

but it don't work!!

I want the same behaviour for -o option
How can I solve this problem?

Thanks everyone to help
 
Old 04-01-2007, 09:08 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You do not tell getopt the "a" and "o" options take arguments.

Pass the
Code:
"a:o:"
parameter to getopt instead of "ao".
 
Old 04-01-2007, 07:55 PM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Ok, it works, but it isn't yet perfect...
The code is the following:

opterr = 0;

while ((c = getopt(argc, argv, "a:")) != -1) {
switch (c) {
case 'a': printf(optarg);
break;
case 'o': printf(optarg);
break;
default: printf("opzione non valida");
break;
}
}

I try:

./myprogram -a argument1 -o argument2

and it print 'argument1' and 'argument2'

but I'd like can specify also more than one parameter in -a option, for example, I would use also this syntax:

./myprogram -a argument1 argument2 argument3 -o argument4

How can I solve this other problem?

Thank you in advance.
 
Old 04-01-2007, 11:23 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
AFAIK, getopt doesn't support this way of passing parameters.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt availability Bostonian Programming 2 12-26-2004 10:37 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 04:19 AM
getopt(3) Berhanie Programming 8 06-15-2004 09:50 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM
Using getopt in cplus UltimaGuy Programming 3 08-28-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration