BASIS  version 1.2.3 (revision 2104)
CmdArgs.h
Go to the documentation of this file.
00001 /**
00002  * @file  CmdArgs.h
00003  * @brief Definition of commonly used command-line arguments.
00004  *
00005  * This include file mainly redefines the TCLAP command-line argument types
00006  * in the namespace of BASIS itself. It only defines commonly used argument
00007  * types without template parameters.
00008  *
00009  * Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00010  * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00011  *
00012  * Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00013  */
00014 
00015 #pragma once
00016 #ifndef _SBIA_BASIS_CMDARGS_H
00017 #define _SBIA_BASIS_CMDARGS_H
00018 
00019 
00020 #include <sbia/tclap/SwitchArg.h>
00021 #include <sbia/tclap/MultiSwitchArg.h>
00022 #include <sbia/tclap/UnlabeledValueArg.h>
00023 #include <sbia/tclap/UnlabeledMultiArg.h>
00024 
00025 #include <sbia/basis/ValueArg.h>
00026 #include <sbia/basis/MultiArg.h>
00027 
00028 #include <sbia/tclap/Constraint.h>
00029 
00030 #include <sbia/basis/path.h> // exists()
00031 
00032 
00033 namespace sbia
00034 {
00035 
00036 namespace basis
00037 {
00038 
00039 
00040 /// @addtogroup CxxCmdLine
00041 /// @{
00042 
00043 
00044 // ---------------------------------------------------------------------------
00045 // common
00046 /// Base type of command-line arguments.
00047 typedef TCLAP::Arg Arg;
00048 
00049 // ---------------------------------------------------------------------------
00050 // option switches
00051 
00052 /// Switch to enable/disable option.
00053 typedef TCLAP::SwitchArg SwitchArg;
00054 /// Counts occurrences of option switch.
00055 typedef TCLAP::MultiSwitchArg MultiSwitchArg;
00056 
00057 // ---------------------------------------------------------------------------
00058 // single argument option
00059 
00060 // Note: Use full namespace on the left side to help Doxygen to create
00061 //       the proper references to the BASIS ValueArg class.
00062 
00063 /// String argument.
00064 typedef sbia::basis::ValueArg<std::string> StringArg;
00065 /// Signed 32-bit integer argument.
00066 typedef sbia::basis::ValueArg<int> Int32Arg;
00067 /// Unsigned 32-bit integer argument.
00068 typedef sbia::basis::ValueArg<unsigned int> UInt32Arg;
00069 /// Signed 64-bit integer argument.
00070 typedef sbia::basis::ValueArg<long> Int64Arg;
00071 /// Unsigned 64-bit integer argument.
00072 typedef sbia::basis::ValueArg<unsigned long> UInt64Arg;
00073 /// Alias for Int32Arg.
00074 typedef sbia::basis::ValueArg<int> IntArg;
00075 /// Alias for UInt32Arg.
00076 typedef sbia::basis::ValueArg<unsigned int> UIntArg;
00077 /// Floating-point argument.
00078 typedef sbia::basis::ValueArg<float> FloatArg;
00079 /// Floating-point argument (double precision).
00080 typedef sbia::basis::ValueArg<double> DoubleArg;
00081 
00082 // ---------------------------------------------------------------------------
00083 // multiple arguments option
00084 
00085 // Note: Use full namespace on the left side to help Doxygen to create
00086 //       the proper references to the BASIS MultiArg class.
00087 
00088 /// String argument (multiple occurrences allowed).
00089 typedef sbia::basis::MultiArg<std::string> MultiStringArg;
00090 /// Signed 32-bit integer argument (multiple occurrences allowed).
00091 typedef sbia::basis::MultiArg<int> MultiInt32Arg;
00092 /// Unsigned 32-bit integer argument (multiple occurrences allowed).
00093 typedef sbia::basis::MultiArg<unsigned int> MultiUInt32Arg;
00094 /// Signed 64-bit integer argument (multiple occurrences allowed).
00095 typedef sbia::basis::MultiArg<long> MultiInt64Arg;
00096 /// Unsigned 64-bit integer argument (multiple occurrences allowed).
00097 typedef sbia::basis::MultiArg<unsigned long> MultiUInt64Arg;
00098 /// Floating-point argument (multiple occurrences allowed).
00099 typedef sbia::basis::MultiArg<float> MultiFloatArg;
00100 /// Floating-point argument (double precision, multiple occurrences allowed).
00101 typedef sbia::basis::MultiArg<double> MultiDoubleArg;
00102 /// Alias for MultiInt32Arg.
00103 typedef sbia::basis::MultiArg<int> MultiIntArg;
00104 /// Alias for MultiUInt32Arg.
00105 typedef sbia::basis::MultiArg<unsigned int> MultiUIntArg;
00106 
00107 // ---------------------------------------------------------------------------
00108 // positional arguments
00109 
00110 /**
00111  * @brief Positional argument.
00112  *
00113  * Processes only one positional argument. Add the positional arguments in
00114  * the right order to the command-line.
00115  *
00116  * @note The other unlabeled arguments as supported by TCLAP are intentionally
00117  *       not available through BASIS itself. Any non-string argument should
00118  *       more likely be a labeled argument, i.e., one with an option flag.
00119  */
00120 typedef TCLAP::UnlabeledValueArg<std::string> PositionalArg;
00121 
00122 /**
00123  * @brief Positional arguments.
00124  *
00125  * Use only one positional argument per command-line. Must be the last argument
00126  * added to the command-line as it is greedy and will aggregate all remaining
00127  * command-line arguments.
00128  *
00129  * @note The other unlabeled arguments as supported by TCLAP are intentionally
00130  *       not available through BASIS itself. Any non-string argument should
00131  *       more likely be a labeled argument, i.e., one with an option flag.
00132  */
00133 typedef TCLAP::UnlabeledMultiArg<std::string> PositionalArgs;
00134 
00135 // ===========================================================================
00136 // constraints
00137 // ===========================================================================
00138 
00139 // ---------------------------------------------------------------------------
00140 // constraints on enumerations
00141 // ---------------------------------------------------------------------------
00142 
00143 /**
00144  * @brief Constrains string arguments to allow only predefined values.
00145  */
00146 typedef TCLAP::ValuesConstraint<std::string> StringValuesConstraint;
00147 
00148 // ---------------------------------------------------------------------------
00149 // constraints on numbers
00150 // ---------------------------------------------------------------------------
00151 
00152 /**
00153  * @brief Constrain argument values to negative values.
00154  */
00155 template<typename T>
00156 class NegativeValueConstraint : public TCLAP::Constraint<T>
00157 {
00158 public:
00159     NegativeValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
00160     virtual ~NegativeValueConstraint() {}
00161     virtual std::string description() const { return "Value must be negative."; }
00162     virtual std::string shortID() const { return _typeDesc; }
00163     virtual bool check(const T& value) const { return value < 0; }
00164 protected:
00165     std::string _typeDesc;
00166 };
00167 
00168 /**
00169  * @brief Constrain argument values to zero or negative values.
00170  */
00171 template<typename T>
00172 class ZeroOrNegativeValueConstraint : public TCLAP::Constraint<T>
00173 {
00174 public:
00175     ZeroOrNegativeValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
00176     virtual ~ZeroOrNegativeValueConstraint() {}
00177     virtual std::string description() const { return "Value must be less or equal to zero."; }
00178     virtual std::string shortID() const { return _typeDesc; }
00179     virtual bool check(const T& value) const { return value <= 0; }
00180 protected:
00181     std::string _typeDesc;
00182 };
00183 
00184 /**
00185  * @brief Constrain argument values to non-zero values.
00186  */
00187 template<typename T>
00188 class NonZeroValueConstraint : public TCLAP::Constraint<T>
00189 {
00190 public:
00191     NonZeroValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
00192     virtual ~NonZeroValueConstraint() {}
00193     virtual std::string description() const { return "Value must not be zero."; }
00194     virtual std::string shortID() const { return _typeDesc; }
00195     virtual bool check(const T& value) const { return value != 0; }
00196 protected:
00197     std::string _typeDesc;
00198 };
00199 
00200 /**
00201  * @brief Constrain argument values to positive values.
00202  */
00203 template<typename T>
00204 class PositiveValueConstraint : public TCLAP::Constraint<T>
00205 {
00206 public:
00207     PositiveValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
00208     virtual ~PositiveValueConstraint() {}
00209     virtual std::string description() const { return "Value must be positive."; }
00210     virtual std::string shortID() const { return _typeDesc; }
00211     virtual bool check(const T& value) const { return value > 0; }
00212 protected:
00213     std::string _typeDesc;
00214 };
00215 
00216 /**
00217  * @brief Constrain argument values to zero or positive values.
00218  */
00219 template<typename T>
00220 class ZeroOrPositiveValueConstraint : public TCLAP::Constraint<T>
00221 {
00222 public:
00223     ZeroOrPositiveValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
00224     virtual ~ZeroOrPositiveValueConstraint() {}
00225     virtual std::string description() const { return "Value must be greater or equal to zero."; }
00226     virtual std::string shortID() const { return _typeDesc; }
00227     virtual bool check(const T& value) const { return value >= 0; }
00228 protected:
00229     std::string _typeDesc;
00230 };
00231 
00232 // ---------------------------------------------------------------------------
00233 // constraints on paths
00234 // ---------------------------------------------------------------------------
00235 
00236 /**
00237  * @brief Constrain argument values to paths of existing files.
00238  */
00239 class ExistingFileConstraint : public TCLAP::Constraint<std::string>
00240 {
00241 public:
00242     ExistingFileConstraint(const std::string& typeDesc = "<file>") : _typeDesc(typeDesc) {}
00243     virtual ~ExistingFileConstraint() {}
00244     virtual std::string description() const { return "Value must name an existing file."; }
00245     virtual std::string shortID() const { return _typeDesc; }
00246     virtual bool check(const std::string& value) const { return is_file(value); }
00247 protected:
00248     std::string _typeDesc;
00249 };
00250 
00251 /**
00252  * @brief Constrain argument values to paths of existing directories.
00253  */
00254 class ExistingDirectoryConstraint : public TCLAP::Constraint<std::string>
00255 {
00256 public:
00257     ExistingDirectoryConstraint(const std::string& typeDesc = "<dir>") : _typeDesc(typeDesc) {}
00258     virtual ~ExistingDirectoryConstraint() {}
00259     virtual std::string description() const { return "Value must name an existing directory."; }
00260     virtual std::string shortID() const { return _typeDesc; }
00261     virtual bool check(const std::string& value) const { return is_dir(value); }
00262 protected:
00263     std::string _typeDesc;
00264 };
00265 
00266 
00267 /// @}
00268 // end of Doxygen group
00269 
00270 
00271 } // namespace basis
00272 
00273 } // namespace sbia
00274 
00275 
00276 #endif // _SBIA_BASIS_CMDARGS_H