Implement our own command-line argument parser which accepts '--name value' as well as '--name=value' and '--prop name=vale' as well as '--prop:name=value' to be compatible with the FlightGear command-line syntax.
This commit is contained in:
parent
6e42c8fc57
commit
c24e0fb5b8
6 changed files with 197 additions and 10 deletions
132
utils/fgviewer/ArgumentParser.cxx
Normal file
132
utils/fgviewer/ArgumentParser.cxx
Normal file
|
@ -0,0 +1,132 @@
|
|||
// ArgumentParser.cxx -- flightgear viewer argument parser
|
||||
//
|
||||
// Copyright (C) 2021 by Erik Hofman
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License as
|
||||
// published by the Free Software Foundation; either version 2 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "ArgumentParser.hxx"
|
||||
|
||||
ArgumentParser::ArgumentParser(int argc, char **argv) :
|
||||
arg_num(argc),
|
||||
arg_values(argv),
|
||||
appName(argv[0]),
|
||||
arguments( osg::ArgumentParser(&arg_num, arg_values) )
|
||||
{
|
||||
for (int i=1; i<argc; i++)
|
||||
{
|
||||
std::string arg = argv[i];
|
||||
std::string val;
|
||||
|
||||
if (arg[0] == '-')
|
||||
{
|
||||
std::size_t pos = arg.find('=');
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
val = arg.substr(pos+1);
|
||||
arg = arg.substr(0, pos);
|
||||
}
|
||||
else if (++i<argc) {
|
||||
val = argv[i];
|
||||
} else {
|
||||
val = "";
|
||||
}
|
||||
|
||||
args[arg] = val;
|
||||
}
|
||||
else {
|
||||
files.push_back(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
ArgumentParser::read(const char *arg)
|
||||
{
|
||||
auto it = args.find(arg);
|
||||
if (it != args.end())
|
||||
{
|
||||
args.erase(it);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
ArgumentParser::read(const char *arg, std::string& value)
|
||||
{
|
||||
auto it = args.find(arg);
|
||||
if (it != args.end())
|
||||
{
|
||||
value = it->second;
|
||||
args.erase(it);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
ArgumentParser::read(const char *arg, std::string& name, std::string& value)
|
||||
{
|
||||
auto it = args.find(arg);
|
||||
|
||||
// old fgviewer behavior: --prop name=value
|
||||
if (it != args.end()) {
|
||||
std::string str = it->second;
|
||||
|
||||
std::size_t pos = str.find("=");
|
||||
if (pos != std::string::npos && pos != str.size())
|
||||
{
|
||||
value = str.substr(pos);
|
||||
name = str.substr(0, pos);
|
||||
args.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// FlightGear behavior: --prop:name=value
|
||||
std::string str = name;
|
||||
str += ':';
|
||||
str += name;
|
||||
|
||||
it = args.find(str);
|
||||
if (it != args.end())
|
||||
{
|
||||
value = it->second;
|
||||
args.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
ArgumentParser::reportRemainingOptionsAsUnrecognized()
|
||||
{
|
||||
for (auto it : args) {
|
||||
std::cerr << "Unsupported argument: " << it.first << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ArgumentParser::writeErrorMessages(std::ostream& output)
|
||||
{
|
||||
}
|
57
utils/fgviewer/ArgumentParser.hxx
Normal file
57
utils/fgviewer/ArgumentParser.hxx
Normal file
|
@ -0,0 +1,57 @@
|
|||
// ArgumentParser.hxx -- flightgear viewer argument parser
|
||||
//
|
||||
// Copyright (C) 2021 by Erik Hofman
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License as
|
||||
// published by the Free Software Foundation; either version 2 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <osg/ArgumentParser>
|
||||
|
||||
class ArgumentParser
|
||||
{
|
||||
public:
|
||||
ArgumentParser(int argc, char **argv);
|
||||
~ArgumentParser() = default;
|
||||
|
||||
bool read(const char *arg);
|
||||
bool read(const char *arg, std::string& value);
|
||||
bool read(const char *arg, std::string& name, std::string& value);
|
||||
|
||||
int argc() { return files.size(); }
|
||||
std::string& getApplicationName() { return appName; }
|
||||
|
||||
void reportRemainingOptionsAsUnrecognized();
|
||||
void writeErrorMessages(std::ostream& output);
|
||||
|
||||
osg::ArgumentParser& osg() { return arguments; }
|
||||
|
||||
operator std::vector<std::string>&() { return files; }
|
||||
|
||||
private:
|
||||
int arg_num;
|
||||
char **arg_values;
|
||||
std::string appName;
|
||||
|
||||
std::vector<std::string> errors;
|
||||
std::map<std::string, std::string> args;
|
||||
std::vector<std::string> files;
|
||||
|
||||
osg::ArgumentParser arguments;
|
||||
};
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
set(FGVIEWER_SOURCES
|
||||
ArgumentParser.cxx
|
||||
fgviewer.cxx
|
||||
Drawable.cxx
|
||||
Renderer.cxx
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include "Viewer.hxx"
|
||||
|
||||
#include <osg/Version>
|
||||
#include <osg/ArgumentParser>
|
||||
#include <osg/ProxyNode>
|
||||
#include <osg/PagedLOD>
|
||||
#include <osgDB/ReadFile>
|
||||
|
@ -34,13 +33,14 @@
|
|||
#endif
|
||||
|
||||
#include "MEncoderCaptureOperation.hxx"
|
||||
#include "ArgumentParser.hxx"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace fgviewer {
|
||||
|
||||
Viewer::Viewer(osg::ArgumentParser& arguments) :
|
||||
osgViewer::Viewer(arguments),
|
||||
Viewer::Viewer(ArgumentParser& arguments) :
|
||||
osgViewer::Viewer(arguments.osg()),
|
||||
_sceneDataGroup(new osg::Group),
|
||||
_timeIncrement(SGTimeStamp::fromSec(0)),
|
||||
_simTime(SGTimeStamp::fromSec(0))
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#ifndef _FGVIEWER_VIEWER_HXX
|
||||
#define _FGVIEWER_VIEWER_HXX
|
||||
|
||||
#include <osg/ArgumentParser>
|
||||
#include <osgViewer/Viewer>
|
||||
|
||||
#include <simgear/math/SGMath.hxx>
|
||||
|
@ -31,6 +30,7 @@
|
|||
#include "Frustum.hxx"
|
||||
#include "Renderer.hxx"
|
||||
#include "SlaveCamera.hxx"
|
||||
#include "ArgumentParser.hxx"
|
||||
|
||||
#if FG_HAVE_HLA
|
||||
#include "HLAViewerFederate.hxx"
|
||||
|
@ -40,7 +40,7 @@ namespace fgviewer {
|
|||
|
||||
class Viewer : public osgViewer::Viewer {
|
||||
public:
|
||||
Viewer(osg::ArgumentParser& arguments);
|
||||
Viewer(ArgumentParser& arguments);
|
||||
virtual ~Viewer();
|
||||
|
||||
bool readCameraConfig(const SGPropertyNode& viewerNode);
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#endif
|
||||
|
||||
#include <osg/Version>
|
||||
#include <osg/ArgumentParser>
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgViewer/ViewerEventHandlers>
|
||||
#include <osgGA/KeySwitchMatrixManipulator>
|
||||
|
@ -40,6 +39,7 @@
|
|||
#include <simgear/scene/model/ModelRegistry.hxx>
|
||||
#include <simgear/misc/ResourceManager.hxx>
|
||||
|
||||
#include "ArgumentParser.hxx"
|
||||
#include "Renderer.hxx"
|
||||
#include "Viewer.hxx"
|
||||
|
||||
|
@ -53,10 +53,7 @@ int
|
|||
main(int argc, char** argv)
|
||||
{
|
||||
/// Read arguments and environment variables.
|
||||
|
||||
// use an ArgumentParser object to manage the program arguments.
|
||||
// FIXME implement a flightgear similar argument parser into simgear and use this one
|
||||
osg::ArgumentParser arguments(&argc, argv);
|
||||
ArgumentParser arguments(argc, argv);
|
||||
|
||||
sglog().set_log_classes(SG_ALL);
|
||||
sglog().set_log_priority(SG_ALERT);
|
||||
|
|
Loading…
Add table
Reference in a new issue