7 #ifndef JADE_ARGS_HPP__
8 #define JADE_ARGS_HPP__
10 #include "jade.error.hpp"
17 template <
typename TChar>
29 template <
typename TValue>
30 explicit basic_args(
const std::initializer_list<TValue> & values)
33 , is_flag_read (false)
36 auto iter = values.begin();
38 if (iter == values.end())
39 throw error(
"invalid command-line arguments");
41 while (++iter != values.end())
44 throw error(
"invalid command-line arguments");
46 _m.emplace_back(*iter);
59 , is_flag_read (false)
62 if (argc < 1 || argv ==
nullptr)
63 throw error(
"invalid command-line arguments");
65 for (
auto i = 1; i < argc; i++)
67 const auto s = argv[i];
69 throw error(
"invalid command-line arguments");
95 template <
typename TValue>
99 throw error(
"not enough arguments");
101 const auto value = _parse<TValue>(0);
102 _m.erase(_m.begin());
116 template <
typename TValue>
120 const TValue fallback = TValue())
122 assert(!is_flag_read);
124 const auto index = _find(long_name, short_name);
129 throw error() <<
"missing argument for option " << long_name;
131 const auto value = _parse<TValue>(
size_t(index + 1));
132 _m.erase(_m.begin() + index, _m.begin() + index + 2);
146 const auto index = _find(long_name, short_name);
154 _m.erase(_m.begin() + index);
167 throw error() <<
"unexpected option " << _m[0];
169 throw error() <<
"unexpected argument '" << _m[0] <<
"'";
180 if (_m.size() != length)
181 throw error(
"invalid syntax; try --help");
185 typedef std::basic_string<char_type> string_type;
186 typedef std::vector<string_type> vector_type;
201 for (
size_t i = 0; i < n; i++)
203 if (!_match(long_name, short_name, i))
206 for (
size_t j = i + 1; j < n; j++)
207 if (_match(long_name, short_name, j))
208 throw error() <<
"duplicate option for " << long_name;
222 const auto & s = _m[index];
223 return s == long_name || s == short_name;
227 template <
typename TValue>
228 TValue _parse(
const size_t index)
230 const auto & s = _m[index];
232 typedef std::basic_istringstream<char_type> stream_type;
237 throw error() <<
"invalid argument '" << s <<
"'";
246 typedef basic_args<char> args;
249 #endif // JADE_ARGS_HPP__