Parsing the command line arguments


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Parsing the command line arguments
# 1  
Old 12-17-2003
Parsing the command line arguments

Is there a way to get the command line arguments.

I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg

./a.out -x abc def

now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored in optarg.

Now while parsing I get the \0 in between so how can I get this sorted out.

Basically I want to enter the command like this

./a.out -k abc def -a junk -b junk1

now I want to store "abc" and "def" in some string. And parse further the options with -a and -b

I am using solaris 5.8

Regards
JK

Last edited by jayakhanna; 12-17-2003 at 08:31 AM..
# 2  
Old 12-17-2003
Use $1 to $9 to get up to 9 parameters... in your case, "-k" would be stored in $1, "abc" would be stored in $2, etc...
# 3  
Old 12-17-2003
So you want 'abc' and 'def' in the same string, why don't you simply put a quote around it, i.e.

./a.out -k 'abc def' -a junk -b junk2

then getopt(argc, argv, "k:aSmilie") will work ....

Smilie

Last edited by cbkihong; 12-17-2003 at 10:00 AM..
# 4  
Old 12-17-2003
I wan' t a system call and need not want to tokenize either, I just want to execute the way as normal program does.

./a.out -k abc def -x adf ghi

Regards
JK
# 5  
Old 12-17-2003
My earlier post wasn't what you wanted.. I don't think I understood your question at first.
Quote:
Originally posted by jayakhanna
I wan' t a system call and need not want to tokenize either, I just want to execute the way as normal program does.
A "normal" program executes the way cbkihong suggested. For example, if you use the find command to find a file called "some file" with a space in it, then you have to surround the name of that file with quotes or the command will fail. From the timestamp on the posts, I can see you most likely didn't see cbkihong's post before you posted...
# 6  
Old 12-17-2003
Yeah that is a workaround that will do,

But I am really wondering how to do the following

suppose I am using getopt function, in a C program and try to caputre the arguments with the -x option then how can I do that.

Like I have a command ls abc def, this is just an example now I want to wrap it up in some other function. So basically i write this
Code:
main()
{
      char ch;
      char buf[100];
      while ( (ch = getopt (x:a:b:)) != NULL) {
               switch (ch) {
                          case 'x' :
                                   strcpy (buf, optarg);

now here comes the problem how will I copy optarg to buf, because \0 will be there at the end of the first string so only the first string will be stored in buf

I tried to do this
Code:
                  for ( i = 0; i < 50; i++) {
                          dest[i ] = optarg[i ];
                  }

and then print dest[i ] its showing that optarg has all the arguments whatever we pass with -x, now how to determine how many arguments are passed with -x,

Here I am giving the i value randomly, how will I determine the exact i value.

Now this arguments needs to be passed to ls which i will call in some other part like system ("ls arguments) got from above which will stored in some variable

Hoping that you understood the real problem

Regards
JK

[i]added code tags and spaces to so rest of post wasn't italicized --oombera

Last edited by oombera; 02-21-2004 at 02:32 AM..
# 7  
Old 12-17-2003
Yes, I understand your problem. I don't know if any Unix variants have special extensions. Otherwise, from my interpretation of the getopt manpage I believe getopt doesn't expect multiple parameters to a single option, at all.

So, for example, if you invoke this as
./a.out -x abc def -a

Then argv[] is an array consisting of

-x
abc
def
-a

(not showing argv[0] above)

What getopt does is that it monotonically scans forward. It identifies '-x', associates 'abc' as optarg. It doesn't know what 'def' is, and throw it to the end. Then it identifies '-a', and so on. 'def' is left at the end and regarded as a non-option (the index of which you get with optind). Therefore, as getopt proceeds it permutes the order of arguments that appear in argv[].

I made this test program to illustrate the idea:
Code:
#include <stdio.h>
#include <getopt.h>

extern char *optarg;
extern int optind;

int main(int argc, char** argv) {
	int optchr; int i;
	while ((optchr = getopt(argc, argv, "x:a")) != -1) {
		printf("Option '%c' detected.\n", optchr);
		if (optarg) {
			printf("\tArgument: '%s'\n", optarg);
		}
	}
	printf("Other arguments:\n");
	for(i=optind; i<argc; i++) {
		printf("\t%s\n", argv[i]);
	}
	return 0;
}

./a.out -x abc def -a

gives

Code:
Option 'x' detected.
        Argument: 'abc'
Option 'a' detected.
Other arguments:
        def

I think conventional Unix tradition is that there is at most one argument for each option. I think this is because getopt allows options to be given in any order. So if you would like to break this convention you can always write your own routine to parse argv[]. Of course that will not be easy if your program has to accommodate a variety of options. Formally speaking, that requires parser construction knowledge (lexical analysis) that are beyond the scope of this forum.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

2. Programming

Parsing command line arguments in Python

Hi, I've a python script called aaa.py and passing an command line option " -a" to the script like, ./aaa.py -a & Inside the script if the -a option is given I do some operation if not something else. code looks like ./aaa.py -a . . if options.a ---some operation--- if not options.a... (1 Reply)
Discussion started by: testin
1 Replies

3. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

4. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

5. Shell Programming and Scripting

Maximum command line arguments

Hi, Can anyone please help me to know what is the maximum number of command line arguments that we can pass in unix shell script? Thanks in advance, Punitha.S (2 Replies)
Discussion started by: puni
2 Replies

6. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

7. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

8. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

10. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies
Login or Register to Ask a Question