121 struct OptionCompare {
122 using is_transparent = void;
123 bool operator()(
const std::string& lhs,
const std::string& rhs)
const noexcept
127 bool operator()(
const std::string& lhs, std::string_view rhs)
const noexcept
131 bool operator()(std::string_view lhs,
const std::string& rhs)
const noexcept
137 using Data = std::variant<Value, Help, NoOp, Switch>;
139 struct OptionData :
RefCount<OptionData, ThreadUnsafePolicy> {
140 template <
typename DataT>
141 OptionData(
char short_opt,
const std::string& long_opt, DataT
data, std::string description)
144 , description{std::move(description)}
147 this->short_opt.push_back(short_opt);
150 const std::string& opt()
const noexcept {
return long_opt.empty() ? short_opt : long_opt; }
152 std::string short_opt, long_opt;
154 std::string description;
157 using OptionDataPtr = boost::intrusive_ptr<OptionData>;
160 explicit Options(std::string description =
"");
162 template <
typename DataT>
164 std::string description =
"")
166 this->operator()(0, long_opt, std::forward<DataT>(option_data), std::move(description));
170 template <
typename DataT>
172 std::string description =
"")
174 auto opt_data = make_intrusive<OptionData>(short_opt, long_opt, std::forward<DataT>(
data),
175 std::move(description));
176 help_.push_back(opt_data);
178 const bool inserted = opts_.emplace(opt_data->short_opt, opt_data).second;
180 throw std::runtime_error{
"attempting to register duplicate option "
181 + opt_data->short_opt};
184 if (!long_opt.empty()) {
185 const bool inserted = opts_.emplace(long_opt, opt_data).second;
187 throw std::runtime_error{
"attempting to register duplicate option " + long_opt};
193 template <
typename DataT>
196 positional_.push_back(std::move(
data));
200 template <
typename DataT>
203 this->operator()(short_opt,
"", std::forward<DataT>(option_data), std::move(description));
207 bool operator[](
const std::string& long_opt)
const noexcept;
208 bool operator[](
char short_opt)
const noexcept;
209 void parse(
int argc,
const char*
const argv[]);
214 std::string description_;
215 using HelpVec = std::vector<OptionDataPtr>;
217 using OptsMap = std::map<std::string, OptionDataPtr, OptionCompare>;
219 using DataVec = std::vector<Data>;