|
Packit |
a4aae4 |
/* Getopt for GNU.
|
|
Packit |
a4aae4 |
Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
|
|
Packit |
a4aae4 |
(Modified by Douglas C. Schmidt for use with GNU G++.)
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
This file is part of the GNU C++ Library. This library is free
|
|
Packit |
a4aae4 |
software; you can redistribute it and/or modify it under the terms of
|
|
Packit |
a4aae4 |
the GNU Library General Public License as published by the Free
|
|
Packit |
a4aae4 |
Software Foundation; either version 2 of the License, or (at your
|
|
Packit |
a4aae4 |
option) any later version. This library is distributed in the hope
|
|
Packit |
a4aae4 |
that it will be useful, but WITHOUT ANY WARRANTY; without even the
|
|
Packit |
a4aae4 |
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
Packit |
a4aae4 |
PURPOSE. See the GNU Library General Public License for more details.
|
|
Packit |
a4aae4 |
You should have received a copy of the GNU Library General Public
|
|
Packit |
a4aae4 |
License along with this library; if not, write to the Free Software
|
|
Packit |
a4aae4 |
Foundation 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA.
|
|
Packit |
a4aae4 |
*/
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* This version of `getopt' appears to the caller like standard Unix `getopt'
|
|
Packit |
a4aae4 |
but it behaves differently for the user, since it allows the user
|
|
Packit |
a4aae4 |
to intersperse the options with the other arguments.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
As `getopt' works, it permutes the elements of `argv' so that,
|
|
Packit |
a4aae4 |
when it is done, all the options precede everything else. Thus
|
|
Packit |
a4aae4 |
all application programs are extended to handle flexible argument order.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
|
|
Packit |
a4aae4 |
Then the behavior is completely standard.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
GNU application programs can use a third alternative mode in which
|
|
Packit |
a4aae4 |
they can distinguish the relative order of options and other arguments. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
#ifndef GetOpt_h
|
|
Packit |
a4aae4 |
#define GetOpt_h 1
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
// #include <stdio.h>
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
class GetOpt
|
|
Packit |
a4aae4 |
{
|
|
Packit |
a4aae4 |
private:
|
|
Packit |
a4aae4 |
/* The next char to be scanned in the option-element
|
|
Packit |
a4aae4 |
in which the last option character we returned was found.
|
|
Packit |
a4aae4 |
This allows us to pick up the scan where we left off.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
If this is zero, or a null string, it means resume the scan
|
|
Packit |
a4aae4 |
by advancing to the next ARGV-element. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
static char *nextchar;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* Describe how to deal with options that follow non-option ARGV-elements.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
UNSPECIFIED means the caller did not specify anything;
|
|
Packit |
a4aae4 |
the default is then REQUIRE_ORDER if the environment variable
|
|
Packit |
a4aae4 |
_OPTIONS_FIRST is defined, PERMUTE otherwise.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
REQUIRE_ORDER means don't recognize them as options.
|
|
Packit |
a4aae4 |
Stop option processing when the first non-option is seen.
|
|
Packit |
a4aae4 |
This is what Unix does.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
PERMUTE is the default. We permute the contents of `argv' as we scan,
|
|
Packit |
a4aae4 |
so that eventually all the options are at the end. This allows options
|
|
Packit |
a4aae4 |
to be given in any order, even with programs that were not written to
|
|
Packit |
a4aae4 |
expect this.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
RETURN_IN_ORDER is an option available to programs that were written
|
|
Packit |
a4aae4 |
to expect options and other ARGV-elements in any order and that care about
|
|
Packit |
a4aae4 |
the ordering of the two. We describe each non-option ARGV-element
|
|
Packit |
a4aae4 |
as if it were the argument of an option with character code zero.
|
|
Packit |
a4aae4 |
Using `-' as the first character of the list of option characters
|
|
Packit |
a4aae4 |
requests this mode of operation.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
The special argument `--' forces an end of option-scanning regardless
|
|
Packit |
a4aae4 |
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
|
|
Packit |
a4aae4 |
`--' can cause `getopt' to return EOF with `optind' != ARGC. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
enum OrderingEnum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER };
|
|
Packit |
a4aae4 |
OrderingEnum ordering;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* Handle permutation of arguments. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* Describe the part of ARGV that contains non-options that have
|
|
Packit |
a4aae4 |
been skipped. `first_nonopt' is the index in ARGV of the first of them;
|
|
Packit |
a4aae4 |
`last_nonopt' is the index after the last of them. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
static int first_nonopt;
|
|
Packit |
a4aae4 |
static int last_nonopt;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
void exchange (char **argv);
|
|
Packit |
a4aae4 |
public:
|
|
Packit |
a4aae4 |
/* For communication from `getopt' to the caller.
|
|
Packit |
a4aae4 |
When `getopt' finds an option that takes an argument,
|
|
Packit |
a4aae4 |
the argument value is returned here.
|
|
Packit |
a4aae4 |
Also, when `ordering' is RETURN_IN_ORDER,
|
|
Packit |
a4aae4 |
each non-option ARGV-element is returned here. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
char *optarg;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* Index in ARGV of the next element to be scanned.
|
|
Packit |
a4aae4 |
This is used for communication to and from the caller
|
|
Packit |
a4aae4 |
and for communication between successive calls to `getopt'.
|
|
Packit |
a4aae4 |
On entry to `getopt', zero means this is the first call; initialize.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
When `getopt' returns EOF, this is the index of the first of the
|
|
Packit |
a4aae4 |
non-option elements that the caller should itself scan.
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
Otherwise, `optind' communicates from one call to the next
|
|
Packit |
a4aae4 |
how much of ARGV has been scanned so far. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
int optind;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
/* Callers store zero here to inhibit the error message
|
|
Packit |
a4aae4 |
for unrecognized options. */
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
int opterr;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
int nargc;
|
|
Packit |
a4aae4 |
char **nargv;
|
|
Packit |
a4aae4 |
const char *noptstring;
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
GetOpt (int argc, char **argv, const char *optstring);
|
|
Packit |
a4aae4 |
int operator () (void);
|
|
Packit |
a4aae4 |
};
|
|
Packit |
a4aae4 |
|
|
Packit |
a4aae4 |
#endif
|