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

Abstract base class for regular and multi-value arguments. More...

#include <ookii/command_line_argument.h>

Public Types

using parser_type = basic_command_line_parser< CharType, Traits, Alloc >
 The concrete type of basic_command_line_parser used.
 
using storage_type = details::argument_storage< CharType, Traits, Alloc >
 The concrete type of argument information storage used. For internal use.
 
using stream_type = std::basic_ostream< CharType, Traits >
 The concrete type of std::basic_ostream used.
 
using string_type = typename storage_type::string_type
 The concrete type of std::basic_string used.
 
using string_view_type = std::basic_string_view< CharType, Traits >
 The concrete type of std::basic_string_view used.
 

Public Member Functions

 command_line_argument_base (const command_line_argument_base &)=delete
 
const std::vector< string_type > & aliases () const noexcept
 Gets a list of aliases that can be used instead of the argument's name. More...
 
virtual void apply_default_value ()=0
 Sets the variable holding the argument's value to the default value. More...
 
bool cancel_parsing () const noexcept
 Gets a value that indicates whether supplying this argument will cancel parsing. More...
 
const string_typedescription () const noexcept
 Gets the long description of the argument. More...
 
virtual bool has_default_value () const noexcept=0
 Gets a value that indicates whether this argument has a default value. More...
 
bool has_long_name () const noexcept
 Gets a value that indicates whether the argument has a long name. More...
 
bool has_short_name () const noexcept
 Gets a value that indicates whether the argument has a short name. More...
 
bool has_value () const noexcept
 Gets a value that indicates whether the argument was specified on the last invocation of basic_command_line_parser::parse(). More...
 
virtual bool is_multi_value () const noexcept
 Gets a value that indicates whether the argument can be provided more than once, collecting all the specified values. More...
 
bool is_required () const noexcept
 Gets a value that indicates whether the argument is required. More...
 
virtual bool is_switch () const noexcept=0
 Gets a value that indicates whether the argument is a switch, which means it can be supplied without a value. More...
 
const string_typename () const noexcept
 Gets the name of the argument. More...
 
string_type name_with_prefix (const parser_type &parser) const
 Gets the argument name with the appropriate prefix. More...
 
command_line_argument_baseoperator= (const command_line_argument_base &)=delete
 
std::optional< size_t > position () const noexcept
 Gets the position of the argument. More...
 
virtual void reset ()
 Resets the argument to indicate it hasn't been set. More...
 
virtual set_value_result set_switch_value (parser_type &parser)=0
 Applies the implicit value for a switch argument. More...
 
virtual set_value_result set_value (string_view_type value, parser_type &parser)=0
 Sets the argument to the specified value. More...
 
const std::vector< CharType > & short_aliases () const noexcept
 Gets a list of short aliases that can be used instead of the argument's name. More...
 
CharType short_name () const noexcept
 Gets the short name of the argument, or a NULL character if it doesn't have one. More...
 
string_view_type short_or_long_name () const
 Gets the short name if the argument has one, otherwise the long name. More...
 
const string_typevalue_description () const noexcept
 Gets the value description for the argument. More...
 
virtual stream_typewrite_default_value (stream_type &stream) const =0
 Writes the default value to the specified stream. More...
 

Protected Member Functions

 command_line_argument_base (const parser_type &parser, storage_type &&storage)
 Constructs a command line argument base from a command_line_argument_storage. More...
 
const storage_typebase_storage () const
 Provides access to the storage fields to derived classes.
 
void set_value () noexcept
 Used to indicate that the argument has a value. More...
 

Detailed Description

template<typename CharType, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
class ookii::command_line_argument_base< CharType, Traits, Alloc >

Abstract base class for regular and multi-value arguments.

Template Parameters
CharTypeThe character type used for arguments and other strings.
TraitsThe character traits to use for strings. Defaults to std::char_traits<CharType>.
AllocThe allocator to use for strings. Defaults to std::allocator<CharType>.

This class provides the common functionality for both regular and multi-value arguments.

Constructor & Destructor Documentation

◆ command_line_argument_base()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
ookii::command_line_argument_base< CharType, Traits, Alloc >::command_line_argument_base ( const parser_type parser,
storage_type &&  storage 
)
inlineprotected

Constructs a command line argument base from a command_line_argument_storage.

Parameters
parserThe basic_command_line_parser this argument belongs to.
storageStorage containing the argument's information.

Member Function Documentation

◆ aliases()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
const std::vector<string_type>& ookii::command_line_argument_base< CharType, Traits, Alloc >::aliases ( ) const
inlinenoexcept

