C++ similar interface to getopt(3).
More...
|
using | pack = std::tuple< std::vector< std::string >, std::unordered_multimap< char, std::string > > |
|
|
template<typename InputIt > |
auto | parse (InputIt &&it, InputIt &&end, std::string_view fmt) -> pack |
|
template<typename String > |
auto | parse (std::initializer_list< String > args, std::string_view fmt) -> pack |
|
auto | parse (int argc, char **argv, std::string_view fmt) -> pack |
|
C++ similar interface to getopt(3).
◆ pack
using irccd::options::pack = typedef std::tuple< std::vector<std::string>, std::unordered_multimap<char, std::string> > |
Store the positional arguments and options.
◆ parse() [1/3]
template<typename InputIt >
auto irccd::options::parse |
( |
InputIt && |
it, |
|
|
InputIt && |
end, |
|
|
std::string_view |
fmt |
|
) |
| -> pack
|
|
inline |
Parse a collection of options and arguments.
This function uses the same format as getopt(3) function, you need specify each option in the fmt string and add a colon after the option character if it requires a value.
If a – option appears in the argument list, it stops option parsing and all next tokens are considered arguments even if they start with an hyphen.
If the exlamation mark appears in the fmt argument, the function will stop parsing tokens immediately when one argument is not an option.
This function explicitly takes references to it and end parameters to allow the user to determine the number of tokens actually parsed.
Example of format strings:
- "abc": are all three boolean options,
- "c:v": v is a boolean option c requires a value.
Example of invocation:
mycli -v -a
: is similar to -va
if both 'v' and 'a' are boolean options,
mycli -v -- -c
: -c will be a positional argument rather than an option but '-v' is still an option.
- Template Parameters
-
InputIt | must dereference a string type (literal, std::string_view or std::string) |
- Parameters
-
it | the first item |
end | the next item |
fmt | the format string |
- Returns
- the result
◆ parse() [2/3]
auto irccd::options::parse |
( |
int |
argc, |
|
|
char ** |
argv, |
|
|
std::string_view |
fmt |
|
) |
| -> pack
|
|
inline |
Convenient overload for main() arguments.
- Parameters
-
argc | the number of arguments |
argv | the arguments |
fmt | the format string |
- Returns
- the result
◆ parse() [3/3]
template<typename String >
auto irccd::options::parse |
( |
std::initializer_list< String > |
args, |
|
|
std::string_view |
fmt |
|
) |
| -> pack
|
|
inline |
Convenient overload with an initializer_list.
- Template Parameters
-
StringType | must be either a std::string or std::string_view |
- Parameters
-
args | the arguments |
fmt | the format string |
- Returns
- the result