lemon/arg_parser.h
changeset 843 81f7e910060b
parent 440 88ed40ad0d4f
child 877 141f9c0db4a3
equal deleted inserted replaced
13:42ec9ba30fb1 14:539780c061a0
    32 ///\file
    32 ///\file
    33 ///\brief A tool to parse command line arguments.
    33 ///\brief A tool to parse command line arguments.
    34 
    34 
    35 namespace lemon {
    35 namespace lemon {
    36 
    36 
       
    37   ///Exception used by ArgParser
       
    38   class ArgParserException : public Exception {
       
    39   public:
       
    40     enum Reason {
       
    41       HELP,         /// <tt>--help</tt> option was given
       
    42       UNKNOWN_OPT,  /// Unknown option was given
       
    43       INVALID_OPT   /// Invalid combination of options
       
    44     };
       
    45     
       
    46   private:
       
    47     Reason _reason;
       
    48     
       
    49   public:
       
    50     ///Constructor
       
    51     ArgParserException(Reason r) throw() : _reason(r) {}
       
    52     ///Virtual destructor
       
    53     virtual ~ArgParserException() throw() {}
       
    54     ///A short description of the exception
       
    55     virtual const char* what() const throw() {
       
    56       switch(_reason)
       
    57         {
       
    58         case HELP:
       
    59           return "lemon::ArgParseException: ask for help";
       
    60           break;
       
    61         case UNKNOWN_OPT:
       
    62           return "lemon::ArgParseException: unknown option";
       
    63           break;
       
    64         case INVALID_OPT:
       
    65           return "lemon::ArgParseException: invalid combination of options";
       
    66           break;
       
    67         }
       
    68       return "";
       
    69     }
       
    70     ///Return the reason for the failure
       
    71     Reason reason() const {return _reason; }
       
    72   };
       
    73 
       
    74 
    37   ///Command line arguments parser
    75   ///Command line arguments parser
    38 
    76 
    39   ///\ingroup misc
    77   ///\ingroup misc
    40   ///Command line arguments parser.
    78   ///Command line arguments parser.
    41   ///
    79   ///
   101 
   139 
   102     std::vector<OtherArg> _others_help;
   140     std::vector<OtherArg> _others_help;
   103     std::vector<std::string> _file_args;
   141     std::vector<std::string> _file_args;
   104     std::string _command_name;
   142     std::string _command_name;
   105 
   143 
   106 
   144     
   107   private:
   145   private:
   108     //Bind a function to an option.
   146     //Bind a function to an option.
   109 
   147 
   110     //\param name The name of the option. The leading '-' must be omitted.
   148     //\param name The name of the option. The leading '-' must be omitted.
   111     //\param help A help string.
   149     //\param help A help string.
   113     //  must be of type "void f(void *)"
   151     //  must be of type "void f(void *)"
   114     //\param data Data to be passed to \c func
   152     //\param data Data to be passed to \c func
   115     ArgParser &funcOption(const std::string &name,
   153     ArgParser &funcOption(const std::string &name,
   116                     const std::string &help,
   154                     const std::string &help,
   117                     void (*func)(void *),void *data);
   155                     void (*func)(void *),void *data);
       
   156 
       
   157     bool _exit_on_problems;
       
   158     
       
   159     void _terminate(ArgParserException::Reason reason) const;
   118 
   160 
   119   public:
   161   public:
   120 
   162 
   121     ///Constructor
   163     ///Constructor
   122     ArgParser(int argc, const char * const *argv);
   164     ArgParser(int argc, const char * const *argv);
   378 
   420 
   379     ///Give back a reference to a vector consisting of the program arguments
   421     ///Give back a reference to a vector consisting of the program arguments
   380     ///not starting with a '-' character.
   422     ///not starting with a '-' character.
   381     const std::vector<std::string> &files() const { return _file_args; }
   423     const std::vector<std::string> &files() const { return _file_args; }
   382 
   424 
       
   425     ///Throw instead of exit in case of problems
       
   426     void throwOnProblems() 
       
   427     {
       
   428       _exit_on_problems=false;
       
   429     }
   383   };
   430   };
   384 }
   431 }
   385 
   432 
   386 #endif // LEMON_ARG_PARSER_H
   433 #endif // LEMON_ARG_PARSER_H