43 class VSG_DECLSPEC CommandLine
46 CommandLine(
int* argc,
char** argv);
48 int& argc() {
return *_argc; }
49 const int& argc()
const {
return *_argc; }
51 char** argv() {
return _argv; }
52 const char*
const* argv()
const {
return _argv; }
54 char* operator[](
int i) {
return _argv[i]; }
55 const char* operator[](
int i)
const {
return _argv[i]; }
58 bool read(
int& i, T& v)
60 const int num_args = *_argc;
61 if (i >= num_args)
return false;
63 if constexpr (std::is_same_v<T, std::string>)
68 if constexpr (std::is_same_v<T, vsg::Path>)
75 std::size_t num_elements = type_num_elements(v);
78 if (num_elements == 1)
86 for (; num_elements > 0 && i < num_args; --num_elements, ++i)
96 return (!_istr.fail());
100 void remove(
int i,
int num)
102 if (i >= *_argc)
return;
104 int source = i + num;
105 if (source >= *_argc)
113 for (; source < *_argc; ++i, ++source)
115 _argv[i] = _argv[source];
121 _argv[*_argc] =
nullptr;
124 template<
typename... Args>
125 bool read(
const std::string& match, Args&... args)
127 for (
int i = 1; i < *_argc; ++i)
129 if (match == _argv[i])
135 bool result = (read(i, args) && ...);
139 remove(start, i - start);
143 std::string parameters = (match + ... + space_type_name(args));
144 std::string errorMessage = std::string(
"Failed to match command line required parameters for ") + parameters;
145 _errorMessages.push_back(errorMessage);
154 template<
typename... Args>
155 bool read(std::initializer_list<std::string> matches, Args&... args)
158 for (
auto str : matches) result = read(str, args...) | result;
162 template<
typename T,
typename... Args>
163 T value(T defaultValue,
const std::string& match, Args&... args)
166 read(match, args..., v);
170 template<
typename T,
typename... Args>
171 T value(T defaultValue, std::initializer_list<std::string> matches, Args&... args)
174 read(matches, args..., v);
179 bool readAndAssign(
const std::string& match,
Options* options)
181 if constexpr (std::is_same_v<T, void>)
183 if (options && read(std::string(
"--") + match))
192 if (options && read(std::string(
"--") + match, v))
204 using Messages = std::vector<std::string>;
205 bool errors()
const {
return !_errorMessages.empty(); }
207 Messages& getErrorMessages() {
return _errorMessages; }
208 const Messages& getErrorMessages()
const {
return _errorMessages; }
210 int writeErrorMessages(std::ostream& out)
const
212 if (_errorMessages.empty())
return 1;
213 for (
const auto& message : _errorMessages) out << message << std::endl;
220 std::istringstream _istr;
221 Messages _errorMessages;
234 if (std::strcmp(str,
"true") == 0 || std::strcmp(str,
"True") == 0 || std::strcmp(str,
"TRUE") == 0 || std::strcmp(str,
"1") == 0)
241 if (std::strcmp(str,
"false") == 0 || std::strcmp(str,
"False") == 0 || std::strcmp(str,
"FALSE") == 0 || std::strcmp(str,
"0") == 0)