Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Ex_Option.cpp

Go to the documentation of this file.
00001 #include <Option.h>
00002 
00003 using namespace std;
00004 using namespace rs::cmdline;
00005 using namespace rs::err;
00006 
00007 // every program has to create a ProgrammInfo
00008 static ProgramInfo demoInfo("Demo", 1, 0, "Demonstrate command line usage");
00009 
00010 static CmdLineArgument input("input", "name of the file to read");
00011 static OptionalCmdLineArgument output("output", "name of the file to write (write to console if omitted)");
00012 
00013 static CmdLineOption caseSensitive('c', "Be case sensitive");
00014 static CmdLineOption caseInsensitive('i', "Be case insensitive");
00015 
00016 static CmdLineArgumentOption verbose('v', "level", "Verbose output (level: 0=least 5=most)", "1");
00017 
00018 static CmdLineOption *case_options[] = { &caseSensitive, &caseInsensitive, 0 };
00019 
00020 int main(int argc, char *argv[])
00021 {
00022     try {
00023         demoInfo.init(argc, argv); // pass command line to demoInfo
00024         demoInfo.needOne(case_options); // we need -c or -i
00025         demoInfo.exclude(caseSensitive, caseInsensitive); // but we don't want both
00026 
00027         // now we are sure command line satisfies our needs..
00028 
00029         // input is always set
00030         cout << "input='" << input.get() << "'\n";
00031 
00032         if (output) { // test if output was given
00033             cout << "output='" << output.get() << "'\n";
00034         }
00035         else {
00036             cout << "output not specified\n";
00037         }
00038 
00039         if (caseSensitive) {
00040             cout << "Option -c was given on the command line\n";
00041         }
00042         if (caseInsensitive) {
00043             cout << "Option -i was given on the command line\n";
00044         }
00045         // verbose is always set (because it has a default value)
00046         cout << "verbose level is " << verbose.get() << "\n";
00047     }
00048     catch (Error& err) {
00049         err.print(cerr); // here the error is displayed (see output below)
00050     }
00051 
00052     return 0;
00053 }

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen