99 lines
2.4 KiB
C++
99 lines
2.4 KiB
C++
/* libopt++/src/tests/simple.cpp
|
|
*
|
|
* (c)2006, Laurence Withers. Released under the GNU GPL. See file
|
|
* COPYING for more information / terms of license.
|
|
*/
|
|
|
|
#include "opt"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
class Opt : private opt::Parser {
|
|
public:
|
|
Opt(int argc, const char* const* argv)
|
|
: quiet(false)
|
|
{
|
|
addOption(OptSummary, false, "print-summary", 0, "Displays a one-line summary of usage.");
|
|
addOption(OptHelp, false, "help", 'h', "Displays options and usage.");
|
|
addOption(OptPrint, true, "print", 'p', "Prints a string.");
|
|
addOption(OptPrint, true, "", 'P', "As -p.");
|
|
parse(argc, argv);
|
|
}
|
|
|
|
virtual ~Opt() throw() { }
|
|
|
|
void displayArgs()
|
|
{
|
|
if(quiet) return;
|
|
std::cout << "\nprogName = " << progName
|
|
<< "\nprogPath = " << progPath
|
|
<< "\nprogDir = " << progDir
|
|
<< "\n";
|
|
for(uint i = 0, end = args.size(); i != end; ++i) std::cout << "Argument: " << args[i] << "\n";
|
|
}
|
|
|
|
private:
|
|
enum { OptSummary, OptHelp, OptPrint };
|
|
bool quiet;
|
|
|
|
virtual void option(int result, const std::string& opt)
|
|
{
|
|
switch(result) {
|
|
case OptSummary:
|
|
std::cout << "A simple example program with a couple of options.\n";
|
|
quiet = true;
|
|
break;
|
|
|
|
case OptHelp:
|
|
std::cout << "Usage:\n\n "
|
|
<< progName << " [options] args\n\n";
|
|
showHelp(std::cout);
|
|
break;
|
|
|
|
default:
|
|
std::cerr << "Unrecognised option " << result << " (" << opt <<
|
|
") -- this is a program bug.\n";
|
|
}
|
|
}
|
|
|
|
virtual void option(int result, const std::string& opt, const std::string& arg)
|
|
{
|
|
switch(result) {
|
|
case OptPrint:
|
|
std::cout << opt << ": " << arg << "\n";
|
|
break;
|
|
|
|
default:
|
|
std::cerr << "Unrecognised option " << result << " (" << opt << ' ' << arg <<
|
|
") -- this is a program bug.\n";
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
int ret = 0;
|
|
try {
|
|
Opt o(argc, argv);
|
|
o.displayArgs();
|
|
}
|
|
catch(std::exception& e) {
|
|
std::cerr << e.what() << std::endl;
|
|
ret = 1;
|
|
}
|
|
catch(...) {
|
|
std::cerr << "Unknown exception caught." << std::endl;
|
|
ret = 1;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/* options for text editors
|
|
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
|
|
*/
|