irccd  3.0.3
Typedefs | Functions
irccd::options Namespace Reference

C++ similar interface to getopt(3). More...

Typedefs

using pack = std::tuple< std::vector< std::string >, std::unordered_multimap< char, std::string > >
 

Functions

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
 

Detailed Description

C++ similar interface to getopt(3).

Typedef Documentation

◆ pack

using irccd::options::pack = typedef std::tuple< std::vector<std::string>, std::unordered_multimap<char, std::string> >

Store the positional arguments and options.

Function Documentation

◆ 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
InputItmust dereference a string type (literal, std::string_view or std::string)
Parameters
itthe first item
endthe next item
fmtthe 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
argcthe number of arguments
argvthe arguments
fmtthe 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
StringTypemust be either a std::string or std::string_view
Parameters
argsthe arguments
fmtthe format string
Returns
the result