Ookii.CommandLine for C++  2.0.0
Public Member Functions | Protected Member Functions | List of all members
ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base Class Referenceabstract

Abstract base class with common functionality for the argument builders. More...

#include <ookii/command_line_builder.h>

Inheritance diagram for ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base:
Inheritance graph
[legend]

Public Member Functions

 argument_builder_base (argument_builder_base &)=delete
 Deleted copy constructor.
 
virtual ~argument_builder_base ()=default
 Default destructor.
 
template<typename Action >
action_argument_builder< details::first_argument_type< Action > > & add_action_argument (Action action, CharType short_name)
 Adds a new action argument. More...
 
template<typename Action >
action_argument_builder< details::first_argument_type< Action > > & add_action_argument (Action action, string_type name)
 Adds a new action argument. More...
 
template<typename T >
argument_builder< T > & add_argument (T &value, CharType short_name)
 Adds a new argument, and returns an argument_builder that can be used to further customize it. More...
 
template<typename T >
argument_builder< T > & add_argument (T &value, string_type name)
 Adds a new argument, and returns an argument_builder that can be used to further customize it. More...
 
template<typename T >
multi_value_argument_builder< T > & add_multi_value_argument (T &value, CharType short_name)
 Adds a new multi-value argument. More...
 
template<typename T >
multi_value_argument_builder< T > & add_multi_value_argument (T &value, string_type name)
 Adds a new multi-value argument. More...
 
action_argument_builder< bool > & add_version_argument (version_function function)
 Adds the standard version argument. More...
 
action_argument_builder< bool > & add_win32_version_argument ()
 Adds the standard version argument, using version information from the VERSIONINFO resource. More...
 
parser_type build ()
 Creates a basic_command_line_parser using the current options and arguments. More...
 
const string_typename () const noexcept
 Returns the name of the argument.
 
argument_builder_baseoperator= (argument_builder_base &)=delete
 
const CharType & short_name () const noexcept
 Returns the short name of the argument.
 
virtual std::unique_ptr< argument_base_typeto_argument (parser_type &parser)=0
 Converts the argument_builder_base into a command_line_argument_base that can be used by the basic_command_line_parser. More...
 

Protected Member Functions

 argument_builder_base (basic_parser_builder &basic_parser_builder, CharType short_name)
 Initializes a new instance of the argument_builder_base class. More...
 
 argument_builder_base (basic_parser_builder &basic_parser_builder, string_type name)
 Initializes a new instance of the argument_builder_base class. More...
 
size_t get_next_position ()
 Gets the next position for a positional argument.
 
storage_type & storage ()
 Provides access to the argument's options storage.
 

Detailed Description

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
class ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base

Abstract base class with common functionality for the argument builders.

This class contains functionality that doesn't depend on the argument's type.

Constructor & Destructor Documentation

