Skip to content

Commit

Permalink
introduced program options using boost library, also checking existen…
Browse files Browse the repository at this point in the history
…ce of cfg files
  • Loading branch information
Senoner Samuel committed Feb 2, 2017
1 parent 0c6f70e commit 9908ea7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 25 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ SUBDIRLIST(3DTESTS ${3D_DIRECTORY})
foreach(TEST ${1DTESTS})
add_test(NAME "1D.${TEST}"
WORKING_DIRECTORY ${1D_DIRECTORY}/${TEST}
COMMAND $<TARGET_FILE:geotop> geotops.inpts )
COMMAND $<TARGET_FILE:geotop> . )

#testing with test_runner
add_test(NAME "1D.test_runner.${TEST}"
Expand All @@ -425,7 +425,7 @@ ENDFOREACH()
foreach(TEST ${3DTESTS})
add_test(NAME "3D.${TEST}"
WORKING_DIRECTORY ${3D_DIRECTORY}/${TEST}
COMMAND $<TARGET_FILE:geotop> geotops.inpts )
COMMAND $<TARGET_FILE:geotop> . )

#testing with test_runner
add_test(NAME "3D.test_runner.${TEST}"
Expand Down
1 change: 1 addition & 0 deletions src/geotop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ INSTALL(TARGETS ${GEOTOP_LIBRARIES}
)

TARGET_LINK_LIBRARIES(geotop
${Boost_PROGRAM_OPTIONS_LIBRARY}
${GEOTOP_LIBRARIES}
)
SET_TARGET_PROPERTIES(geotop PROPERTIES
Expand Down
135 changes: 112 additions & 23 deletions src/geotop/geotop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
#endif


#include <boost/filesystem.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/value_semantic.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/version.hpp>

using namespace std;

void time_loop(AllData *A, mio::IOManager& iomanager);
Expand All @@ -77,9 +86,89 @@ int main(int argc,char *argv[]){
AllData *adt;
FILE *f;

geotop::logger::GlobalLogger* lg = geotop::logger::GlobalLogger::getInstance();

boost::program_options::options_description usage("Options"); // name of help function
usage.add_options() //detailed specification of command line interface
("version,v", "print version string")
("help,h", "this help") // this option return boolean variable and is used to print command line help
("workdir,w", boost::program_options::value<std::string>(), "the working directory of the simulation");

boost::program_options::positional_options_description positionalOptions;
positionalOptions.add("workdir", -1);

// map for options/value
boost::program_options::variables_map vm;

// option parsing statements.
try{
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(usage).positional(positionalOptions).run(), vm);
boost::program_options::notify(vm);
}
catch ( boost::program_options::unknown_option &u ){
std::cerr << "Option parsing error: " << u.what() << ": please use only valid flags"<< std::endl;
return 10 ;
}
catch ( boost::program_options::invalid_command_line_syntax &u ){
std::cerr << "Option parsing error: " << u.what() << ": please specificy a valid value for this option "<< std::endl;
return 11 ;
}
catch ( boost::program_options::error &u ){
std::cout << "Option parsing error: " << u.what() << std::endl;
return 12 ;
}

// if no options or --help or -h print help and exit.
if(argc <= 1 || vm.count("help"))
{
std::cout << "Geotop 2.1 usage" << std::endl
<< usage << std::endl;
return 13 ;
}

if (vm.count("version"))
{
std::cout << "This is Getop version 2.1" << std::endl;
return 0;
}

std::string lDataPath ;
std::string lFilePath ;
string cfgfile = "io_it.ini";
if (vm.count("workdir"))
{
lDataPath = vm["workdir"].as<std::string>();
} else {
std::cerr << "The workdir must be specified" << std::endl ;
return 20 ;
}

if(not boost::filesystem::is_directory(lDataPath))
{
std::cerr << "Workdir " << lDataPath << " is not a directory or does not exist" << std::endl;
return 21;
}

lDataPath = boost::filesystem::canonical(lDataPath).c_str();
geotop::common::Variables::WORKING_DIRECTORY = lDataPath;
lFilePath = geotop::common::Variables::WORKING_DIRECTORY + "/" + program_name;
cfgfile = geotop::common::Variables::WORKING_DIRECTORY + "/" + cfgfile;
boost::filesystem::path lInputFilePath (lFilePath.c_str());

if(not boost::filesystem::exists(lInputFilePath))
{
std::cerr << "geotop cfg file not found: " << lInputFilePath.string() << std::endl ;
return 21 ;
}

if(not boost::filesystem::exists(cfgfile))
{
std::cerr << "meteoio cfg file not found: " << cfgfile << std::endl ;
return 21 ;
}


geotop::logger::GlobalLogger* lg = geotop::logger::GlobalLogger::getInstance();


/* ANTONIO: This is just crazy. We have *two* incompatible way of
calling geotop, and part of the command line parsing is done
Expand All @@ -92,31 +181,31 @@ int main(int argc,char *argv[]){
ncgt_open_from_option_string().
*/

std::string lDataPath ;
if (argc >= 2)
{
lDataPath = argv[1] ;
} else {
lDataPath = get_workingdirectory() ;
}
// std::string lDataPath ;
// if (argc >= 2)
// {
// lDataPath = argv[1] ;
// } else {
// lDataPath = get_workingdirectory() ;
// }

if(lDataPath == "" )
{
std::cerr << "Error: data path is empty" << std::endl ;
exit (200) ;
}
// if(lDataPath == "" )
// {
// std::cerr << "Error: data path is empty" << std::endl ;
// exit (200) ;
// }

chdir(lDataPath.c_str());
char lCWD[8192] ;
char * lCwdStr = getcwd(lCWD, sizeof(lCWD));
if (lCWD == NULL)
{
std::cerr << "Error: unable to get the current path: " << strerror(errno) << std::endl ;
exit (201) ;
}
std::string lFullPath(lCwdStr) ;
geotop::common::Variables::WORKING_DIRECTORY = lFullPath ;
cfgfile = geotop::common::Variables::WORKING_DIRECTORY + "/" + cfgfile;
// char lCWD[8192] ;
// char * lCwdStr = getcwd(lCWD, sizeof(lCWD));
// if (lCWD == NULL)
// {
// std::cerr << "Error: unable to get the current path: " << strerror(errno) << std::endl ;
// exit (201) ;
// }
// std::string lFullPath(lCwdStr) ;
// geotop::common::Variables::WORKING_DIRECTORY = lFullPath ;
// cfgfile = geotop::common::Variables::WORKING_DIRECTORY + "/" + cfgfile;

mio::Config cfg(cfgfile);
cfg.addKey("GRID2DPATH", "Input", "");
Expand Down

0 comments on commit 9908ea7

Please sign in to comment.