00001 /* Arg_parser - A POSIX/GNU command line argument parser. 00002 Copyright (C) 2006, 2007, 2008 Antonio Diaz Diaz. 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program 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 General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 /* Arg_parser reads the arguments in `argv' and creates a number of 00019 option codes, option arguments and non-option arguments. 00020 00021 In case of error, `error' returns a non-empty error message. 00022 00023 `options' is an array of `struct Option' terminated by an element 00024 containing a code which is zero. A null name means a short-only 00025 option. A code value outside the unsigned char range means a 00026 long-only option. 00027 00028 Arg_parser normally makes it appear as if all the option arguments 00029 were specified before all the non-option arguments for the purposes 00030 of parsing, even if the user of your program intermixed option and 00031 non-option arguments. If you want the arguments in the exact order 00032 the user typed them, call `Arg_parser' with `in_order' = true. 00033 00034 The argument `--' terminates all options; any following arguments are 00035 treated as non-option arguments, even if they begin with a hyphen. 00036 00037 The syntax for optional option arguments is `-<short_option><argument>' 00038 (without whitespace), or `--<long_option>=<argument>'. 00039 */ 00040 00041 class Arg_parser 00042 { 00043 public: 00044 enum Has_arg { no, yes, maybe }; 00045 00046 struct Option 00047 { 00048 int code; // Short option letter or code ( code != 0 ) 00049 const char * name; // Long option name (maybe null) 00050 Has_arg has_arg; 00051 }; 00052 00053 private: 00054 struct Record 00055 { 00056 int code; 00057 std::string argument; 00058 Record ( const int c = 0 ) : code ( c ) {} 00059 }; 00060 00061 std::string _error; 00062 std::vector< Record > data; 00063 00064 bool parse_long_option ( const char * const opt, const char * const arg, 00065 const Option options[], int & argind ) throw(); 00066 bool parse_short_option ( const char * const opt, const char * const arg, 00067 const Option options[], int & argind ) throw(); 00068 00069 public: 00070 Arg_parser ( const int argc, const char * const argv[], 00071 const Option options[], const bool in_order = false ) throw(); 00072 00073 // Restricted constructor. Parses a single token and argument (if any) 00074 Arg_parser ( const char * const opt, const char * const arg, 00075 const Option options[] ) throw(); 00076 00077 const std::string & error() const throw() { return _error; } 00078 00079 // The number of arguments parsed (may be different from argc) 00080 int arguments() const throw() { return data.size(); } 00081 00082 // If code( i ) is 0, argument( i ) is a non-option. 00083 // Else argument( i ) is the option's argument (or empty). 00084 int code ( const int i ) const throw() 00085 { 00086 if ( i >= 0 && i < arguments() ) return data[i].code; 00087 else return 0; 00088 } 00089 00090 const std::string & argument ( const int i ) const throw() 00091 { 00092 if ( i >= 0 && i < arguments() ) return data[i].argument; 00093 else return _error; 00094 } 00095 };