From 965c72f5bd4f8436dd7abf2fd21eb038b497f559 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 12 May 2008 10:55:55 +0000 Subject: [PATCH] From Eric Sokolowski and Robert Osfield, moved command line option usage setup from osgviewer example into osg::ArgumentParser and osgViewer::Viewer to make them more universally available. --- applications/osgviewer/osgviewer.cpp | 20 ++++---------------- include/osg/ApplicationUsage | 4 +++- include/osg/ArgumentParser | 8 ++++++++ src/osg/ArgumentParser.cpp | 16 ++++++++++++++++ src/osgViewer/Viewer.cpp | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/applications/osgviewer/osgviewer.cpp b/applications/osgviewer/osgviewer.cpp index e3730c600..82186ca7c 100644 --- a/applications/osgviewer/osgviewer.cpp +++ b/applications/osgviewer/osgviewer.cpp @@ -39,27 +39,15 @@ int main(int argc, char** argv) arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("--image ","Load an image and render it on a quad"); arguments.getApplicationUsage()->addCommandLineOption("--dem ","Load an image/DEM and render it on a HeightField"); - arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); - arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available"); - arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available"); - arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings."); - arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded","Select SingleThreaded threading model for viewer."); - arguments.getApplicationUsage()->addCommandLineOption("--CullDrawThreadPerContext","Select CullDrawThreadPerContext threading model for viewer."); - arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext","Select DrawThreadPerContext threading model for viewer."); - arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext","Select CullThreadPerCameraDrawThreadPerContext threading model for viewer."); - // if user request help write it out to cout. - bool helpAll = arguments.read("--help-all"); - unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) | - ((helpAll || arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) | - ((helpAll || arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 ); - if (helpType) + osgViewer::Viewer viewer(arguments); + + unsigned int helpType = 0; + if ((helpType = arguments.readHelpType())) { arguments.getApplicationUsage()->write(std::cout, helpType); return 1; } - - osgViewer::Viewer viewer(arguments); // report any errors if they have occurred when parsing the program arguments. if (arguments.errors()) diff --git a/include/osg/ApplicationUsage b/include/osg/ApplicationUsage index 312f7c758..1f71f0a06 100644 --- a/include/osg/ApplicationUsage +++ b/include/osg/ApplicationUsage @@ -43,9 +43,11 @@ class OSG_EXPORT ApplicationUsage : public osg::Referenced enum Type { + NO_HELP = 0x0, COMMAND_LINE_OPTION = 0x1, ENVIRONMENTAL_VARIABLE = 0x2, - KEYBOARD_MOUSE_BINDING = 0x4 + KEYBOARD_MOUSE_BINDING = 0x4, + HELP_ALL = KEYBOARD_MOUSE_BINDING|ENVIRONMENTAL_VARIABLE|COMMAND_LINE_OPTION }; void addUsageExplanation(Type type,const std::string& option,const std::string& explanation); diff --git a/include/osg/ArgumentParser b/include/osg/ArgumentParser index 4562c90b0..0c7699b68 100644 --- a/include/osg/ArgumentParser +++ b/include/osg/ArgumentParser @@ -190,6 +190,14 @@ class OSG_EXPORT ArgumentParser /** Write error messages to the given ostream, if at or above the given severity. */ void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN); + + /** This convinience method handles help requests on the command line. + * Return the type(s) of help requested. The return value of this + * function is suitable for passing into getApplicationUsage()->write(). + * If ApplicationUsage::NO_HELP is returned then no help commandline option + * was found on the command line. */ + ApplicationUsage::Type readHelpType(); + protected: diff --git a/src/osg/ArgumentParser.cpp b/src/osg/ArgumentParser.cpp index 51c31e756..4ead39119 100644 --- a/src/osg/ArgumentParser.cpp +++ b/src/osg/ArgumentParser.cpp @@ -630,3 +630,19 @@ void ArgumentParser::writeErrorMessages(std::ostream& output,ErrorSeverity sever } } } + +ApplicationUsage::Type ArgumentParser::readHelpType() +{ + getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); + getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available"); + getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available"); + getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings."); + + // if user request help write it out to cout. + if (read("--help-all")) return ApplicationUsage::HELP_ALL; + if (read("-h") || read("--help")) return ApplicationUsage::COMMAND_LINE_OPTION; + if (read("--help-env")) return ApplicationUsage::ENVIRONMENTAL_VARIABLE; + if (read("--help-keys")) return ApplicationUsage::KEYBOARD_MOUSE_BINDING; + + return ApplicationUsage::NO_HELP; +} diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index f54d96ad0..0c3c907c3 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -43,6 +43,21 @@ Viewer::Viewer(osg::ArgumentParser& arguments) _viewerBase = this; constructorInit(); + + // Add help for command-line options read here + arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded","Select SingleThreaded threading model for viewer."); + arguments.getApplicationUsage()->addCommandLineOption("--CullDrawThreadPerContext","Select CullDrawThreadPerContext threading model for viewer."); + arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext","Select DrawThreadPerContext threading model for viewer."); + arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext","Select CullThreadPerCameraDrawThreadPerContext threading model for viewer."); + arguments.getApplicationUsage()->addCommandLineOption("--clear-color ","Set the background color of the viewer in the form \"r,g,b[,a]\"."); + arguments.getApplicationUsage()->addCommandLineOption("--screen ","Set the screen to use when multiple screens are present."); + arguments.getApplicationUsage()->addCommandLineOption("--window ","Set the position (x,y) and size (w,h) of the viewer window."); + // FIXME: Uncomment these lines when the options have been documented properly + //arguments.getApplicationUsage()->addCommandLineOption("--3d-sd",""); + //arguments.getApplicationUsage()->addCommandLineOption("--panoramic-sd",""); + //arguments.getApplicationUsage()->addCommandLineOption("--radius",""); + //arguments.getApplicationUsage()->addCommandLineOption("--collar",""); + //arguments.getApplicationUsage()->addCommandLineOption("--im",""); std::string filename; bool readConfig = false;