Gets a list of aliases that can be used instead of the argument's name.

An argument may have zero or more aliases.

Aliases can be added using the basic_parser_builder::argument_builder_common::alias() method.

◆ apply_default_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual void ookii::command_line_argument_base< CharType, Traits, Alloc >::apply_default_value ( )
pure virtual

Sets the variable holding the argument's value to the default value.

This method has no effect if the argument was supplied on the command line (has_value() returns true) or no default value was specified.

For a multi-value argument, the default value will be added as the only value in the container if no values were supplied on the command line.

The default value for an argument can be specified using basic_parser_builder::typed_argument_builder::default_value().

Implemented in ookii::action_command_line_argument< T, CharType, Traits, Alloc >, ookii::multi_value_command_line_argument< T, CharType, Traits, Alloc >, and ookii::command_line_argument< T, CharType, Traits, Alloc >.

◆ cancel_parsing()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
bool ookii::command_line_argument_base< CharType, Traits, Alloc >::cancel_parsing ( ) const
inlinenoexcept

Gets a value that indicates whether supplying this argument will cancel parsing.

If this returns true, if this argument is supplied, argument parsing will immediately return with parse_error::parsing_cancelled. Arguments after this one will not be evaluated.

This can be used, for example, to implement a -Help argument where usage help should be shown when supplied even if the rest of the command line is valid.

This value can be changed using the basic_parser_builder::argument_builder_common::cancel_parsing() method.

◆ description()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
const string_type& ookii::command_line_argument_base< CharType, Traits, Alloc >::description ( ) const
inlinenoexcept

Gets the long description of the argument.

The description is used when generating usage help, and is included in the list of descriptions after the usage syntax.

An argument without a description will not be included in the list of descriptions (but will still be included in the syntax).

This value can be changed using the basic_parser_builder::argument_builder_common::description() method.

◆ has_default_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual bool ookii::command_line_argument_base< CharType, Traits, Alloc >::has_default_value ( ) const
pure virtualnoexcept

Gets a value that indicates whether this argument has a default value.

Returns
true if the argument has a default value; otherwise, false.

Implemented in ookii::action_command_line_argument< T, CharType, Traits, Alloc >, ookii::multi_value_command_line_argument< T, CharType, Traits, Alloc >, and ookii::command_line_argument< T, CharType, Traits, Alloc >.

◆ has_long_name()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
bool ookii::command_line_argument_base< CharType, Traits, Alloc >::has_long_name ( ) const
inlinenoexcept

Gets a value that indicates whether the argument has a long name.

If not using parsing_mode::long_short, this is always true.

◆ has_short_name()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
bool ookii::command_line_argument_base< CharType, Traits, Alloc >::has_short_name ( ) const
inlinenoexcept

Gets a value that indicates whether the argument has a short name.

If not using parsing_mode::long_short, this is always false.

◆ has_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
bool ookii::command_line_argument_base< CharType, Traits, Alloc >::has_value ( ) const
inlinenoexcept

Gets a value that indicates whether the argument was specified on the last invocation of basic_command_line_parser::parse().

This allows you to determine if an argument was explicitly specified with a value that matches the default value.

◆ is_multi_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual bool ookii::command_line_argument_base< CharType, Traits, Alloc >::is_multi_value ( ) const
inlinevirtualnoexcept

Gets a value that indicates whether the argument can be provided more than once, collecting all the specified values.

An argument is multi-value if it was created using basic_parser_builder::add_multi_value_argument().

Reimplemented in ookii::multi_value_command_line_argument< T, CharType, Traits, Alloc >.

◆ is_required()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
bool ookii::command_line_argument_base< CharType, Traits, Alloc >::is_required ( ) const
inlinenoexcept

Gets a value that indicates whether the argument is required.

A required argument must be provided, otherwise parsing is not successful.

An argument can be made required using the basic_parser_builder::argument_builder_common::required() method.

◆ is_switch()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual bool ookii::command_line_argument_base< CharType, Traits, Alloc >::is_switch ( ) const
pure virtualnoexcept

Gets a value that indicates whether the argument is a switch, which means it can be supplied without a value.

An argument is a switch if its type is bool or std::optional<bool>.

Implemented in ookii::action_command_line_argument< T, CharType, Traits, Alloc >, ookii::multi_value_command_line_argument< T, CharType, Traits, Alloc >, and ookii::command_line_argument< T, CharType, Traits, Alloc >.

◆ name()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
const string_type& ookii::command_line_argument_base< CharType, Traits, Alloc >::name ( ) const
inlinenoexcept

