|
Packit |
01d647 |
#ifndef GETOPT_H
|
|
Packit |
01d647 |
#define GETOPT_H
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
#include <ctype.h>
|
|
Packit |
01d647 |
#include <stdio.h>
|
|
Packit |
01d647 |
#include <string>
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
namespace Util {
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
extern int optind ;
|
|
Packit |
01d647 |
extern int opterr ;
|
|
Packit |
01d647 |
extern int optopt ;
|
|
Packit |
01d647 |
extern int optpos ;
|
|
Packit |
01d647 |
extern const char* optarg;
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
int getopt(int argc, char * const argv[], const char *optstring);
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
// *********************************************************************
|
|
Packit |
01d647 |
// class definitions
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
/*!
|
|
Packit |
01d647 |
@brief Parse the command line options of a program.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
A wrapper around the POSIX %getopt(3) function. Parses the command line
|
|
Packit |
01d647 |
options and passes each option to virtual option(). A derived class
|
|
Packit |
01d647 |
implements this method to handle options as needed. Similarly,
|
|
Packit |
01d647 |
remaining non-option parameters are passed to the virtual nonoption()
|
|
Packit |
01d647 |
method.
|
|
Packit |
01d647 |
*/
|
|
Packit |
01d647 |
class Getopt {
|
|
Packit |
01d647 |
public:
|
|
Packit |
01d647 |
//! Default constructor.
|
|
Packit |
01d647 |
Getopt();
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
//! Destructor.
|
|
Packit |
01d647 |
virtual ~Getopt();
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
/*!
|
|
Packit |
01d647 |
@brief Parse command line arguments.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
Parses the command line arguments. Calls option() with the
|
|
Packit |
01d647 |
character value of the option and its argument (if any) for each
|
|
Packit |
01d647 |
recognized option and with ':' or '?' for unrecognized options.
|
|
Packit |
01d647 |
See the manual pages for %getopt(3) for details. In addition,
|
|
Packit |
01d647 |
nonoption() is invoked for each remaining non-option parameter on
|
|
Packit |
01d647 |
the command line.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@param argc Argument count as passed to main() on program invocation.
|
|
Packit |
01d647 |
@param argv Argument array as passed to main() on program invocation.
|
|
Packit |
01d647 |
@param optstring String containing the legitimate option characters.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@return Number of errors (the sum of the return values from option()
|
|
Packit |
01d647 |
and nonoption()).
|
|
Packit |
01d647 |
*/
|
|
Packit |
01d647 |
int getopt(int argc, char* const argv[], const std::string& optstring);
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
/*!
|
|
Packit |
01d647 |
@brief Callback used by getopt() to pass on each option and its
|
|
Packit |
01d647 |
argument (if any).
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
Implement this method in a derived class to handle the options as
|
|
Packit |
01d647 |
needed. See the manual pages for %getopt(3) for further details, in
|
|
Packit |
01d647 |
particular, the semantics of optarg and optopt.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@param opt Value of the option character as returned by %getopt(3).
|
|
Packit |
01d647 |
@param optarg The corresponding option argument.
|
|
Packit |
01d647 |
@param optopt The actual option character in case of an unrecognized
|
|
Packit |
01d647 |
option or a missing option argument (opt is '?' or ':').
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@return 0 if successful, 1 in case of an error.
|
|
Packit |
01d647 |
*/
|
|
Packit |
01d647 |
virtual int option(int opt, const std::string& optarg, int optopt) = 0;
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
/*!
|
|
Packit |
01d647 |
@brief Callback used by getopt() to pass on each non-option parameter
|
|
Packit |
01d647 |
found on the command line.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
Implement this method in a derived class to handle the non-option
|
|
Packit |
01d647 |
parameters as needed. The default implementation ignores all non-option
|
|
Packit |
01d647 |
parameters.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@param argv The non-option parameter from the command line.
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
@return 0 if successful, 1 in case of an error.
|
|
Packit |
01d647 |
*/
|
|
Packit |
01d647 |
virtual int nonoption(const std::string& argv);
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
//! Program name (argv[0])
|
|
Packit |
01d647 |
const std::string& progname() const { return progname_; }
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
//! Total number of errors returned by calls to option()
|
|
Packit |
01d647 |
int errcnt() const { return errcnt_; }
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
private:
|
|
Packit |
01d647 |
std::string progname_;
|
|
Packit |
01d647 |
int errcnt_;
|
|
Packit |
01d647 |
};
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
}; // namespace Util
|
|
Packit |
01d647 |
|
|
Packit |
01d647 |
#endif
|