Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnCommandLine.h
Go to the documentation of this file.
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnCommandLine.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
42 #pragma once
43 
44 #define DRWN_STANDARD_OPTIONS_USAGE \
45  " -help :: display application usage\n" \
46  " -config <xml> :: configure Darwin from XML file\n" \
47  " -set <m> <n> <v> :: set (configuration) <m>::<n> to value <v>\n" \
48  " -profile :: profile code\n" \
49  " -quiet :: only show warnings and errors\n" \
50  " -verbose :: show verbose messages\n" \
51  " -debug :: show debug messages\n" \
52  " -log <filename> :: log filename\n" \
53  " -threads <max> :: set maximum number of threads\n" \
54  " -randseed <n> :: seed random number generators rand and drand48\n"
55 
56 #define DRWN_PROCESS_STANDARD_OPTIONS(ARGS, ARGC) \
57  if (!strcmp(*ARGS, "-config")) { \
58  if (ARGC == 1) { \
59  drwnConfigurationManager::get().showRegistry(true); \
60  return 0; \
61  } \
62  drwnConfigurationManager::get().configure(*(++ARGS)); \
63  ARGC -= 1; \
64  } else if (!strcmp(*ARGS, "-set")) { \
65  if (ARGC == 1) { \
66  drwnConfigurationManager::get().showRegistry(false); \
67  return 0; \
68  } \
69  if (ARGC == 2) { \
70  drwnConfigurationManager::get().showModuleUsage(ARGS[1]); \
71  return 0; \
72  } \
73  DRWN_ASSERT_MSG(ARGC > 3, "not enough arguments for -set"); \
74  drwnConfigurationManager::get().configure(ARGS[1], ARGS[2], ARGS[3]); \
75  ARGC -= 3; ARGS += 3; \
76  } else if (!strcmp(*ARGS, "-profile")) { \
77  drwnCodeProfiler::enabled = true; \
78  } else if (!strcmp(*ARGS, "-quiet")) { \
79  drwnLogger::setLogLevel(DRWN_LL_WARNING); \
80  } else if (!strcmp(*ARGS, "-verbose") || !strcmp(*ARGS, "-v")) { \
81  if (drwnLogger::getLogLevel() < DRWN_LL_VERBOSE) \
82  drwnLogger::setLogLevel(DRWN_LL_VERBOSE); \
83  } else if (!strcmp(*ARGS, "-debug")) { \
84  drwnLogger::setLogLevel(DRWN_LL_DEBUG); \
85  } else if (!strcmp(*ARGS, "-log")) { \
86  drwnLogger::initialize(*(++ARGS)); \
87  ARGC -= 1; \
88  } else if (!strcmp(*ARGS, "-threads")) { \
89  drwnThreadPool::MAX_THREADS = atoi(*(++ARGS)); \
90  ARGC -= 1; \
91  } else if (!strcmp(*ARGS, "-randseed")) { \
92  const unsigned n = atoi(*(++ARGS)); \
93  srand(n); srand48(n); \
94  ARGC -= 1; \
95  }
96 
97 #define DRWN_BEGIN_CMDLINE_PROCESSING(ARGC, ARGV) \
98  drwnLogger::cacheCommandLine(ARGC, ARGV); \
99  char **_drwn_args = ARGV + 1; \
100  int _drwn_argc = ARGC; \
101  while (--_drwn_argc > 0) { \
102  DRWN_LOG_DEBUG("processing cmdline arg " << *_drwn_args << " (" << _drwn_argc << ")"); \
103  DRWN_PROCESS_STANDARD_OPTIONS(_drwn_args, _drwn_argc) \
104 
105 #define DRWN_CMDLINE_STR_OPTION(OPTSTR, VAR) \
106  else if (!strcmp(*_drwn_args, OPTSTR)) { \
107  VAR = *(++_drwn_args); _drwn_argc -= 1; }
108 
109 #define DRWN_CMDLINE_INT_OPTION(OPTSTR, VAR) \
110  else if (!strcmp(*_drwn_args, OPTSTR)) { \
111  VAR = atoi(*(++_drwn_args)); _drwn_argc -= 1; }
112 
113 #define DRWN_CMDLINE_REAL_OPTION(OPTSTR, VAR) \
114  else if (!strcmp(*_drwn_args, OPTSTR)) { \
115  VAR = atof(*(++_drwn_args)); _drwn_argc -= 1; }
116 
117 #define DRWN_CMDLINE_BOOL_OPTION(OPTSTR, VAR) \
118  else if (!strcmp(*_drwn_args, OPTSTR)) { VAR = true; }
119 
120 #define DRWN_CMDLINE_BOOL_TOGGLE_OPTION(OPTSTR, VAR) \
121  else if (!strcmp(*_drwn_args, OPTSTR)) { VAR = !VAR; }
122 
123 #define DRWN_CMDLINE_VEC_OPTION(OPTSTR, VAR) \
124  else if (!strcmp(*_drwn_args, OPTSTR)) { \
125  VAR.push_back(*(++_drwn_args)); _drwn_argc -= 1; }
126 
127 #define DRWN_CMDLINE_OPTION_BEGIN(OPTSTR, PTR) \
128  else if (!strcmp(*_drwn_args, OPTSTR)) { \
129  const char **PTR = (const char **)(_drwn_args + 1);
130 
131 #define DRWN_CMDLINE_OPTION_END(N) \
132  _drwn_args += (N); _drwn_argc -= (N); }
133 
134 #define DRWN_CMDLINE_FLAG_BEGIN(OPTSTR) \
135  else if (!strcmp(*_drwn_args, OPTSTR)) {
136 
137 #define DRWN_CMDLINE_FLAG_END }
138 
139 #define DRWN_END_CMDLINE_PROCESSING(USAGE) \
140  else if (!strcmp(*_drwn_args, "-help")) { \
141  USAGE; \
142  return 0; \
143  } else if ((*_drwn_args)[0] == '-') { \
144  USAGE; \
145  DRWN_LOG_ERROR("unrecognized option " << *_drwn_args); \
146  return -1; \
147  } else { \
148  DRWN_LOG_DEBUG(_drwn_argc << " commandline arguments remaining"); \
149  break; \
150  } \
151  _drwn_args++; \
152  }
153 
154 #define DRWN_CMDLINE_ARGV _drwn_args
155 #define DRWN_CMDLINE_ARGC _drwn_argc