LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 06-06-2008, 07:54 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
flag problem in c


I am having a problem which I am unable to resolve. My programme can be executed from the command line with several options, some with an additional argument, some not:

Code:
...
int defaultflag = 0;
int helpflag = 0;
int orderflag =0;
int tmaxflag = 0;
int stepflag = 0;
char *stepvalue = NULL;
char *tmaxvalue = NULL;
char *ordervalue = NULL;
int flagindex;
int flags;
opterr = 0;

while ((flags = getopt (argc, argv, "adhs:t:")) != -1)
	switch (flags)
	{
	case 'd':
	defaultflag = 1;
	break;
	case 'o':
	orderflag = 1;
	ordervalue = optarg;
	break;
	case 's':
	stepflag = 1;
	stepvalue = optarg;
	break;
	case 't':
	tmaxflag = 1;
	tmaxvalue = optarg;
	break;
	case 'h':
	helpflag = 1;
	break;
	case '?':
	if (optopt == 't' || optopt == 's' || optopt == 'o')
	fprintf (stderr, "Option -%c requires a numerical argument.\n", optopt);
	else if (isprint (optopt))
	fprintf (stderr, "Unknown option `-%c'.\n", optopt);
	else
	fprintf (stderr,
			"Unknown option character `\\x%x'.\n",
			optopt);
	return 1;
	default:
	exit(1);
	}
for (flagindex = optind; flagindex < argc; flagindex++)
	{
	printf ("Non-option argument %s\n", argv[flagindex]);
	exit(1);
	}
...
The ones without additional arguments are d and h and the ones with arguments are s,t,o, where the values one has to give are used somewhere else in the programme. Now, the problem is that if I execute the programme it works fine except for the -o flag, which always gives me the line "-o requires a numerical argument", even if I give it one. What am I missing here?
 
Old 06-06-2008, 09:25 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
getopt (argc, argv, "adhs:t:")
change to
getopt (argc, argv, "adhs:t:o:")
You evidently added the -o option to your code, but neglected to add it to the options recognized by getopt(). You don't seem to have an argument handler for '-a', although it is in the list of valid args.
--- rod.
 
Old 06-06-2008, 09:46 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
You're absolutely right! So stupid of me to forget the list... (that's the reason why there still is a valid -a option - it used to be there in the first version). Thanks!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
What is this Bootable Flag ? lazylogic Debian 5 02-21-2012 06:20 PM
Flag this message I have a problem with gdbm and ndbm bangusilo Linux - Newbie 2 04-29-2008 01:02 PM
About growisofs flag satimis Linux - General 1 01-14-2008 08:21 PM
bootable flag noonmid27 Linux - Hardware 1 12-21-2006 09:37 AM
bootable flag? alaios Debian 1 04-29-2004 11:37 PM

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

All times are GMT -5. The time now is 03:03 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration