CmdLine.h
Go to the documentation of this file.
00001 /** 00002 * @file basis/CmdLine.h 00003 * @brief Manages command line definition and parsing of arguments. 00004 * 00005 * Copyright (c) 2011, 2012 University of Pennsylvania. All rights reserved.<br /> 00006 * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file. 00007 * 00008 * Contact: SBIA Group <sbia-software at uphs.upenn.edu> 00009 * 00010 * @ingroup CxxCmdLine 00011 */ 00012 00013 #pragma once 00014 #ifndef _BASIS_CMDLINE_H 00015 #define _BASIS_CMDLINE_H 00016 00017 00018 #include "tclap/CmdLine.h" // TCLAP implementations 00019 #include "CmdArgs.h" // commonly used arguments 00020 00021 00022 namespace basis { 00023 00024 00025 /** 00026 * @brief Manages command line definition and parsing of arguments. 00027 * 00028 * Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br /> 00029 * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file. 00030 * 00031 * @ingroup CxxCmdLine 00032 */ 00033 class CmdLine : public TCLAP::CmdLine 00034 { 00035 // ----------------------------------------------------------------------- 00036 // XorHandler 00037 protected: 00038 00039 /** 00040 * @brief Handles lists of Arg's that are to be XOR'd on the command-line. 00041 * 00042 * This subclass of the TCLAP::XorHandler overloads the check() method such 00043 * that XOR'd arguments where none of the arguments is required are handled 00044 * correctly. The TCLA::XorHandler and TCLAP::CmdLine implementations imply 00045 * that all XOR'd arguments are required, i.e., that one of the mutual 00046 * exclusive arguments need to be specified. The sbia::basis::XorHandler and 00047 * sbia::basis::CmdLine implementations, on the other side, do not require 00048 * that any of the XOR'd arguments be given on the command-line if none of 00049 * the XOR'd arguments is required. 00050 */ 00051 class XorHandler : public TCLAP::XorHandler 00052 { 00053 public: 00054 00055 /** 00056 * @brief Constructor. 00057 */ 00058 XorHandler() {} 00059 00060 /** 00061 * @brief Checks whether the specified Arg is in one of the xor lists. 00062 * 00063 * If the argument does match one, this function returns the size of the xor 00064 * list that the Arg matched if the Arg is required. If the Arg matches, 00065 * then it also sets the rest of the Arg's in the list. 00066 * 00067 * @param a The Arg to be checked. 00068 */ 00069 int check(const Arg* a) 00070 { 00071 int n = TCLAP::XorHandler::check(a); 00072 return a->isRequired() ? n : 0; 00073 } 00074 00075 }; // class XorHandler 00076 00077 // ----------------------------------------------------------------------- 00078 // construction / destruction 00079 public: 00080 00081 /** 00082 * @brief Constructor. 00083 * 00084 * @param [in] name Program name. Should be a constant string which 00085 * helps to identify the program, not the name of 00086 * the executable as determined at runtime. 00087 * @param [in] project Name of project this program belongs to. 00088 * @param [in] description Program description. 00089 * @param [in] example Usage example. 00090 * @param [in] version Program version. 00091 * @param [in] copyright Copyright notice. 00092 * @param [in] license License information. 00093 * @param [in] contact Contact information. 00094 * @param [in] stdargs Enable/disable handling of standard arguments. 00095 */ 00096 CmdLine(const std::string& name, 00097 const std::string& project, 00098 const std::string& description, 00099 const std::string& example, 00100 const std::string& version, 00101 const std::string& copyright = 00102 "Copyright (c) University of Pennsylvania." 00103 " All rights reserved.", 00104 const std::string& license = 00105 "See https://www.cbica.upenn.edu/sbia/software/license.html" 00106 " or COPYING file.", 00107 const std::string& contact = 00108 "SBIA Group <sbia-software at uphs.upenn.edu>", 00109 bool stdargs = true); 00110 00111 /** 00112 * @brief Constructor. 00113 * 00114 * @param [in] name Program name. Should be a constant string which 00115 * helps to identify the program, not the name of 00116 * the executable as determined at runtime. 00117 * @param [in] project Name of project this program belongs to. 00118 * @param [in] description Program description. 00119 * @param [in] examples Usage examples. 00120 * @param [in] version Program version. 00121 * @param [in] copyright Copyright notice. 00122 * @param [in] license License information. 00123 * @param [in] contact Contact information. 00124 * @param [in] stdargs Enable/disable handling of standard arguments. 00125 */ 00126 CmdLine(const std::string& name, 00127 const std::string& project, 00128 const std::string& description, 00129 const std::vector<std::string>& examples, 00130 const std::string& version, 00131 const std::string& copyright = 00132 "Copyright (c) University of Pennsylvania." 00133 " All rights reserved.", 00134 const std::string& license = 00135 "See https://www.cbica.upenn.edu/sbia/software/license.html" 00136 " or COPYING file.", 00137 const std::string& contact = 00138 "SBIA Group <sbia-software at uphs.upenn.edu>", 00139 bool stdargs = true); 00140 00141 /** 00142 * @brief Destructor. 00143 */ 00144 virtual ~CmdLine() { } 00145 00146 // ----------------------------------------------------------------------- 00147 // command arguments 00148 public: 00149 00150 /** 00151 * @brief Adds an argument to the list of arguments to be parsed. 00152 * 00153 * @param [in] a Argument to be added. 00154 */ 00155 void add(Arg& a); 00156 00157 /** 00158 * @brief An alternative add. Functionally identical. 00159 * 00160 * @param [in] a Argument to be added. 00161 */ 00162 void add(Arg* a); 00163 00164 /** 00165 * @brief Add two Args that will be xor'd. 00166 * 00167 * If this method is used, add does not need to be called. 00168 * 00169 * @param [in] a Argument to be added and xor'd. 00170 * @param [in] b Argument to be added and xor'd. 00171 */ 00172 void xorAdd(Arg& a, Arg& b); 00173 00174 /** 00175 * @brief Add a list of arguments that will be xor'd. 00176 * 00177 * If this method is used, add does not need to be called. 00178 * 00179 * @param [in] xors List of Args to be added and xor'd. 00180 */ 00181 void xorAdd(std::vector<Arg*>& xors); 00182 00183 // ----------------------------------------------------------------------- 00184 // help / version 00185 public: 00186 00187 /** 00188 * @brief Print short help, i.e., usage information. 00189 */ 00190 void print_usage() const; 00191 00192 /** 00193 * @brief Print help. 00194 */ 00195 void print_help() const; 00196 00197 /** 00198 * @brief Print version information. 00199 */ 00200 void print_version() const; 00201 00202 // ----------------------------------------------------------------------- 00203 // parse command-line arguments 00204 public: 00205 00206 /** 00207 * @brief Parses the command line. 00208 * 00209 * @param [in] argc Number of arguments. 00210 * @param [in] argv Array of arguments. 00211 */ 00212 void parse(int argc, const char* const* argv); 00213 00214 /** 00215 * @brief Parses the command line. 00216 * 00217 * @param [in] args A vector of strings representing the args. 00218 * args[0] is still the program name. 00219 */ 00220 void parse(std::vector<std::string>& args); 00221 00222 // ----------------------------------------------------------------------- 00223 // accessors 00224 public: 00225 00226 /** 00227 * @brief Get name of program. 00228 * 00229 * @returns Name of program this command-line object belongs to. 00230 */ 00231 std::string& getProgramName() { return _name; } 00232 00233 /** 00234 * @brief Get name of project the program belongs to. 00235 * 00236 * @returns Name of project this program belongs to. 00237 */ 00238 std::string& getProjectName() { return _project; } 00239 00240 /** 00241 * @brief Get program description. 00242 * 00243 * @returns Description of program this command-line object belongs to. 00244 */ 00245 std::string& getDescription() { return _message; } 00246 00247 /** 00248 * @brief Get usage example. 00249 * 00250 * @returns Example command-line usage. 00251 */ 00252 std::vector<std::string>& getExamples() { return _examples; } 00253 00254 /** 00255 * @brief Get copyright notice. 00256 * 00257 * @return Copyright information of program. 00258 */ 00259 std::string& getCopyright() { return _copyright; } 00260 00261 /** 00262 * @brief Get license information. 00263 * 00264 * @returns License information of program. 00265 */ 00266 std::string& getLicense() { return _license; } 00267 00268 /** 00269 * @brief Get contact information. 00270 * 00271 * @returns Contact information. 00272 */ 00273 std::string& getContact() { return _contact; } 00274 00275 /** 00276 * @brief Get handler of XOR'd arguments. 00277 */ 00278 XorHandler& getXorHandler() { return _xorHandler; } 00279 00280 // ----------------------------------------------------------------------- 00281 // helpers 00282 protected: 00283 00284 /** 00285 * @brief Set up command-line object. 00286 */ 00287 void setup(bool stdargs); 00288 00289 // ----------------------------------------------------------------------- 00290 // unsupported 00291 private: 00292 00293 CmdLine(const CmdLine&); ///< Intentionally not implemented. 00294 CmdLine& operator=(const CmdLine&); ///< Intentionally not implemented. 00295 00296 // ----------------------------------------------------------------------- 00297 // member variables 00298 protected: 00299 00300 XorHandler _xorHandler; ///< Customized XorHandler. 00301 std::string _name; ///< Program name. 00302 std::string _project; ///< Name of project. 00303 std::vector<std::string> _examples; ///< Program usage example. 00304 std::string _copyright; ///< Program copyright. 00305 std::string _license; ///< Program license. 00306 std::string _contact; ///< Contact information. 00307 00308 }; // class CmdLine 00309 00310 00311 } // namespace basis 00312 00313 00314 #endif // _BASIS_CMDLINE_H