Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getopt()

167 views
Skip to first unread message

Anson Ng

unread,
Nov 26, 2000, 2:05:30 AM11/26/00
to
There isn't getopt() in Borland C++Builder??
I cannot find it in the header files nor the online help.
Is there any alternatives for deploying command line arguments??

--
Anson

Thomas Maeder

unread,
Nov 26, 2000, 3:00:00 AM11/26/00
to

If command-line programs, the main() function is called at startup which can
have the following signature:

int main(int argc, char **argv)
{
// ...
}

argv is an array of 0-terminated character strings containing the
command-line arguments, argc contains the number of elements of argv.

In non-command-line programs, similar functions are called at startup;
please consult the documentation.

Anson Ng

unread,
Nov 26, 2000, 3:00:00 AM11/26/00
to
I know these, but getopt() is a very handful function for deplying
options and option arguments like

cmd -a -o ofilename files...
or
cmd -ab -o ofilename files...

but it's okay, I just wrote my own verion, just wondering if it's
provided in BC++. Because it's present in every UNIX distributions's C
Library.

--
Anson


Thomas Maeder wrote:

Helmut Giese

unread,
Nov 26, 2000, 3:00:00 AM11/26/00
to

Not any more - sorry to say. But it used to be included up to Borland
C++ 5.0. Look in ...\examples\dos.
Borland improved it - in a way - to handle the possibility that a user
(program) had changed the so called 'switch character'. If I remember
well, this caused problems so just throw this part out.
HTH,
Helmut

Anson Ng

unread,
Nov 26, 2000, 3:00:00 AM11/26/00
to
Can you explain a bit about the "switch character" thing??

--
Anson


Helmut Giese wrote:

> On Sun, 26 Nov 2000 02:05:30 -0500, Anson Ng <an...@mail.com> wrote:
>
>

Helmut Giese

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to

>Can you explain a bit about the "switch character" thing??
>
>--
Hello Anson,
in MS DOS the character that is used to identify an option is usually
'/': E.g. you say "dir /p *.*" to get a page wise directory listing
instead of dumping it all at once.
But they introduced the option to change this character (the 'switch
character'). Maybe people coming from Unix would change it to '-' so
that they could keep their habit of defining options.

So Borland modified option.c in such a way as not to presume any
specific character but to ask the operating system instead.
This asking, I'm afraid, will not work.
But no big deal:
- They use a variable 'SW' to hold the result of asking the OS.
- Just remove the 'asking part' and instead put the option character
that you would like to use into SW ('/' or '-' or whatever) - or use
#define SW '/'
which probably gets you back to the beginning of it all.

HTH
Helmut.

Wayne A. King

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
On Sun, 26 Nov 2000 09:17:55 -0500, Anson Ng <an...@mail.com> wrote:

> There isn't getopt() in Borland C++Builder?

> Is there any alternatives for deploying command line arguments?

For an alternative, you can go to http://www.snippets.org and get:

Functions to get command line options
================================
File O/S Description
------------ --- ---------------------------
Getopts.Man n/a Manual for using getopts()
Getopts.H any Header for Getopts.C
Getopts.C any Scan command line for switches, filenames, response files
Getoptst.C any Test program to exercise getopts()
Palnfilt.C any Palindrome filter, demo for Getopts.C
Getoptsl.Man n/a Manual for using getopts_lite()
Getoptsl.H any "Getopts Lite" - Smaller & simpler than full GETOPTS
Getoptsl.C any

--
Wayne A. King
(ba...@torfree.net, wayne...@ablelink.org,
wak...@idirect.com, Wayne_...@compuserve.com)

Rich Webb

unread,
Nov 28, 2000, 7:46:18 PM11/28/00
to

This is the public domain version I've been using for years. I think
it's small enough to post; besides, others might like it...


/* ::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]:: */
#ifndef LINT
static char sccsid[]="::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]::";
#endif

/*
* Here's something you've all been waiting for: the AT&T public domain
* source for getopt(3). It is the code which was given out at the 1985
* UNIFORUM conference in Dallas. I obtained it by electronic mail
* directly from AT&T. The people there assure me that it is indeed
* in the public domain.
*
* There is no manual page. That is because the one they gave out at
* UNIFORUM was slightly different from the current System V Release 2
* manual page. The difference apparently involved a note about the
* famous rules 5 and 6, recommending using white space between an option
* and its first argument, and not grouping options that have arguments.
* Getopt itself is currently lenient about both of these things White
* space is allowed, but not mandatory, and the last option in a group can
* have an argument. That particular version of the man page evidently
* has no official existence, and my source at AT&T did not send a copy.
* The current SVR2 man page reflects the actual behavor of this getopt.
* However, I am not about to post a copy of anything licensed by AT&T.
*/

/*
Minor modifications by Rahul Dhesi 1989/03/06
*/

#include <string.h>
#include <stdio.h>

/* Avoid possible compiler warning if we simply redefine NULL or EOF */
#define XNULL 0
#define XEOF (-1)

#define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}

int opterr = 1;
int optind = 1;
int optopt;
char *optarg;

int
getopt(int argc, char **argv, char *opts)
{
static int sp = 1;
register int c;
register char *cp;

if(sp == 1)
if(optind >= argc ||
argv[optind][0] != '-' || argv[optind][1] == '\0')
return(XEOF);
else if(strcmp(argv[optind], "--") == XNULL) {
optind++;
return(XEOF);
}
optopt = c = argv[optind][sp];
if(c == ':' || (cp=strchr(opts, c)) == XNULL) {
ERR(": illegal option -- ", c);
if(argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return('?');
}
if(*++cp == ':') {
if(argv[optind][sp+1] != '\0')
optarg = &argv[optind++][sp+1];
else if(++optind >= argc) {
ERR(": option requires an argument -- ", c);
sp = 1;
return('?');
} else
optarg = argv[optind++];
sp = 1;
} else {
if(argv[optind][++sp] == '\0') {
sp = 1;
optind++;
}
optarg = XNULL;
}
return(c);
}

--
Rich Webb Norfolk, VA

0 new messages