Gets the name of the argument.

The argument's name is set using the basic_parser_builder::add_argument() method.

If using parsing_mode::long_short, and the argument has no long name, this is identical to the short name.

◆ name_with_prefix()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
string_type ookii::command_line_argument_base< CharType, Traits, Alloc >::name_with_prefix ( const parser_type parser) const
inline

Gets the argument name with the appropriate prefix.

Parameters
parserThe parser this argument belongs to.

◆ position()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
std::optional<size_t> ookii::command_line_argument_base< CharType, Traits, Alloc >::position ( ) const
inlinenoexcept

Gets the position of the argument.

Returns
The position, or std::nullopt if the argument is not positional.

A positional argument can be specified without supplying -Name before the value. Note that a positional argument can still be specified by name, as well as by position.

An argument can be made positional using the basic_parser_builder::argument_builder_common::positional() method.

◆ reset()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual void ookii::command_line_argument_base< CharType, Traits, Alloc >::reset ( )
inlinevirtual

Resets the argument to indicate it hasn't been set.

The reset() method is called on all arguments before parsing. After the call, the has_value() method will return false. This method does not affect the value of the variable holding the argument's value, except for multi-value arguments, where the container holding the values is cleared before parsing.

Reimplemented in ookii::multi_value_command_line_argument< T, CharType, Traits, Alloc >.

◆ set_switch_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual set_value_result ookii::command_line_argument_base< CharType, Traits, Alloc >::set_switch_value ( parser_type parser)
pure virtual

Applies the implicit value for a switch argument.

Parameters
parserThe parser that this argument belongs to.
Returns
One of the values of the set_value_result enumeration.

If the argument is a switch argument (is_switch() returns true), the variable holding its variable will be set to true. Otherwise, the value is not changed and set_value_result::error is returned.

For a multi-value switch argument, this adds a value of true to the container.

◆ set_value() [1/2]

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
void ookii::command_line_argument_base< CharType, Traits, Alloc >::set_value ( )
inlineprotectednoexcept

Used to indicate that the argument has a value.

After calling this method, has_value() will return true.

◆ set_value() [2/2]

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual set_value_result ookii::command_line_argument_base< CharType, Traits, Alloc >::set_value ( string_view_type  value,
parser_type parser 
)
pure virtual

Sets the argument to the specified value.

Parameters
valueThe string value of the argument.
parserThe parser that this argument belongs to.
Returns
One of the values of the set_value_result enumeration.

When the value is set, this method converts to the string value to the actual type of the argument using either the custom converter function (if any), or the default converter, which is the lexical_convert template.

For multi-value arguments, the new value will be added to the list of values.

◆ short_aliases()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
const std::vector<CharType>& ookii::command_line_argument_base< CharType, Traits, Alloc >::short_aliases ( ) const
inlinenoexcept

Gets a list of short aliases that can be used instead of the argument's name.

An argument may have zero or more short aliases.

If not using parsing_mode::long_short, this is always an empty collection.

Short aliases can be added using the basic_parser_builder::argument_builder_common::short_alias() method.

◆ short_name()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
CharType ookii::command_line_argument_base< CharType, Traits, Alloc >::short_name ( ) const
inlinenoexcept

Gets the short name of the argument, or a NULL character if it doesn't have one.

The argument's short name is set using the basic_parser_builder::argument_builder_common::short_name() method.

If not using parsing_mode::long_short, this is always false.

◆ short_or_long_name()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
string_view_type ookii::command_line_argument_base< CharType, Traits, Alloc >::short_or_long_name ( ) const
inline

Gets the short name if the argument has one, otherwise the long name.

Returns
A string representation of the short name, or the long name. The returned std::basic_string_view is valid only as long as this command_line_argument_base instance exists.

◆ value_description()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
const string_type& ookii::command_line_argument_base< CharType, Traits, Alloc >::value_description ( ) const
inlinenoexcept

Gets the value description for the argument.

The value description is a brief, often one-word description of the type of values that the argument accepts. It's used as part of the usage help's syntax.

This value defaults to the short type name (excluding namespace names) of the argument's type. The exact value may depend on your compiler.

This value can be changed using the basic_parser_builder::argument_builder_common::value_description() method.

◆ write_default_value()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual stream_type& ookii::command_line_argument_base< CharType, Traits, Alloc >::write_default_value ( stream_type stream) const
pure virtual

Writes the default value to the specified stream.

The default value for an argument can be specified using basic_parser_builder::typed_argument_builder::default_value().

Parameters
streamThe stream to write to.
Returns
The stream.

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