Adding another option to getopt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding another option to getopt
# 1  
Old 06-13-2013
Adding another option to getopt

I am trying to code for the addition of a new argument to the command line, the option D to a code that already has ABC (below). When I use make to compile it, it displays: invalid option --D. I did define the global d variable, as well as initialized it inside the main function of the C code. I don't know what else to try. Is there some trick to add additional arguments to getopt?

Code:
while ((op = getopt(argc, argv, "A:B:C:D")) != EOF) 
                switch (op) {
                	case    'A':    a = atoi(optarg); 
                                         break;
                        case    'B':    b= atof(optarg);
                                        break;
                        case    'C':    c= atof(optarg);
					break; //why break???
                        case    'D':    d = atoi(optarg);
					break;
                      default:  instruct();
                }

Thank you!

Last edited by Scott; 06-13-2013 at 06:42 PM.. Reason: Please use code tags
# 2  
Old 06-13-2013
You need a colon following the D in your optstring, if it is supposed to take an value (i.e. -D7 ) and not just be a flag (eg -D):

Code:
getopt(argc, argv, "A:B:C:D:")

btw break ends the case statement, if you don't have a break execution falls into the following case statement.
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 06-14-2013
Thank you, my bad. I added the ":" after D, but it still displays "invalid option". It should not be a problem with changing argc, right? The next part is this. Not sure if something else is happening to optind.

Code:
if ((argc - optind) != 1) {
                instruct();
        }

	strcpy(file_name, argv[optind])

# 4  
Old 06-14-2013
What OS are you using?

Exactly what is the command line that this code is parsing?

Is there any code in your script that modifies argc, argv, or any of the strings pointed to by argv[0] through argv[argc - 1] before you get to the code shown in your first post in this thread? (If at all possible, show us all of the code between your entry into main() and the while getopt() loop.)

Please show us the exact error message that you're getting from getopt (in CODE tags).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-14-2013
I am using Mac OS X.

Eventually I want to add all ABCD cases, but for now I am giving it just -D1 to parse.

There are no such operations, below is all the code starting from main until the while op=getopt loop. The instruct function prints instructions for the ABC cases, but not the added part for case D.
I am using the make compiler on this code together with 2 other programs, and giving the command line -D1 to the resulting file.
Code:
main(argc, argv)
	int	argc;
	char	**argv;
{
	FILE 	*fopen(), *ifp, *ofp;
	int	a=a0, d=d0;//a0 and d0 defined above
	float	b=b0, c=c0;//b0 and c0 defined above
	char	in_file[256], out_file[256];
	void instruct();
	if (argc == 1) 
		instruct();

The error message is:
Code:
invalid option -- 'D'

Thank you!
# 6  
Old 06-14-2013
Did you try ?

Code:
getopt(argc, argv, "ABCD:")

This User Gave Thanks to rajamadhavan For This Post:
# 7  
Old 06-14-2013
Thank you, just did, but still getting "invalid option --D".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] adding email option to KSH

Hi, I wanted to add a email option to this script. and was wondering if anyone could help me out. #!/bin/ksh echo "Finding hdisk" <DIR>/find-disk i=1 b=0 p=0 while ... (2 Replies)
Discussion started by: vpundit
2 Replies

2. Shell Programming and Scripting

getopt invalid option selection

What is the significance of the *) and ?) in the below code. while getopts a:b:c:he opt do case $opt in a) _name="$OPTARG";; b) _project="$OPTARG";; c) line="$OPTARG";; e) _cmd="XX";; h) Projects=1;; *) echo "$OPTARG is an invalid option"; my_exit 1;; ?)... (3 Replies)
Discussion started by: posix
3 Replies

3. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

4. UNIX for Dummies Questions & Answers

Using Getopt Option

Hi I need to use getopt option and I have no idea what it is or how to use it. I need to use it on this awk script: awk -F, -v cellid="$1" -v paramval="$2" -v oldfile="$3" -v newfile="$4" '$2==cellid{$3=newvalue}1' OFS="," $3 > $4 I tried reading up on it but I just confuse... (2 Replies)
Discussion started by: ladyAnne
2 Replies

5. Shell Programming and Scripting

--alternative option in getopt

Hi, i need to use --alternative option of getopt for ex . getopt -o a:c: --alternative pw: -- "$@" if i use like this, i am not getting any output.Please help me how to correct this.i need to have a combination of long and short options.But long options have to begin with - and not... (0 Replies)
Discussion started by: padmisri
0 Replies

6. Programming

probs adding linker option in g++

Hi all Im trying to compile a C++ program, however Im having issues trying to link a particular library to the compiling and linking statement. Here is what Im doing g++ mips4 -std=c++98 -g disk_reporter.cpp -o disk_reporter If I do this alone, the linker complains that library... (2 Replies)
Discussion started by: JamesGoh
2 Replies

7. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

8. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Shell Programming and Scripting

Adding an Option

I have a file that takes 2 or 3 arguments... ie: $argv(2), $argv(3) If I had to add another argument, I would be ok, but nooo.. that would be too easy for the new programmer. Before you go there - This is not a school problem, it's a real world problem.. If I wanted to add an option, for... (1 Reply)
Discussion started by: Brusimm
1 Replies
Login or Register to Ask a Question