Unix Programming - getopt and help switch

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2005 > getopt and help switch





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author getopt and help switch
j0mbolar

2005-02-20, 6:20 pm

I have the following:

while((c = getopt(argc, argv, ":p:")) != -1) {
switch(c) {
case ':':
fprintf(stderr, "No argument\n");
usage();
case '?':
fprintf(stderr, "Invalid option\n");
usage();
case 'p': printf("Argument: %s\n", optarg);
break;
}
}

how can I implement a switch for "--help" ?
my man page does not shed any light on this.

also, is the leading colon in my opstring
portable among unices? or does it vary?

Heiner Steven

2005-02-20, 6:20 pm

j0mbolar wrote:
> I have the following:
>
> while((c = getopt(argc, argv, ":p:")) != -1) {
> switch(c) {
> case ':':
> fprintf(stderr, "No argument\n");
> usage();
> case '?':
> fprintf(stderr, "Invalid option\n");
> usage();
> case 'p': printf("Argument: %s\n", optarg);
> break;
> }
> }
>
> how can I implement a switch for "--help" ?
> my man page does not shed any light on this.


The standard getopt(3) cannot handle long option names. If you want
to support them, you should use getopt_long(3)

> also, is the leading colon in my opstring
> portable among unices? or does it vary?


A leasing colon suppresses error messages, and is portable
among different getopt() implementations.

Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
Nils O. Selåsdal

2005-02-20, 6:20 pm

getopt does not support these "long options". You can do it yourself, by e.g.
looking at all the args yourself and compare them to "--help" e.g.
Or you can use some other (less standard) arg parsing tool like e.g. getopt_long,
Nils O. Selåsdal

2005-02-20, 6:20 pm

getopt does not support these "long options". You can do it yourself, by e.g.
looking at all the args yourself and compare them to "--help" e.g.
Or you can use some other (less standard) arg parsing tool like e.g. getopt_long,
Heny Townsend

2005-02-20, 6:20 pm

j0mbolar wrote:
> how can I implement a switch for "--help" ?
> my man page does not shed any light on this.
>
> also, is the leading colon in my opstring
> portable among unices? or does it vary?


What I did was to go to NetBSD sources and grab their implementation of
getopt_long(). Or you could use the GNU version if the GPL isn't an
issue. Either way, a few minutes of porting work and you have a built-in
getopt which works the same on every platform.

--
Henry Townsend
James Antill

2005-02-21, 2:48 am

On Sun, 20 Feb 2005 18:16:12 -0500, Heny Townsend wrote:

> What I did was to go to NetBSD sources and grab their implementation of
> getopt_long(). Or you could use the GNU version if the GPL isn't an
> issue. Either way, a few minutes of porting work and you have a built-in
> getopt which works the same on every platform.


Minor nit, the GNU version is under the LGPL.

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

Heny Townsend

2005-02-21, 7:54 am

James Antill wrote:
> On Sun, 20 Feb 2005 18:16:12 -0500, Heny Townsend wrote:
>
>
>
>
> Minor nit, the GNU version is under the LGPL.


True, but that doesn't mean the GPL isn't an issue. The thing I ran into
was that non-GPL code is required to link dynamically with LGPL code;
static links essentially convert the LGPL to a GPL. This is reasonable
for the intended purpose (glibc et al) but if you want to extract getopt
and use it standalone, for which instructions are provided since it's a
common thing to do, you technically need to make a little one-file
shared object out of it or be in violation of the LGPL. I found this a
minoir irritant so I just switched over to the BSD variant.

Bottom line, *GPL code can use GNU getopt standalone. Other licenses may
be happier with the BSD version.

--
Henry Townsend
Ian Zimmerman

2005-02-21, 6:00 pm


Heny> What I did was to go to NetBSD sources and grab their
Heny> implementation of getopt_long(). Or you could use the GNU version
Heny> if the GPL isn't an issue. Either way, a few minutes of porting
Heny> work and you have a built-in getopt which works the same on every
Heny> platform.

If already must reuse glibc code, I see no reason not to go all the way
and get argp.

--
I wonder which is the best virus for unix and if I can write
a better one in Microsoft BASIC ?
Hans-Marc Olsen in comp.unix.programmer
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com