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