1*6b3a42afSjmmv // Copyright 2010 Google Inc. 2*6b3a42afSjmmv // All rights reserved. 3*6b3a42afSjmmv // 4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without 5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are 6*6b3a42afSjmmv // met: 7*6b3a42afSjmmv // 8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright 9*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer. 10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright 11*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer in the 12*6b3a42afSjmmv // documentation and/or other materials provided with the distribution. 13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors 14*6b3a42afSjmmv // may be used to endorse or promote products derived from this software 15*6b3a42afSjmmv // without specific prior written permission. 16*6b3a42afSjmmv // 17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*6b3a42afSjmmv 29*6b3a42afSjmmv /// \file utils/cmdline/options.hpp 30*6b3a42afSjmmv /// Definitions of command-line options. 31*6b3a42afSjmmv 32*6b3a42afSjmmv #if !defined(UTILS_CMDLINE_OPTIONS_HPP) 33*6b3a42afSjmmv #define UTILS_CMDLINE_OPTIONS_HPP 34*6b3a42afSjmmv 35*6b3a42afSjmmv #include <string> 36*6b3a42afSjmmv #include <utility> 37*6b3a42afSjmmv #include <vector> 38*6b3a42afSjmmv 39*6b3a42afSjmmv #include "utils/fs/path.hpp" 40*6b3a42afSjmmv 41*6b3a42afSjmmv namespace utils { 42*6b3a42afSjmmv namespace cmdline { 43*6b3a42afSjmmv 44*6b3a42afSjmmv 45*6b3a42afSjmmv /// Type-less base option class. 46*6b3a42afSjmmv /// 47*6b3a42afSjmmv /// This abstract class provides the most generic representation of options. It 48*6b3a42afSjmmv /// allows defining options with both short and long names, with and without 49*6b3a42afSjmmv /// arguments and with and without optional values. These are all the possible 50*6b3a42afSjmmv /// combinations supported by the getopt_long(3) function, on which this is 51*6b3a42afSjmmv /// built. 52*6b3a42afSjmmv /// 53*6b3a42afSjmmv /// The internal values (e.g. the default value) of a generic option are all 54*6b3a42afSjmmv /// represented as strings. However, from the caller's perspective, this is 55*6b3a42afSjmmv /// suboptimal. Hence why this class must be specialized: the subclasses 56*6b3a42afSjmmv /// provide type-specific accessors and provide automatic validation of the 57*6b3a42afSjmmv /// types (e.g. a string '3foo' is not passed to an integer option). 58*6b3a42afSjmmv /// 59*6b3a42afSjmmv /// Given that subclasses are used through templatized code, they must provide: 60*6b3a42afSjmmv /// 61*6b3a42afSjmmv /// <ul> 62*6b3a42afSjmmv /// <li>A public option_type typedef that defines the type of the 63*6b3a42afSjmmv /// option.</li> 64*6b3a42afSjmmv /// 65*6b3a42afSjmmv /// <li>A convert() method that takes a string and converts it to 66*6b3a42afSjmmv /// option_type. The string can be assumed to be convertible to the 67*6b3a42afSjmmv /// destination type. Should not raise exceptions.</li> 68*6b3a42afSjmmv /// 69*6b3a42afSjmmv /// <li>A validate() method that matches the implementation of convert(). 70*6b3a42afSjmmv /// This method can throw option_argument_value_error if the string cannot 71*6b3a42afSjmmv /// be converted appropriately. If validate() does not throw, then 72*6b3a42afSjmmv /// convert() must execute successfully.</li> 73*6b3a42afSjmmv /// </ul> 74*6b3a42afSjmmv /// 75*6b3a42afSjmmv /// TODO(jmmv): Many methods in this class are split into two parts: has_foo() 76*6b3a42afSjmmv /// and foo(), the former to query if the foo is available and the latter to get 77*6b3a42afSjmmv /// the foo. It'd be very nice if we'd use something similar Boost.Optional to 78*6b3a42afSjmmv /// simplify this interface altogether. 79*6b3a42afSjmmv class base_option { 80*6b3a42afSjmmv /// Short name of the option; 0 to indicate that none is available. 81*6b3a42afSjmmv char _short_name; 82*6b3a42afSjmmv 83*6b3a42afSjmmv /// Long name of the option. 84*6b3a42afSjmmv std::string _long_name; 85*6b3a42afSjmmv 86*6b3a42afSjmmv /// Textual description of the purpose of the option. 87*6b3a42afSjmmv std::string _description; 88*6b3a42afSjmmv 89*6b3a42afSjmmv /// Descriptive name of the required argument; empty if not allowed. 90*6b3a42afSjmmv std::string _arg_name; 91*6b3a42afSjmmv 92*6b3a42afSjmmv /// Whether the option has a default value or not. 93*6b3a42afSjmmv /// 94*6b3a42afSjmmv /// \todo We should probably be using the optional class here. 95*6b3a42afSjmmv bool _has_default_value; 96*6b3a42afSjmmv 97*6b3a42afSjmmv /// If _has_default_value is true, the default value. 98*6b3a42afSjmmv std::string _default_value; 99*6b3a42afSjmmv 100*6b3a42afSjmmv public: 101*6b3a42afSjmmv base_option(const char, const char*, const char*, const char* = NULL, 102*6b3a42afSjmmv const char* = NULL); 103*6b3a42afSjmmv base_option(const char*, const char*, const char* = NULL, 104*6b3a42afSjmmv const char* = NULL); 105*6b3a42afSjmmv virtual ~base_option(void); 106*6b3a42afSjmmv 107*6b3a42afSjmmv bool has_short_name(void) const; 108*6b3a42afSjmmv char short_name(void) const; 109*6b3a42afSjmmv const std::string& long_name(void) const; 110*6b3a42afSjmmv const std::string& description(void) const; 111*6b3a42afSjmmv 112*6b3a42afSjmmv bool needs_arg(void) const; 113*6b3a42afSjmmv const std::string& arg_name(void) const; 114*6b3a42afSjmmv 115*6b3a42afSjmmv bool has_default_value(void) const; 116*6b3a42afSjmmv const std::string& default_value(void) const; 117*6b3a42afSjmmv 118*6b3a42afSjmmv std::string format_short_name(void) const; 119*6b3a42afSjmmv std::string format_long_name(void) const; 120*6b3a42afSjmmv 121*6b3a42afSjmmv virtual void validate(const std::string&) const; 122*6b3a42afSjmmv }; 123*6b3a42afSjmmv 124*6b3a42afSjmmv 125*6b3a42afSjmmv /// Definition of a boolean option. 126*6b3a42afSjmmv /// 127*6b3a42afSjmmv /// A boolean option can be specified once in the command line, at which point 128*6b3a42afSjmmv /// is set to true. Such an option cannot carry optional arguments. 129*6b3a42afSjmmv class bool_option : public base_option { 130*6b3a42afSjmmv public: 131*6b3a42afSjmmv bool_option(const char, const char*, const char*); 132*6b3a42afSjmmv bool_option(const char*, const char*); ~bool_option(void)133*6b3a42afSjmmv virtual ~bool_option(void) {} 134*6b3a42afSjmmv 135*6b3a42afSjmmv /// The data type of this option. 136*6b3a42afSjmmv typedef bool option_type; 137*6b3a42afSjmmv }; 138*6b3a42afSjmmv 139*6b3a42afSjmmv 140*6b3a42afSjmmv /// Definition of an integer option. 141*6b3a42afSjmmv class int_option : public base_option { 142*6b3a42afSjmmv public: 143*6b3a42afSjmmv int_option(const char, const char*, const char*, const char*, 144*6b3a42afSjmmv const char* = NULL); 145*6b3a42afSjmmv int_option(const char*, const char*, const char*, const char* = NULL); ~int_option(void)146*6b3a42afSjmmv virtual ~int_option(void) {} 147*6b3a42afSjmmv 148*6b3a42afSjmmv /// The data type of this option. 149*6b3a42afSjmmv typedef int option_type; 150*6b3a42afSjmmv 151*6b3a42afSjmmv virtual void validate(const std::string& str) const; 152*6b3a42afSjmmv static int convert(const std::string& str); 153*6b3a42afSjmmv }; 154*6b3a42afSjmmv 155*6b3a42afSjmmv 156*6b3a42afSjmmv /// Definition of a comma-separated list of strings. 157*6b3a42afSjmmv class list_option : public base_option { 158*6b3a42afSjmmv public: 159*6b3a42afSjmmv list_option(const char, const char*, const char*, const char*, 160*6b3a42afSjmmv const char* = NULL); 161*6b3a42afSjmmv list_option(const char*, const char*, const char*, const char* = NULL); ~list_option(void)162*6b3a42afSjmmv virtual ~list_option(void) {} 163*6b3a42afSjmmv 164*6b3a42afSjmmv /// The data type of this option. 165*6b3a42afSjmmv typedef std::vector< std::string > option_type; 166*6b3a42afSjmmv 167*6b3a42afSjmmv virtual void validate(const std::string&) const; 168*6b3a42afSjmmv static option_type convert(const std::string&); 169*6b3a42afSjmmv }; 170*6b3a42afSjmmv 171*6b3a42afSjmmv 172*6b3a42afSjmmv /// Definition of an option representing a path. 173*6b3a42afSjmmv /// 174*6b3a42afSjmmv /// The path pointed to by the option may not exist, but it must be 175*6b3a42afSjmmv /// syntactically valid. 176*6b3a42afSjmmv class path_option : public base_option { 177*6b3a42afSjmmv public: 178*6b3a42afSjmmv path_option(const char, const char*, const char*, const char*, 179*6b3a42afSjmmv const char* = NULL); 180*6b3a42afSjmmv path_option(const char*, const char*, const char*, const char* = NULL); ~path_option(void)181*6b3a42afSjmmv virtual ~path_option(void) {} 182*6b3a42afSjmmv 183*6b3a42afSjmmv /// The data type of this option. 184*6b3a42afSjmmv typedef utils::fs::path option_type; 185*6b3a42afSjmmv 186*6b3a42afSjmmv virtual void validate(const std::string&) const; 187*6b3a42afSjmmv static utils::fs::path convert(const std::string&); 188*6b3a42afSjmmv }; 189*6b3a42afSjmmv 190*6b3a42afSjmmv 191*6b3a42afSjmmv /// Definition of a property option. 192*6b3a42afSjmmv /// 193*6b3a42afSjmmv /// A property option is an option whose required arguments are of the form 194*6b3a42afSjmmv /// 'name=value'. Both components of the property are treated as free-form 195*6b3a42afSjmmv /// non-empty strings; any other validation must happen on the caller side. 196*6b3a42afSjmmv /// 197*6b3a42afSjmmv /// \todo Would be nice if the delimiter was parametrizable. With the current 198*6b3a42afSjmmv /// parser interface (convert() being a static method), the only way to do 199*6b3a42afSjmmv /// this would be to templatize this class. 200*6b3a42afSjmmv class property_option : public base_option { 201*6b3a42afSjmmv public: 202*6b3a42afSjmmv property_option(const char, const char*, const char*, const char*); 203*6b3a42afSjmmv property_option(const char*, const char*, const char*); ~property_option(void)204*6b3a42afSjmmv virtual ~property_option(void) {} 205*6b3a42afSjmmv 206*6b3a42afSjmmv /// The data type of this option. 207*6b3a42afSjmmv typedef std::pair< std::string, std::string > option_type; 208*6b3a42afSjmmv 209*6b3a42afSjmmv virtual void validate(const std::string& str) const; 210*6b3a42afSjmmv static option_type convert(const std::string& str); 211*6b3a42afSjmmv }; 212*6b3a42afSjmmv 213*6b3a42afSjmmv 214*6b3a42afSjmmv /// Definition of a free-form string option. 215*6b3a42afSjmmv /// 216*6b3a42afSjmmv /// This class provides no restrictions on the argument passed to the option. 217*6b3a42afSjmmv class string_option : public base_option { 218*6b3a42afSjmmv public: 219*6b3a42afSjmmv string_option(const char, const char*, const char*, const char*, 220*6b3a42afSjmmv const char* = NULL); 221*6b3a42afSjmmv string_option(const char*, const char*, const char*, const char* = NULL); ~string_option(void)222*6b3a42afSjmmv virtual ~string_option(void) {} 223*6b3a42afSjmmv 224*6b3a42afSjmmv /// The data type of this option. 225*6b3a42afSjmmv typedef std::string option_type; 226*6b3a42afSjmmv 227*6b3a42afSjmmv virtual void validate(const std::string& str) const; 228*6b3a42afSjmmv static std::string convert(const std::string& str); 229*6b3a42afSjmmv }; 230*6b3a42afSjmmv 231*6b3a42afSjmmv 232*6b3a42afSjmmv } // namespace cmdline 233*6b3a42afSjmmv } // namespace utils 234*6b3a42afSjmmv 235*6b3a42afSjmmv #endif // !defined(UTILS_CMDLINE_OPTIONS_HPP) 236