command_args.h
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 // File: commandArgs.h
00018 // Copyright (c) 2009 Rainer Kümmerle <rk@raikue.net>
00019 //
00020 // This program is free software: you can redistribute it and/or modify it
00021 // under the terms of the GNU Lesser General Public License as published by
00022 // the Free Software Foundation, either version 3 of the License, or (at
00023 // your option) any later version.
00024 //
00025 // This program is distributed in the hope that it will be useful,
00026 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028 // GNU Lesser General Public License for more details.
00029 //
00030 // You should have received a copy of the GNU Lesser General Public License
00031 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00032 
00033 #ifndef COMMAND_ARGS_H
00034 #define COMMAND_ARGS_H
00035 
00036 #include <string>
00037 #include <vector>
00038 #include <iostream>
00039 #include <sstream>
00040 
00041 namespace g2o {
00042 
00050 class CommandArgs
00051 {
00052   public:
00053     struct CommandArgument
00054     {
00055       std::string name;
00056       std::string description;
00057       int type;
00058       void* data;
00059       bool parsed;
00060       bool optional;
00061       CommandArgument() : name(""), description(""), type(0), data(0), parsed(false), optional(false)
00062       {}
00063     };
00064   public:
00065     CommandArgs();
00066     virtual ~CommandArgs();
00067 
00075     bool parseArgs(int argc, char** argv, bool exitOnError = true);
00076 
00078     void param(const std::string& name, bool& p, bool defValue, const std::string& desc);
00080     void param(const std::string& name, int& p, int defValue, const std::string& desc);
00082     void param(const std::string& name, float& p, float defValue, const std::string& desc);
00084     void param(const std::string& name, double& p, double defValue, const std::string& desc);
00086     void param(const std::string& name, std::string& p, const std::string& defValue, const std::string& desc);
00088     void paramLeftOver(const std::string& name, std::string& p, const std::string& defValue, const std::string& desc, bool optional = false);
00089 
00093     void printParams(std::ostream& os);
00094 
00096     const std::string& getBanner() const { return _banner; }
00097     void setBanner(const std::string& banner);
00098 
00102     void printHelp(std::ostream& os);
00103 
00104   protected:
00105     std::vector<CommandArgument> _args;
00106     std::vector<CommandArgument> _leftOvers;
00107     std::vector<CommandArgument> _leftOversOptional;
00108     std::string _banner;
00109     std::string _progName;
00110 
00111     const char* type2str(int t) const;
00112     void str2arg(const std::string& input, CommandArgument& ca) const;
00113     std::string arg2str(const CommandArgument& ca) const;
00114 
00118     template<typename T>
00119     bool convertString(const std::string& s, T& x) const
00120     {
00121       std::istringstream i(s);
00122       bool status = (i >> x);
00123       return status;
00124     }
00125 
00126     std::string trim(const std::string& s) const;
00127 
00129     template<class T1, class T2, class Pred = std::less<T1> >
00130     struct CmpPairFirst {
00131       bool operator()(const std::pair<T1,T2>& left, const std::pair<T1,T2>& right) {
00132         return Pred()(left.first, right.first);
00133       }
00134     };
00135 
00136 };
00137 
00138 } // end namespace
00139 
00140 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:30:57