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