Arachne 1.0
Arachne - the perpetual stitcher of Wikidata entities.
Loading...
Searching...
No Matches
anonymous_namespace{main.cpp}::options Class Referencefinal

Public Member Functions

 options (const std::vector< std::string > &arguments, const std::size_t first, const std::initializer_list< std::string_view > allowed, const std::initializer_list< std::string_view > flags={})
const std::string & require (const std::string_view name) const
std::optional< std::string > optional (const std::string_view name) const
bool flag (const std::string_view name) const
 options (const std::vector< std::string > &arguments, const std::size_t first, const std::initializer_list< std::string_view > allowed, const std::initializer_list< std::string_view > flags={})
const std::string & require (const std::string_view name) const
std::optional< std::string > optional (const std::string_view name) const
bool flag (const std::string_view name) const

Private Attributes

std::map< std::string, std::string, std::less<> > values_
std::set< std::string, std::less<> > present_flags_

Detailed Description

Definition at line 185 of file main.cpp.

Constructor & Destructor Documentation

◆ options() [1/2]

anonymous_namespace{main.cpp}::options::options ( const std::vector< std::string > & arguments,
const std::size_t first,
const std::initializer_list< std::string_view > allowed,
const std::initializer_list< std::string_view > flags = {} )
inline

Definition at line 187 of file main.cpp.

190 {}
191 ) {
192 std::set<std::string, std::less<>> allowed_values;
193 std::set<std::string, std::less<>> allowed_flags;
194 for (const auto value : allowed) {
195 allowed_values.emplace(value);
196 }
197 for (const auto value : flags) {
198 allowed_flags.emplace(value);
199 }
200 for (std::size_t index = first; index < arguments.size();) {
201 const std::string& name = arguments[index];
202 if (!name.starts_with("--")) {
203 throw cli_error("unexpected positional argument: " + name);
204 }
205 if (values_.contains(name) || present_flags_.contains(name)) {
206 throw cli_error("duplicate option: " + name);
207 }
208 if (allowed_flags.contains(name)) {
209 present_flags_.emplace(name);
210 ++index;
211 continue;
212 }
213 if (!allowed_values.contains(name)) {
214 throw cli_error("unknown option: " + name);
215 }
216 if (index + 1U >= arguments.size()
217 || arguments[index + 1U].starts_with("--")) {
218 throw cli_error("option requires a value: " + name);
219 }
220 values_.emplace(name, arguments[index + 1U]);
221 index += 2U;
222 }
223 }
std::map< std::string, std::string, std::less<> > values_
Definition main.cpp:248
std::set< std::string, std::less<> > present_flags_
Definition main.cpp:249

◆ options() [2/2]

anonymous_namespace{main.cpp}::options::options ( const std::vector< std::string > & arguments,
const std::size_t first,
const std::initializer_list< std::string_view > allowed,
const std::initializer_list< std::string_view > flags = {} )
inline

Definition at line 187 of file main.cpp.

190 {}
191 ) {
192 std::set<std::string, std::less<>> allowed_values;
193 std::set<std::string, std::less<>> allowed_flags;
194 for (const auto value : allowed) {
195 allowed_values.emplace(value);
196 }
197 for (const auto value : flags) {
198 allowed_flags.emplace(value);
199 }
200 for (std::size_t index = first; index < arguments.size();) {
201 const std::string& name = arguments[index];
202 if (!name.starts_with("--")) {
203 throw cli_error("unexpected positional argument: " + name);
204 }
205 if (values_.contains(name) || present_flags_.contains(name)) {
206 throw cli_error("duplicate option: " + name);
207 }
208 if (allowed_flags.contains(name)) {
209 present_flags_.emplace(name);
210 ++index;
211 continue;
212 }
213 if (!allowed_values.contains(name)) {
214 throw cli_error("unknown option: " + name);
215 }
216 if (index + 1U >= arguments.size()
217 || arguments[index + 1U].starts_with("--")) {
218 throw cli_error("option requires a value: " + name);
219 }
220 values_.emplace(name, arguments[index + 1U]);
221 index += 2U;
222 }
223 }

Member Function Documentation

◆ flag() [1/2]

bool anonymous_namespace{main.cpp}::options::flag ( const std::string_view name) const
inlinenodiscard

Definition at line 243 of file main.cpp.

243 {
244 return present_flags_.contains(name);
245 }

◆ flag() [2/2]

bool anonymous_namespace{main.cpp}::options::flag ( const std::string_view name) const
inlinenodiscard

Definition at line 243 of file main.cpp.

243 {
244 return present_flags_.contains(name);
245 }

◆ optional() [1/2]

std::optional< std::string > anonymous_namespace{main.cpp}::options::optional ( const std::string_view name) const
inlinenodiscard

Definition at line 235 of file main.cpp.

235 {
236 const auto value = values_.find(name);
237 if (value == values_.end()) {
238 return std::nullopt;
239 }
240 return value->second;
241 }

◆ optional() [2/2]

std::optional< std::string > anonymous_namespace{main.cpp}::options::optional ( const std::string_view name) const
inlinenodiscard

Definition at line 235 of file main.cpp.

235 {
236 const auto value = values_.find(name);
237 if (value == values_.end()) {
238 return std::nullopt;
239 }
240 return value->second;
241 }

◆ require() [1/2]

const std::string & anonymous_namespace{main.cpp}::options::require ( const std::string_view name) const
inlinenodiscard

Definition at line 226 of file main.cpp.

226 {
227 const auto value = values_.find(name);
228 if (value == values_.end() || value->second.empty()) {
229 throw cli_error("required option is missing: " + std::string(name));
230 }
231 return value->second;
232 }

◆ require() [2/2]

const std::string & anonymous_namespace{main.cpp}::options::require ( const std::string_view name) const
inlinenodiscard

Definition at line 226 of file main.cpp.

226 {
227 const auto value = values_.find(name);
228 if (value == values_.end() || value->second.empty()) {
229 throw cli_error("required option is missing: " + std::string(name));
230 }
231 return value->second;
232 }

Member Data Documentation

◆ present_flags_

std::set< std::string, std::less<> > anonymous_namespace{main.cpp}::options::present_flags_
private

Definition at line 249 of file main.cpp.

◆ values_

std::map< std::string, std::string, std::less<> > anonymous_namespace{main.cpp}::options::values_
private

Definition at line 248 of file main.cpp.


The documentation for this class was generated from the following file: