// CommandLine_t.cxx #include "dataset_util/CommandLine.h" #include #include #include "dataset_util/XmlElement.h" using std::string; using std::cout; using std::endl; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } void show(const char* const argv[]) { int i = 0; while ( argv[i] != 0 ) { cout << i << " " << argv[i] << endl; ++i; } } int CommandLine_t() { const char* cexe = "myexe"; const char* carg1 = "arg1"; const char* carg2 = "arg2"; const char* carg3 = "arg3"; msg("Invalid command line"); CommandLine com0; cout << com0 << endl; assert( ! com0.is_valid() ); msg("Create command line directly"); string exe = cexe; CommandLine::ArgumentList args; args.push_back(carg1); args.push_back(carg2); args.push_back(carg3); CommandLine com1(exe, args); cout << com1 << endl; assert( com1.is_valid() ); assert( com1.name() == exe ); assert( com1.cname() == exe ); assert( com1.arguments() == args ); msg("Check argv"); show(com1.argv()); msg("Create command line from argv"); const char* argv[10]; argv[0] = cexe; argv[1] = carg1; argv[2] = carg2; argv[3] = carg3; CommandLine com2(4, argv); cout << com2 << endl; assert( com2.is_valid() ); assert( com2.name() == exe ); assert( com2.arguments() == args ); assert( com2 == com1 ); msg("Create command line from null-terminated argv"); argv[4] = 0; CommandLine com3(argv); cout << com3 << endl; assert( com3.is_valid() ); assert( com3 == com1 ); msg("Create command from returned argv"); const char *const * newargv = com1.argv(); CommandLine com4(newargv); cout << com4 << endl; assert( com4.is_valid() ); assert( com4 == com1 ); msg("Create command from returned unsafe argv"); char *const * unsafe_argv = com1.unsafe_argv(); CommandLine com5(unsafe_argv); cout << com5 << endl; assert( com5.is_valid() ); assert( com5 == com1 ); assert( com5.arguments() == args ); msg("Write XML"); const XmlElement* pxcom = com1.xml(); assert( pxcom != 0 ); cout << *pxcom << endl; msg("Read from XML"); CommandLine com6(*pxcom); cout << com6 << endl; assert( com6.is_valid() ); assert( com6 == com1 ); assert( com6.arguments() == args ); msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return CommandLine_t(); } #endif