20 #ifndef MIRTK_Options_H 21 #define MIRTK_Options_H 23 #include "mirtk/Path.h" 24 #include "mirtk/Stream.h" 26 #include "mirtk/Parallel.h" 27 #include "mirtk/Profiling.h" 28 #include "mirtk/Terminal.h" 30 #include "mirtk/CommonExport.h" 100 MIRTK_Common_EXPORT
extern int verbose;
105 MIRTK_Common_EXPORT
extern int debug;
118 void PrintStandardOptions(ostream &);
121 void PrintCommonOptions(ostream &);
129 #define Warning(msg) \ 130 cerr << "Warning: " << msg << endl 134 #define FatalError(msg) \ 136 cerr << "Error: " << msg << endl; \ 141 #define EXECNAME BaseName(argv[0]).c_str() 143 #define OPTIDX ARGIDX 144 #define OPTNAME _option 145 #define DISCARD_PARSED_POSARGS() _discard_parsed_posargs = true 146 #define DISCARD_PARSED_OPTIONS() _discard_parsed_options = true 147 #define DISCARD_PARSED_ARGUMENTS() _discard_parsed_options = _discard_parsed_posargs = true 148 #define KEEP_PARSED_POSARGS() _discard_parsed_posargs = false 149 #define KEEP_PARSED_OPTIONS() _discard_parsed_options = false 150 #define KEEP_PARSED_ARGUMENTS() _discard_parsed_options = _discard_parsed_posargs = false 151 #define ALL_ARGUMENTS int ARGIDX = 1; ARGIDX < argc; ARGIDX++ 152 #define ALL_POSARGS int ARGIDX = 1; ARGIDX <= _numposarg; ARGIDX++ 153 #define OPTIONAL_POSARGS int ARGIDX = _posargc + 1; ARGIDX < argc && argv[ARGIDX][0] != '-'; ARGIDX++ 154 #define ARGUMENTS_AFTER(pos) int ARGIDX = (pos)+1; ARGIDX < argc; ARGIDX++ 155 #define ALL_OPTIONS ARGUMENTS_AFTER(_numposarg == -1 ? _posargc : _numposarg) 156 #define POSARG(i) _GetPositionalArgument((i), argc, argv) 157 #define NUM_POSARGS (_numposarg == -1 ? _GetNumberOfPositionalArguments(argc, argv) : _numposarg) 158 #define IS_OPTION _IsOption(OPTIDX, argc, argv) 159 #define OPTION(opt) _IsOption(OPTIDX, argc, argv, opt) 160 #define ARGUMENT _GetOptionArgument(OPTIDX, argc, argv) 161 #define HAS_ARGUMENT _IsArgument(OPTIDX, argc, argv) 162 #define HELP_OPTION (OPTION("-h") || OPTION("-help") || OPTION("--help")) 163 #define VERSION_OPTION (OPTION("-version") || OPTION("--version") || OPTION("-revision")) 164 #define STANDARD_OPTION IsStandardOption (argv[OPTIDX]) 165 #define PARALLEL_OPTION IsParallelOption (argv[OPTIDX]) 166 #define PROFILING_OPTION IsProfilingOption (argv[OPTIDX]) 167 #define TERMINAL_OPTION IsTerminalOption (argv[OPTIDX]) 168 #define PARSE_STANDARD_OPTION() ParseStandardOption (OPTIDX, argc, argv) 169 #define PARSE_PARALLEL_OPTION() ParseParallelOption (OPTIDX, argc, argv) 170 #define PARSE_PROFILING_OPTION() ParseProfilingOption(OPTIDX, argc, argv) 171 #define PARSE_TERMINAL_OPTION() ParseTerminalOption (OPTIDX, argc, argv) 172 #define PARSE_ARGUMENT(value) _ParseArgument(OPTNAME, ARGUMENT, value) 175 #define REQUIRES_POSARGS(n) \ 178 for (ALL_ARGUMENTS) { \ 179 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 180 else if (VERSION_OPTION) HANDLE_VERSION_OPTION(); \ 182 if (argc <= _posargc) { \ 183 PrintHelp(EXECNAME); \ 186 _numposarg = NUM_POSARGS; \ 187 if (_numposarg < _posargc) _numposarg = _posargc; \ 191 #define EXPECTS_POSARGS(n) \ 192 REQUIRES_POSARGS(n); \ 194 if (NUM_POSARGS > n) { \ 195 PrintHelp(EXECNAME); \ 197 cerr << "Error: Too many positional arguments!" << endl; \ 203 #define HANDLE_HELP_OPTION() \ 204 do { PrintHelp(EXECNAME); exit(0); } while (false) 207 #define HANDLE_VERSION_OPTION() \ 208 PARSE_STANDARD_OPTION() 211 #define HANDLE_UNKNOWN_OPTION() \ 213 PrintHelp(EXECNAME); \ 215 cerr << "Error: Unknown option " << argv[OPTIDX] << " (index=" << OPTIDX << ")" << endl; \ 220 #define HANDLE_STANDARD_OPTION() \ 221 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 222 else if (VERSION_OPTION ) HANDLE_VERSION_OPTION(); \ 223 else if (STANDARD_OPTION ) PARSE_STANDARD_OPTION() 226 #define HANDLE_STANDARD_OR_UNKNOWN_OPTION() \ 227 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 228 else if (VERSION_OPTION ) HANDLE_VERSION_OPTION(); \ 229 else if (STANDARD_OPTION ) PARSE_STANDARD_OPTION(); \ 230 else HANDLE_UNKNOWN_OPTION() 233 #define HANDLE_BOOLEAN_OPTION(name, var) \ 234 if (OPTION("-" name)) { \ 235 if (HAS_ARGUMENT) PARSE_ARGUMENT(var); \ 237 } else if (OPTION("-no" name)) \ 241 #define HANDLE_BOOL_OPTION(name) \ 242 HANDLE_BOOLEAN_OPTION(#name, name) 245 #define HANDLE_COMMON_OPTION() \ 246 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 247 else if (VERSION_OPTION ) HANDLE_VERSION_OPTION(); \ 248 else if (STANDARD_OPTION ) PARSE_STANDARD_OPTION(); \ 249 else if (PARALLEL_OPTION ) PARSE_PARALLEL_OPTION(); \ 250 else if (PROFILING_OPTION) PARSE_PROFILING_OPTION(); \ 251 else if (TERMINAL_OPTION ) PARSE_TERMINAL_OPTION() 254 #define HANDLE_COMMON_OR_UNKNOWN_OPTION() \ 255 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 256 else if (VERSION_OPTION ) HANDLE_VERSION_OPTION(); \ 257 else if (STANDARD_OPTION ) PARSE_STANDARD_OPTION(); \ 258 else if (PARALLEL_OPTION ) PARSE_PARALLEL_OPTION(); \ 259 else if (PROFILING_OPTION) PARSE_PROFILING_OPTION(); \ 260 else if (TERMINAL_OPTION ) PARSE_TERMINAL_OPTION(); \ 261 else HANDLE_UNKNOWN_OPTION() 264 #define HANDLE_HELP_OR_VERSION() \ 266 for (ALL_ARGUMENTS) { \ 267 if (HELP_OPTION ) HANDLE_HELP_OPTION(); \ 268 else if (VERSION_OPTION) HANDLE_VERSION_OPTION(); \ 276 MIRTK_Common_EXPORT
extern int _posargc;
277 MIRTK_Common_EXPORT
extern int _numposarg;
278 MIRTK_Common_EXPORT
extern bool _discard_parsed_posargs;
279 MIRTK_Common_EXPORT
extern bool _discard_parsed_options;
280 MIRTK_Common_EXPORT
extern const char *_option;
282 int _GetNumberOfPositionalArguments(
int,
char *[]);
284 void _DiscardArgument (
int &,
int &,
char *[]);
285 bool _IsOption (
int &,
int &,
char *[],
const char * = NULL);
286 bool _IsArgument (
int,
int &,
char *[]);
287 char *_GetPositionalArgument(
int,
int &,
char *[]);
288 char *_GetOptionArgument (
int &,
int &,
char *[]);
292 void _ParseArgument(
const char *opt,
const char *arg, T &value)
295 FatalError(
"Invalid " << opt <<
" argument: " << arg);
302 #endif // MIRTK_Options_H void ParseStandardOption(int &, int &, char *[])
Parse standard option.
MIRTK_Common_EXPORT int verbose
Verbosity of output messages.
MIRTK_Common_EXPORT int debug
bool IsStandardOption(const char *)
Check if given option is a standard option.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.