in 'getopt()', how to handle multiple options in the 'optarg' string?

in 'getopt()', how to handle multiple options in the 'optarg' string?

Post by Harry Mangal » Sat, 31 Aug 1996 04:00:00



Hi All,

Pardon the possible idiocy of this question:

I'm trying to use the 'getopt' routine for handling command-line flags and
while most of it works like a charm, I need to have 1 flag that has 2
arguments:

progname -g 10 filename  yadda yadda yadda

optarg is declared as an 'extern char *', as per the man page for this,
and after the call to getopt, is supposed to point to the beginning of the
variable string.  I read this to mean that after the call, optarg should
point to the string "10 filename".

However, using an intermediate pointer 'tempstr' (because I can't examine
'optarg' directly for some reason (Is that a clue?)), I see with my
de* that after:
   .
   .
while ((c = getopt(argc, argv, "Lsvqhf:n:o:m:M:b:e:R:g:l:t:T:C:F:w:")) != EOF)
   .
   .
tempstr = optarg;

<now look at tempstr in memory, from the above example>

1 0 \0 f i l e n a m e \0

it looks like it's there but as 2 null-terminated strings instead of one,
so I can't read it in with a 'sscanf()' or suchlike.

Two questions:
1) Is this the correct action of getopt? (almost certainly 'yes' - I'm a
novice at this)

2) What is the usual way of extracting multiple options per flag using getopt?

Thanks in advance for any enlightenment.
Cheers
harry
--
Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC
Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598
--
Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598

 
 
 

in 'getopt()', how to handle multiple options in the 'optarg' string?

Post by Harry Mangal » Sat, 31 Aug 1996 04:00:00


Thanks for your comments - I've been able to fix it by direct manipulation
of the argv strings so that it does work the way originally stated, but a
few people have written to support your approach - and I think that I'll
have to correct the way I did it - it's probably not very portable at all.

Cheers
Harry



> The normal way to do this is to quote the arguments so they
> are treated as one by the shell. (Actually, it's the *only*
> way do do it and still use getopt). So:

>         progname -g "10 filename"  yadda yadda yadda

> So you actually have only one argument for -g, containing
> two words. If you can't live with that, I suspect you will
> have to respec the argument list. Hope this helps.
> --


--
Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598

 
 
 

in 'getopt()', how to handle multiple options in the 'optarg' string?

Post by Fletcher.Gl.. » Sat, 31 Aug 1996 04:00:00


See my embedded comments below:



Quote:>Hi All,

>Pardon the possible idiocy of this question:

>I'm trying to use the 'getopt' routine for handling command-line flags and
>while most of it works like a charm, I need to have 1 flag that has 2
>arguments:

>progname -g 10 filename  yadda yadda yadda

>optarg is declared as an 'extern char *', as per the man page for this,
>and after the call to getopt, is supposed to point to the beginning of the
>variable string.  I read this to mean that after the call, optarg should
>point to the string "10 filename".

This is incorrect.  Each space in your command line defines the boundary
between separate arguments.

Quote:

>However, using an intermediate pointer 'tempstr' (because I can't examine
>'optarg' directly for some reason (Is that a clue?)), I see with my
>de* that after:
>   .
>   .
>while ((c = getopt(argc, argv, "Lsvqhf:n:o:m:M:b:e:R:g:l:t:T:C:F:w:")) != EOF)
>   .
>   .
>tempstr = optarg;

><now look at tempstr in memory, from the above example>

>1 0 \0 f i l e n a m e \0

>it looks like it's there but as 2 null-terminated strings instead of one,
>so I can't read it in with a 'sscanf()' or suchlike.

You're right.  See my statement above.

Quote:

>Two questions:
>1) Is this the correct action of getopt? (almost certainly 'yes' - I'm a
>novice at this)

Yes.

Quote:

>2) What is the usual way of extracting multiple options per flag using getopt?

It is rather the problem of either doing your own parsing of argv[] or you
can command:
progname -g "10 filename another_filename"
to cause all of the separate arguments to be put into a single argument.

- Show quoted text -

Quote:

>Thanks in advance for any enlightenment.
>Cheers
>harry
>--
>Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC
>Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598
>--
>Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598

 
 
 

in 'getopt()', how to handle multiple options in the 'optarg' string?

Post by Kurt J Lan » Sat, 31 Aug 1996 04:00:00



>Hi All,
>Pardon the possible idiocy of this question:
>I'm trying to use the 'getopt' routine for handling command-line flags and
>while most of it works like a charm, I need to have 1 flag that has 2
>arguments:
>progname -g 10 filename  yadda yadda yadda

[ SNIP ]

The normal way to do this is to quote the arguments so they
are treated as one by the shell. (Actually, it's the *only*
way do do it and still use getopt). So:

        progname -g "10 filename"  yadda yadda yadda

So you actually have only one argument for -g, containing
two words. If you can't live with that, I suspect you will
have to respec the argument list. Hope this helps.

Quote:>Thanks in advance for any enlightenment.
>Cheers
>harry
>--
>Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC
>Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598
>--
>Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598

--
--