#include <iostream>
#ifndef WIN32
#include <cstdlib>
#endif
#ifdef CCXX_NAMESPACES
using namespace std;
#endif
"test_option1", "p", "This option takes an argument", true
);
"test_noarg", "b", "This option does not take an argument"
);
"help", "?", "Print help usage"
);
0, 0, "Collect all the parameters", true
);
int Example_main( int argc, char ** argv )
{
argc, argv,
"CommonC++ command like option interface. This is example\n"
" code only."
);
if ( helparg.numSet ) {
::exit(0);
}
::exit(1);
}
for ( int i = 0; i < test_option1.numValue; i ++ ) {
cerr << "test_option1 = " << test_option1.values[ i ] << endl;
}
for ( int i = 0; i < restoargs.numValue; i ++ ) {
cerr << "restoargs " << i << " : " << restoargs.values[ i ] << endl;
}
delete args;
return 0;
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <strstream>
public:
file_option(
const char * in_option_name,
const char * in_option_letter,
const char * in_description,
bool in_required = false,
)
in_option_name,
in_option_letter,
in_description,
in_required,
pp_next
)
{
}
if ( numValue ) {
if ( ::access( values[ numValue - 1 ], R_OK ) ) {
int errno_s = errno;
strstream msg;
msg << "Error: " << optionName << " '" << values[ numValue - 1 ];
msg << "' : " << ::strerror( errno_s );
}
}
}
int OpenFile() {
return ::open( values[ numValue - 1 ], O_RDONLY );
}
pid_t pid;
pid = ::fork();
if ( pid ) {
return;
}
int fd = OpenFile();
if ( fd < 0 ) {
int errno_s = errno;
cerr
<< "Error: '"
<< values[ numValue - 1 ]
<< "' : "
<< ::strerror( errno_s )
;
::exit( 1 );
}
dup2(fd, 0);
::execvp( test_restoargs.
values[0], (
char**) test_restoargs.
values );
::exit(1);
}
~file_option() {
if ( pid <= 0 ) return;
int status;
}
};
file_option test_file(
"test_file", "f", "Filename to read from", true, &TestList
);
"test_xnoarg", "b", "This option does not take an argument", false, &TestList
);
"help", "?", "Print help usage", false, &TestList
);
0, 0, "Command to be executed", true, &TestList
);
int Test_main( int argc, char ** argv )
{
argc, argv,
"Command line parser X test.\n"
" This example is executed when the command ends in 'x'\n"
" It shows how the -f parameter can be specialized.\n",
TestList
);
if ( test_helparg.numSet ) {
::exit(0);
}
cerr << "Get help by --help\n";
::exit(1);
}
for ( int i = 0; i < test_file.numValue; i ++ ) {
cerr << "test_file = " << test_file.values[ i ] << endl;
}
for (
int i = 0; i < test_restoargs.
numValue; i ++ ) {
cerr <<
"test_restoargs " << i <<
" : " << test_restoargs.
values[ i ] << endl;
}
delete args;
return 0;
}
int main( int argc, char ** argv )
{
int i = ::strlen( argv[ 0 ] );
if ( argv[ 0 ][ i - 1 ] == 'x' ) {
return Test_main( argc, argv );
} else {
return Example_main( argc, argv );
}
}