00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include <cc++/common.h>
00069
00070 #include <iostream>
00071 #ifndef WIN32
00072 #include <cstdlib>
00073 #endif
00074
00075 #ifdef CCXX_NAMESPACES
00076 using namespace std;
00077 using namespace ost;
00078 #endif
00079
00080
00081
00082
00083
00084
00085
00086
00087 CommandOptionArg test_option1(
00088 "test_option1", "p", "This option takes an argument", true
00089 );
00090
00091 CommandOptionNoArg test_noarg(
00092 "test_noarg", "b", "This option does not take an argument"
00093 );
00094
00095 CommandOptionNoArg helparg(
00096 "help", "?", "Print help usage"
00097 );
00098
00099 CommandOptionCollect restoargs(
00100 0, 0, "Collect all the parameters", true
00101 );
00102
00103
00104
00105
00106
00107
00108 int Example_main( int argc, char ** argv )
00109 {
00110
00111
00112
00113
00114 CommandOptionParse * args = makeCommandOptionParse(
00115 argc, argv,
00116 "CommonC++ command like option interface. This is example\n"
00117 " code only."
00118 );
00119
00120
00121
00122 if ( helparg.numSet ) {
00123 cerr << args->printUsage();
00124 ::exit(0);
00125 }
00126
00127
00128 if ( args->argsHaveError() ) {
00129 cerr << args->printErrors();
00130 cerr << args->printUsage();
00131 ::exit(1);
00132 }
00133
00134
00135 args->performTask();
00136
00137
00138 for ( int i = 0; i < test_option1.numValue; i ++ ) {
00139 cerr << "test_option1 = " << test_option1.values[ i ] << endl;
00140 }
00141
00142
00143 for ( int i = 0; i < restoargs.numValue; i ++ ) {
00144 cerr << "restoargs " << i << " : " << restoargs.values[ i ] << endl;
00145 }
00146
00147 delete args;
00148
00149 return 0;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 CommandOption * TestList = 0;
00159
00160 extern CommandOptionRest test_restoargs;
00161
00162 #include <unistd.h>
00163 #include <sys/types.h>
00164 #include <sys/stat.h>
00165 #include <fcntl.h>
00166 #include <errno.h>
00167 #include <string.h>
00168 #include <stdlib.h>
00169 #include <sys/wait.h>
00170 #include <strstream>
00171
00172
00173
00174
00175
00176
00177 class file_option : public CommandOptionArg {
00178 public:
00179
00180
00181
00182 file_option(
00183 const char * in_option_name,
00184 const char * in_option_letter,
00185 const char * in_description,
00186 bool in_required = false,
00187 CommandOption ** pp_next = & defaultCommandOptionList
00188 )
00189 : CommandOptionArg(
00190 in_option_name,
00191 in_option_letter,
00192 in_description,
00193 in_required,
00194 pp_next
00195 )
00196 {
00197 }
00198
00199
00200
00201
00202 virtual void parseDone( CommandOptionParse * cop ) {
00203 if ( numValue ) {
00204 if ( ::access( values[ numValue - 1 ], R_OK ) ) {
00205 int errno_s = errno;
00206 strstream msg;
00207 msg << "Error: " << optionName << " '" << values[ numValue - 1 ];
00208 msg << "' : " << ::strerror( errno_s );
00209
00210 cop->registerError( msg.str() );
00211 }
00212 }
00213 }
00214
00215
00216
00217 int OpenFile() {
00218
00219 return ::open( values[ numValue - 1 ], O_RDONLY );
00220 }
00221
00222
00223
00224
00225 pid_t pid;
00226 virtual void performTask( CommandOptionParse * cop ) {
00227 pid = ::fork();
00228
00229 if ( pid ) {
00230 return;
00231 }
00232
00233 int fd = OpenFile();
00234 if ( fd < 0 ) {
00235 int errno_s = errno;
00236 cerr
00237 << "Error: '"
00238 << values[ numValue - 1 ]
00239 << "' : "
00240 << ::strerror( errno_s )
00241 ;
00242
00243 ::exit( 1 );
00244 }
00245 dup2(fd, 0);
00246 ::execvp( test_restoargs.values[0], (char**) test_restoargs.values );
00247 ::exit(1);
00248 }
00249
00250 ~file_option() {
00251 if ( pid <= 0 ) return;
00252 int status;
00253 ::wait(&status);
00254 }
00255 };
00256
00257
00258
00259
00260
00261
00262
00263
00264 file_option test_file(
00265 "test_file", "f", "Filename to read from", true, &TestList
00266 );
00267
00268 CommandOptionNoArg test_xnoarg(
00269 "test_xnoarg", "b", "This option does not take an argument", false, &TestList
00270 );
00271
00272 CommandOptionNoArg test_helparg(
00273 "help", "?", "Print help usage", false, &TestList
00274 );
00275
00276 CommandOptionRest test_restoargs(
00277 0, 0, "Command to be executed", true, &TestList
00278 );
00279
00280
00281
00282 int Test_main( int argc, char ** argv )
00283 {
00284 CommandOptionParse * args = makeCommandOptionParse(
00285 argc, argv,
00286 "Command line parser X test.\n"
00287 " This example is executed when the command ends in 'x'\n"
00288 " It shows how the -f parameter can be specialized.\n",
00289 TestList
00290 );
00291
00292
00293
00294 if ( test_helparg.numSet ) {
00295 cerr << args->printUsage();
00296 ::exit(0);
00297 }
00298
00299
00300 if ( args->argsHaveError() ) {
00301 cerr << args->printErrors();
00302 cerr << "Get help by --help\n";
00303 ::exit(1);
00304 }
00305
00306
00307 args->performTask();
00308
00309 for ( int i = 0; i < test_file.numValue; i ++ ) {
00310 cerr << "test_file = " << test_file.values[ i ] << endl;
00311 }
00312
00313 for ( int i = 0; i < test_restoargs.numValue; i ++ ) {
00314 cerr << "test_restoargs " << i << " : " << test_restoargs.values[ i ] << endl;
00315 }
00316
00317 delete args;
00318
00319 return 0;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 int main( int argc, char ** argv )
00335 {
00336
00337 int i = ::strlen( argv[ 0 ] );
00338
00339
00340 if ( argv[ 0 ][ i - 1 ] == 'x' ) {
00341 return Test_main( argc, argv );
00342 } else {
00343 return Example_main( argc, argv );
00344 }
00345
00346 }