Commit 926256cb authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[CHANGE] - openwalnut now has useful --version and --help output. Logger is...

[CHANGE] - openwalnut now has useful --version and --help output. Logger is started AFTER command line parsing. The help output is somewhat standard-conform now. Tools like help2man can create manpages out of this
parent 87f4a0db
......@@ -25,6 +25,8 @@
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include "core/common/WSegmentationFault.h"
#include "core/common/WLogger.h"
#include "core/common/WLogStream.h"
......@@ -46,13 +48,13 @@
* For more information see http://www.openwalnut.org/copying
*/
/**
* The main routine starting up the whole application.
*/
int main( int argc, char** argv )
void printVersion()
{
std::cout << "OpenWalnut ( http://www.openwalnut.org )" << std::endl <<
"Build from: " << W_VERSION << std::endl << std::endl <<
std::cout << "OpenWalnut -- Version: " << W_VERSION << " ( http://www.openwalnut.org )"
<< std::endl
<< std::endl;
std::cout <<
"Copyright 2009-2010 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS\n"
"For more information see http://www.openwalnut.org/copying\n"
"This program comes with ABSOLUTELY NO WARRANTY.\n"
......@@ -62,6 +64,70 @@ int main( int argc, char** argv )
"along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>." << std::endl;
std::cout << std::endl; // Create new line after message for clarity.
std::cout << "Written by the OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS." << std::endl;
}
/**
* The main routine starting up the whole application.
*/
int main( int argc, char** argv )
{
// where to write the by default?
std::string logFile = "openwalnut.log";
// process user parameter
namespace po = boost::program_options; // since the namespace is far to big we use a shortcut here
po::options_description desc( "Options:" );
desc.add_options()
( "help,h", "Prints this help message" )
( "version,v", "Prints the version information" )
( "log,l", po::value< std::string >(), ( std::string( "The log-file to use. If not specified, \"" ) + logFile +
std::string( "\" is used in the current directory." ) ).c_str() )
( "project,p", po::value< std::string >(), "Project file to be loaded on startup." )
( "input,i", po::value< std::vector< std::string > >(), "Input data files that should be loaded automatically" );
po::positional_options_description p;
p.add( "input", -1 );
boost::program_options::variables_map optionsMap;
try
{
po::store( po::command_line_parser( argc, argv ).options( desc ).positional( p ).run(), optionsMap );
}
catch( const po::error &e )
{
std::cerr << e.what() << std::endl;
return false;
}
po::notify( optionsMap );
// print usage information if command line asks for help.
if( optionsMap.count( "help" ) )
{
// NOTE: if you modify this, check that help2man still works properly! (http://www.gnu.org/software/help2man)
std::cout << "OpenWalnut is an highly expansible visualization system with focus on brain- and neurological data." << std::endl
<< std::endl
<< "Usage: openwalnut [OPTION]... [FILE]..." << std::endl
<< std::endl
<< desc << std::endl
<< std::endl
<< "Examples:" << std::endl
<< " openwalnut\t\t\t\tStartup OpenWalnut." << std::endl
<< " openwalnut -p myproject.owp\t\tStart OpenWalnut and load the project." << std::endl
<< " openwalnut t1.nii.gz fibers.fib\tStart OpenWalnut and load the two datasets." << std::endl
<< std::endl;
return 0;
}
else if( optionsMap.count( "version" ) )
{
printVersion();
return 0;
}
printVersion();
// install signal handler as early as possible
WSegmentationFault::installSignalHandler();
......@@ -70,10 +136,10 @@ int main( int argc, char** argv )
// add a crash-log.
// TODO(all): we should think about the location of this file.
std::ofstream crashLogFile( "openwalnut.log" );
std::ofstream crashLogFile( logFile.c_str() );
if( !crashLogFile.is_open() )
{
wlog::warn( "Walnut" ) << "Could not open openwalnut.log for writing. You will have no log-file.";
wlog::warn( "Walnut" ) << "Could not open \"" << logFile << "\" for writing. You will have no log-file.";
}
else
{
......@@ -88,7 +154,7 @@ int main( int argc, char** argv )
// initialize GUI
// NOTE: we need a shared_ptr here since WGUI uses enable_shared_from_this.
boost::shared_ptr< WQt4Gui > gui = boost::shared_ptr< WQt4Gui > ( new WQt4Gui( argc, argv ) );
boost::shared_ptr< WQt4Gui > gui = boost::shared_ptr< WQt4Gui > ( new WQt4Gui( optionsMap, argc, argv ) );
int result = gui->run();
std::cout << "Closed OpenWalnut smoothly. Goodbye!" << std::endl;
......
......@@ -32,7 +32,6 @@
#define BOOST_FILESYSTEM_VERSION 2
#endif
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
#include <QtGui/QApplication>
......@@ -69,8 +68,9 @@ WMainWindow* WQt4Gui::m_mainWindow = NULL;
QSettings* WQt4Gui::m_settings = NULL;
WQt4Gui::WQt4Gui( int argc, char** argv )
: WGUI( argc, argv )
WQt4Gui::WQt4Gui( const boost::program_options::variables_map& options, int argc, char** argv )
: WGUI( argc, argv ),
m_optionsMap( options )
{
m_settings = new QSettings( "OpenWalnut.org", "OpenWalnut" );
}
......@@ -80,45 +80,6 @@ WQt4Gui::~WQt4Gui()
delete m_settings;
}
bool WQt4Gui::parseOptions()
{
namespace po = boost::program_options; // since the namespace is far to big we use a shortcut here
po::options_description desc( "Allowed options" );
desc.add_options()
( "help,h", "Prints this help message" )
( "project,p", po::value< std::string >(), "Project file to be loaded on startup." )
( "input,i", po::value< std::vector< std::string > >(), "Input data files that should be loaded automatically" )
( "timed-output,t", "Flag indicating if all log strings should have a time string preceding" );
po::positional_options_description p;
p.add( "input", -1 );
try
{
po::store( po::command_line_parser( m_argc, m_argv ).options( desc ).positional( p ).run(), m_optionsMap );
}
catch( const po::error &e )
{
std::cerr << e.what() << std::endl;
return false;
}
po::notify( m_optionsMap );
// print usage information if command line asks for help.
if( m_optionsMap.count( "help" ) )
{
std::cout << desc << std::endl;
}
else if( m_optionsMap.count( "timed-output" ) )
{
WLogger::getLogger()->setDefaultFormat( "[%t] *%l [%s] %m \n" );
wlog::info( "GUI" ) << "Timed output enabled";
}
return true;
}
void WQt4Gui::moduleError( boost::shared_ptr< WModule > module, const WException& exception )
{
QCoreApplication::postEvent( m_mainWindow, new WModuleCrashEvent( module, exception.what() ) );
......@@ -131,19 +92,6 @@ WMainWindow* WQt4Gui::getMainWindow()
int WQt4Gui::run()
{
bool parsingSuccessful = parseOptions();
if( !parsingSuccessful )
{
return 1;
}
// exit as fast as possible if command line asked for help. The ,essage has been printed by parseOptions().
if( m_optionsMap.count( "help" ) )
{
return 1;
}
// init logger
m_loggerConnection = WLogger::getLogger()->subscribeSignal( WLogger::AddLog, boost::bind( &WQt4Gui::slotAddLog, this, _1 ) );
......
......@@ -52,10 +52,11 @@ public:
/**
* Constructor.
*
* \param options the option-variable map
* \param argc number of arguments given on command line.
* \param argv arguments given on command line.
*/
WQt4Gui( int argc, char** argv );
WQt4Gui( const boost::program_options::variables_map& options, int argc, char** argv );
/**
* Default destructor.
......@@ -207,18 +208,7 @@ private:
*/
boost::shared_ptr< WKernel > m_kernel;
boost::program_options::variables_map m_optionsMap; //!< Map storing the program options.
/**
* This function defines and parses the valid command line options.
* This might once be put in a separate class like WOptionHandler.
* At the moment it seems reasonable that different GUIs might have
* different command line options, thus we implement their parsing
* in the GUI implemntation itself, i.e. here.
*
* \return True if and only if the parsing was successful.
*/
bool parseOptions();
const boost::program_options::variables_map& m_optionsMap; //!< Map storing the program options.
/**
* New log item added. Pushing event to QT's event queue.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment