arg_router  1.4.0
C++ command line argument parsing and routing
arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs > Class Template Reference

#include <arg_router/multi_lang/root.hpp>

Public Member Functions

 root_t (std::string_view language_id, const RootFactory &f)
 
void parse (vector< parsing::token_type > args) const
 
template<typename Iter , typename = std::enable_if_t<!std::is_same_v<std::decay_t<Iter>, int>>>
void parse (Iter begin, Iter end) const
 
template<typename Container , typename = std::enable_if_t< !std::is_same_v<std::decay_t<Container>, vector<parsing::token_type>>>>
void parse (const Container &c) const
 
void parse (int argc, char **argv) const
 
void help (std::ostream &stream) const
 
string help () const
 

Detailed Description

template<typename RootFactory, typename DefaultLanguageID, typename... SupportedLanguageIDs>
class arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >

Provides multi-language support by instantiating an arg_router::root_t for a given language ID.

This relies on the use of multi_lang::translation to provide compile-time selection of language variants for strings. The user needs to specialise multi_lang::translation for DefaultLanguageID and each supported ID in SupportedLanguageIDs.

namespace ar = arg_router;
namespace arp = ar::policy;
namespace ar::multi_lang
{
template <>
class translation<S_("en_GB")>
{
public:
using force = S_("force");
using force_description = S_("Force overwrite existing files");
...
};
template <>
class translation<S_("fr")>
{
public:
using force = S_("forcer");
using force_description = S_("Forcer l'écrasement des fichiers existants");
...
};
template <>
class translation<S_("ja")>
{
public:
using force = S_("強制");
using force_description = S_("既存のファイルを強制的に上書きする");
...
};
} // namespace ar::multi_lang
ar::multi_lang::root<S_("en_GB"), S_("fr"), S_("ja")>( //
ar::multi_lang::iso_locale(std::locale("").name()),
[&](auto tr_) {
using tr = decltype(tr_);
const auto common_args = ar::list{
ar::flag(arp::long_name<typename tr::force>,
arp::short_name<'f'>,
arp::description<typename tr::force_description>),
...
#define S_(tok)

As shown by the above example, the supported translation object is given to the function object that returns the root instance. This means that each use of translation specialisation must have the same number of language-specific compile-time strings.

RootFactory is a function object used to return a given root type from a supported language ID, its signature must be equivalent to:

template <typename ID>
auto operator()(multi_lang::translation<ID> I) {
...
};

The DefaultLanguageID and SupportedLanguageIDs parameters can be any set of unique compile-time strings, but as they are compared against an input that would usually come from std::locale(), then ISO language codes are least troublesome and easiest to read.

Template Parameters
RootFactoryFunction object type that accepts a multi_lang::translation specialisation and returns a root instance
DefaultLanguageIDThe default language ID as a compile time string, this is used if the runtime input code does not match this or any of SupportedLanguageIDs
SupportedLanguageIDsThe supported language IDs as compile time strings

Definition at line 90 of file root.hpp.

Constructor & Destructor Documentation

◆ root_t()

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::root_t ( std::string_view  language_id,
const RootFactory &  f 
)
inlineexplicit

Constructor

Parameters
language_idThe runtime language selection, if it doesn't match any of the SupportedLanguageIDs, then DefaultLanguageID is used
fFunction object that returns the root instance for a given supported language

Definition at line 113 of file root.hpp.

Member Function Documentation

◆ help() [1/2]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
string arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::help ( ) const
inline

Calls the help method on the selected root.

Returns
String holding the help output

Definition at line 195 of file root.hpp.

◆ help() [2/2]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
void arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::help ( std::ostream &  stream) const
inline

Calls the help method on the selected root.

Parameters
streamOutput stream to write into

Definition at line 186 of file root.hpp.

◆ parse() [1/4]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
template<typename Container , typename = std::enable_if_t< !std::is_same_v<std::decay_t<Container>, vector<parsing::token_type>>>>
void arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::parse ( const Container &  c) const
inline

Calls the parse method on the selected root.

The first element is not expected to be the executable name.

Note
This does not take part in overload resolution if c is a vector<parsing::token_type>
Template Parameters
Container
Parameters
cElements to parse

Definition at line 167 of file root.hpp.

◆ parse() [2/4]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
void arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::parse ( int  argc,
char **  argv 
) const
inline

Calls the parse method on the selected root.

Parameters
argcNumber of arguments
argvArray of char pointers to the command line tokens

Definition at line 177 of file root.hpp.

◆ parse() [3/4]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
template<typename Iter , typename = std::enable_if_t<!std::is_same_v<std::decay_t<Iter>, int>>>
void arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::parse ( Iter  begin,
Iter  end 
) const
inline

Calls the parse method on the selected root.

The first element is not expected to be the executable name.

Note
The strings must out live the parse process as they are not copied.
Template Parameters
IterIterator type to std::string_view convertible elements
Parameters
beginIterator to the first element
endIterator to the one-past-the-end element

Definition at line 151 of file root.hpp.

◆ parse() [4/4]

template<typename RootFactory , typename DefaultLanguageID , typename... SupportedLanguageIDs>
void arg_router::multi_lang::root_t< RootFactory, DefaultLanguageID, SupportedLanguageIDs >::parse ( vector< parsing::token_type args) const
inline

Calls the parse method on the selected root.

The first element is not expected to be the executable name.

Parameters
argsVector of tokens
Exceptions
parse_exceptionThrown if parsing has failed

Definition at line 137 of file root.hpp.


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