1755087 Members
4281 Online
108829 Solutions
New Discussion юеВ

language and scripting

 
SOLVED
Go to solution
Soumya Poddar
Advisor

Re: language and scripting

yes
but using the function getopt itself

as using the getopt
for single argument we can get it using oparg

for the same thing using a list og arguments
can we get it using some standard syntax

plese reply sir
Bill Hassell
Honored Contributor

Re: language and scripting

> ./a.out -f toto tata -c arg1 arg2 arg3 arg4 ... -z
> you want to get the list of -c args?

The shell is separating each space-delimited string as a separate item for getopt. To supply multiple arguments to a single option, keep the arguments together. I like to use commas (more readable) or use quotes:

./a.out -f toto,tata -c arg1,arg2,arg3,arg4 ... -z

or

./a.out -f "toto tata" -c "arg1 arg2 arg3 arg4" ... -z


Bill Hassell, sysadmin
Soumya Poddar
Advisor

Re: language and scripting

in that case is my program need to parse them
or these list of arguments will be passed to me through some standard external variable
like optagr


plese reply
sir
Laurent Menase
Honored Contributor

Re: language and scripting

getopt stop to the first identified -x
now if optarg points to the first string after the identified option if there is a : in the description
optind points to the next arg

so to get a variable number of params you can do for instance:

char * v2;
char *allcparms[256];
int indexcparms=0;
......
case 'c':
allcparms[indexcparms++]=optarg;
while(v2=argv[optind])
{
if ( v2[0]=='-' ) break;
allcparms[indexcparms++]=v2;
optind++;
}
break;

so
./a.out -c 1 2 3 4 5 6 -f xx yy
will put 1 2 3 4 5 6 in allcparms[0..5]
./a.out -c 1 2 3 4 5 6
also
./a.out -c 1 2 3 4 5 6 -- xx yy
also

is it what you are trying to make?
Laurent Menase
Honored Contributor

Re: language and scripting

in one of my previous answer I wrote:
if (optarg[0]=='-')
in fact it is if (argv[optind][0]=='-')
Soumya Poddar
Advisor

Re: language and scripting

yes

but here in that case i parse those arguments using my programs

but i am asking that is there any standard function is there like getopt using which
i get my varriable number of arguments using some standard varriable like optarg in getopt

plese reply sir
Laurent Menase
Honored Contributor

Re: language and scripting

please give an example
I don't follow what you are trying to get and how?

are you trying to do it in script?
Soumya Poddar
Advisor

Re: language and scripting


like
in C

while((c=getopt(...,...,":abf:"))!=-1)
{
.....
.....
.....
}

now if my input is
./a.out -f Mandoc.doc
we can get it like

ifile=optagr

but if we want to give the input like
./a.out -f Mandoc.doc Relese.doc .....

then is there any standard function which will
give same oppurtunity access those argument list

like
arguments=optargarr;

where
arguments is declared like

char* arguments[];

and optagr declared as

extern char* optarg[];

i want to use it in simple C program

James R. Ferguson
Acclaimed Contributor

Re: language and scripting

Hi:

> but if we want to give the input like
./a.out -f Mandoc.doc Relese.doc .....

As Bill said, quote the argument and 'getopt()' will see it as one. I suspect that you come from Windows environments where spaces are common in filenames. Spaces (whitespace) in Unix separates arguments when the shell parses a command line/

Hence:

# ./a.out -f "Mandoc.doc Relese.doc"

...will leave you with *one* argument to the '-f' option.

Regards!

...JRF...
Soumya Poddar
Advisor

Re: language and scripting

ya
I am a fresher i am new in company and i am new in unix i came from windows enviourment

so hence
we have make our program in such a way that it can parse the arguments by it self

and there is no way to parse multiple arguments uning getopt() in c

ok sir thank you
for helping me
and shareing your knowladge with me

thank you