◆ argument_builder_base() [1/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::argument_builder_base ( basic_parser_builder basic_parser_builder,
string_type  name 
)
inlineprotected

Initializes a new instance of the argument_builder_base class.

Parameters
basic_parser_builderA reference to the basic_parser_builder used to build this argument.
nameThe name of the argument.

◆ argument_builder_base() [2/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::argument_builder_base ( basic_parser_builder basic_parser_builder,
CharType  short_name 
)
inlineprotected

Initializes a new instance of the argument_builder_base class.

Parameters
basic_parser_builderA reference to the basic_parser_builder used to build this argument.
short_nameThe short name of the argument.

Member Function Documentation

◆ add_action_argument() [1/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename Action >
action_argument_builder<details::first_argument_type<Action> >& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_action_argument ( Action  action,
CharType  short_name 
)
inline

Adds a new action argument.

When using long/short mode, this method adds an argument that only has a short name.

When not using long/short mode, this method is identical to add_argument(T&,string_type) with a single character name.

See add_action_argument(Action,string_type) for more information.

Template Parameters
ActionThe type of a Callable object used for the action.
Parameters
actionThe action to invoke when the argument is supplied.
short_nameThe short name of the argument.
Returns
An action_argument_builder that can be used to further customize the argument.

◆ add_action_argument() [2/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename Action >
action_argument_builder<details::first_argument_type<Action> >& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_action_argument ( Action  action,
string_type  name 
)
inline

Adds a new action argument.

Action arguments are arguments that don't use a variable to store their value, but which invoke a function when supplied. This function must be a Callable object, such as a function pointer or lambda, which the following signature:

Parses command line arguments into strongly-typed values.
Definition: command_line_parser.h:106

Here, T is the the type of the argument, and the value will be converted to type T before the function is invoked. If T is a bool, the argument is a switch argument. Action arguments cannot be multi-value arguments. They also cannot use std::optional<T> or have a default value.

The function is invoked immediately as the argument is encountered, before the remainder of the command line is parsed. Return false from the function to cancel parsing, causing a parse_result with parse_error::parsing_cancelled to be returned.

Template Parameters
ActionThe type of a Callable object used for the action.
Parameters
actionThe action to invoke when the argument is supplied.
nameThe name of the argument.
Returns
An action_argument_builder that can be used to further customize the argument.

◆ add_argument() [1/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename T >
argument_builder<T>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_argument ( T &  value,
CharType  short_name 
)
inline

Adds a new argument, and returns an argument_builder that can be used to further customize it.

When using long/short mode, this method adds an argument that only has a short name.

When not using long/short mode, this method is identical to add_argument(T&,string_type) with a single character name.

See add_argument(T&,string_type) for more information.

Template Parameters
TThe type of the argument.
Parameters
valueA reference to the storage for the argument's value. When the argument is supplied, the converted value will be written to this reference.
short_nameThe short name of the argument, used to supply it on the command line.
Returns
An argument_builder that can be used to further customize the argument.

◆ add_argument() [2/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename T >
argument_builder<T>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_argument ( T &  value,
string_type  name 
)
inline

Adds a new argument, and returns an argument_builder that can be used to further customize it.

Any type T can be used as an argument type, as long as it meets the following criteria:

  • It must be possible to convert a string to type T, either using stream extraction (operator>>), or by a specialization of the lexical_convert template.
  • It must be possible to convert type T to a string using std::format (or libfmt if <format> is not available).

For custom argument types, you must provide either a stream extractor (operator>>) or specialize lexical_convert, and you must provide a specialization of std::formatter (fmt::formatter if libfmt is being used).

Providing an std::formatter is required so default values can be displayed in the usage help. A formatter must be provided even if you don't plan to set a default value, to prevent compiler errors (in that case, you could of course provide a non-functional, empty formatter).

Of the argument is of type std::optional<T>, the requirements apply to the contained type, not the std::optional<T> itself.

If type T is either bool or std::optional<bool>, the resulting argument will be a switch argument.

Template Parameters
TThe type of the argument.
Parameters
valueA reference to the storage for the argument's value. When the argument is supplied, the converted value will be written to this reference.
nameThe name of the argument, used to supply it on the command line.
Returns
An argument_builder that can be used to further customize the argument.

◆ add_multi_value_argument() [1/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename T >
multi_value_argument_builder<T>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_multi_value_argument ( T &  value,
CharType  short_name 
)
inline

Adds a new multi-value argument.

When using long/short mode, this method adds an argument that only has a short name.

When not using long/short mode, this method is identical to using add_multi_value_argument(T&,string_type) with a single character name.

See add_multi_value_argument(T&,string_type) for more information.

Template Parameters
TThe type of the argument's container.
Parameters
valueA reference to the container for the argument's values. When the argument is supplied, the converted value will be added to this container.
short_nameThe short name of the argument, used to supply it on the command line.
Returns
A multi_value_argument_builder that can be used to further customize the argument.

◆ add_multi_value_argument() [2/2]

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
template<typename T >
multi_value_argument_builder<T>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_multi_value_argument ( T &  value,
string_type  name 
)
inline

Adds a new multi-value argument.

Any container type T can be used, provided it meets the following requirements.

  • It defines a type T::value_type that meets the requirements outlined in the documentation for add_argument().
  • It defines the methods T::push_back() and T::clear()
Template Parameters
TThe type of the argument's container.
Parameters
valueA reference to the container for the argument's values. When the argument is supplied, the converted value will be added to this container.
nameThe name of the argument, used to supply it on the command line.
Returns
A multi_value_argument_builder that can be used to further customize the argument.

◆ add_version_argument()

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
action_argument_builder<bool>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_version_argument ( version_function  function)
inline

Adds the standard version argument.

This method adds an argument with the default name "Version", which invokes the specified function when supplied. The argument cancels parsing, but does not show usage help.

The argument will be called "version" (with a lowercase 'v') if the first letter of the first manually defined argument is lowercase.

You can specify a different name, as well as a custom description, using the basic_localized_string_provider class.

Parameters
functionA function that displays version information. This will be called when the argument is supplied.
Returns
An action_argument_builder that can be used to further customize the argument.

◆ add_win32_version_argument()

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
action_argument_builder<bool>& ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::add_win32_version_argument ( )
inline

Adds the standard version argument, using version information from the VERSIONINFO resource.

Warning
This function is only available when compiling for Windows.

This method adds an argument with the default name "Version", which when supplied will read the VERSION_INFO resource of the current executable, and print the product name, version and optionally copyright information to the standard output. If these resources don't exist, the text "Unknown version" is shown.

The argument will be called "version" (with a lowercase 'v') if the first letter of the first manually defined argument is lowercase.

You can specify a different name, as well as a custom description, using the basic_localized_string_provider class.

Returns
An action_argument_builder that can be used to further customize the argument.

◆ build()

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
parser_type ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::build ( )
inline

Creates a basic_command_line_parser using the current options and arguments.

Returns
A basic_command_line_parser that can be used to parse the arguments defined by the basic_parser_builder.

Since various values are moved out of the basic_parser_builder into the basic_command_line_parser, the basic_parser_builder should not be used after calling build().

◆ to_argument()

template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual std::unique_ptr<argument_base_type> ookii::basic_parser_builder< CharType, Traits, Alloc >::argument_builder_base::to_argument ( parser_type parser)
pure virtual

Converts the argument_builder_base into a command_line_argument_base that can be used by the basic_command_line_parser.

Parameters
parserThe basic_command_line_parser this argument belongs to.

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