AliceVision
Photogrammetric Computer Vision Framework
cmdline.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include <aliceVision/system/Logger.hpp>
10 #include <aliceVision/system/Timer.hpp>
11 #include <aliceVision/system/hardwareContext.hpp>
12 
13 #include <boost/program_options/variables_map.hpp>
14 #include <boost/program_options/option.hpp>
15 #include <boost/program_options/errors.hpp>
16 #include <boost/program_options/parsers.hpp>
17 #include <boost/program_options/options_description.hpp>
18 
19 #include <functional>
20 #include <ostream>
21 
22 #define ALICEVISION_COMMANDLINE_START \
23  \
24  try \
25  { \
26  system::Timer commandLineTimer;
27 
28 #define ALICEVISION_COMMANDLINE_END \
29  \
30  ALICEVISION_LOG_INFO("Task done in (s): " + std::to_string(commandLineTimer.elapsed())); \
31  return EXIT_SUCCESS; \
32  } \
33  catch (std::exception & e) \
34  { \
35  ALICEVISION_CERR("================================================================================"); \
36  ALICEVISION_CERR("====================== Command line failed with an error ======================="); \
37  ALICEVISION_CERR(e.what()); \
38  ALICEVISION_CERR("================================================================================"); \
39  return EXIT_FAILURE; \
40  } \
41  catch (...) \
42  { \
43  ALICEVISION_CERR("================================================================================"); \
44  ALICEVISION_CERR("============== Command line failed with an unrecognized exception =============="); \
45  ALICEVISION_CERR("================================================================================"); \
46  return EXIT_FAILURE; \
47  }
48 
49 namespace aliceVision {
50 
51 template<class T>
52 std::function<void(T)> optInRange(T min, T max, const char* opt_name)
53 {
54  return [=](T v) {
55  if (v < min || v > max)
56  {
57  throw boost::program_options::validation_error(
58  boost::program_options::validation_error::invalid_option_value, opt_name, std::to_string(v));
59  }
60  };
61 };
62 } // namespace aliceVision
63 
64 namespace boost {
65 
66 inline std::ostream& operator<<(std::ostream& os, const boost::any& value)
67 {
68  bool is_char;
69  try
70  {
71  boost::any_cast<const char*>(value);
72  is_char = true;
73  }
74  catch (const boost::bad_any_cast&)
75  {
76  is_char = false;
77  }
78  bool is_str;
79  try
80  {
81  boost::any_cast<std::string>(value);
82  is_str = true;
83  }
84  catch (const boost::bad_any_cast&)
85  {
86  is_str = false;
87  }
88 
89  if (value.type() == typeid(int))
90  {
91  os << boost::any_cast<int>(value);
92  }
93  else if (value.type() == typeid(std::size_t))
94  {
95  os << boost::any_cast<std::size_t>(value);
96  }
97  else if (((boost::any)value).type() == typeid(bool))
98  {
99  os << boost::any_cast<bool>(value);
100  }
101  else if (value.type() == typeid(float))
102  {
103  os << boost::any_cast<float>(value);
104  }
105  else if (value.type() == typeid(double))
106  {
107  os << boost::any_cast<double>(value);
108  }
109  else if (is_char)
110  {
111  os << "\"" << boost::any_cast<const char*>(value) << "\"";
112  }
113  else if (is_str)
114  {
115  os << "\"" << boost::any_cast<std::string>(value) << "\"";
116  }
117  else
118  {
119  // Assumes that the only remainder is vector<string>
120  try
121  {
122  std::vector<std::string> vect = boost::any_cast<std::vector<std::string>>(value);
123  os << " = [";
124  if (!vect.empty())
125  {
126  os << vect[0];
127  for (int i = 1; i < static_cast<int>(vect.size()); ++i)
128  os << ", " << vect[i];
129  }
130  os << "]";
131  }
132  catch (const boost::bad_any_cast&)
133  {
134  os << " Unknown Type \"" << value.type().name() << "\"";
135  }
136  }
137  return os;
138 }
139 
140 namespace program_options {
141 
142 inline std::ostream& operator<<(std::ostream& os, const variables_map& vm)
143 {
144  for (const auto& v : vm)
145  {
146  const std::string& optionName = v.first;
147  const boost::program_options::variable_value& var = v.second;
148 
149  os << " * " << optionName << " = " << var.value();
150 
151  if (var.value().empty())
152  {
153  os << " (empty)";
154  }
155  if (vm[optionName].defaulted() || var.defaulted())
156  {
157  os << " (default)";
158  }
159  os << std::endl;
160  }
161  return os;
162 }
163 
164 } // namespace program_options
165 } // namespace boost
166 
167 namespace aliceVision {
168 
169 class CmdLine
170 {
171  public:
172  CmdLine(const std::string& name)
173  : _allParams(name)
174  {}
175 
176  void add(const boost::program_options::options_description& options) { _allParams.add(options); }
177 
178  bool execute(int argc, char** argv);
179 
180  HardwareContext getHardwareContext() { return _hContext; }
181 
182  private:
183  boost::program_options::options_description _allParams;
184  HardwareContext _hContext;
185 };
186 
187 } // namespace aliceVision
aliceVision::HardwareContext
Definition: hardwareContext.hpp:14
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::CmdLine
Definition: cmdline.hpp:169