Ookii.CommandLine for C++
2.0.0
|
Parses command line arguments into strongly-typed values. More...
#include <ookii/command_line_parser.h>
Public Types | |
using | argument_base_type = command_line_argument_base< CharType, Traits, Alloc > |
The specialized type of command_line_argument_base used. | |
template<typename T > | |
using | argument_type = command_line_argument< T, CharType, Traits, Alloc > |
The specialized type of command_line_argument used. More... | |
template<typename T > | |
using | converter_type = typename argument_type< T >::typed_storage_type::converter_type |
The specialized type of the custom command line converter function used. More... | |
using | creation_options_type = details::creation_options< CharType, Traits, Alloc > |
The specialized type of parser creation options used. For internal use. | |
using | on_parsed_callback = std::function< on_parsed_action(argument_base_type &, std::optional< string_view_type > value)> |
The callback function type for on_parsed(). | |
using | result_type = parse_result< CharType, Traits, Alloc > |
The specialized type of parse_result used. | |
using | storage_type = details::parser_storage< CharType, Traits, Alloc > |
The specialized type of parser parameter storage used. For internal use. | |
using | string_provider_type = basic_localized_string_provider< CharType, Traits, Alloc > |
The specialized type of basic_localized_string_provider used. | |
using | string_type = typename argument_base_type::string_type |
The specialized type of std::basic_string used. | |
using | string_view_type = std::basic_string_view< CharType, Traits > |
The specialized type of std::basic_string_view used. | |
using | usage_writer_type = basic_usage_writer< CharType, Traits, Alloc > |
The specialized type of basic_usage_writer used. | |
Public Member Functions | |
template<typename Range > | |
basic_command_line_parser (const Range &arguments, storage_type &&storage, creation_options_type &options) | |
Creates a new instance of the basic_command_line_parser class. More... | |
bool | allow_duplicate_arguments () const noexcept |
Indicates whether duplicate arguments are allowed. More... | |
bool | allow_white_space_separator () const noexcept |
Indicates whether argument names and values can be separated by white space. More... | |
auto | argument_comparer () const |
Gets the comparer used for argument names. More... | |
size_t | argument_count () const |
Gets the total number of arguments. | |
CharType | argument_value_separator () const noexcept |
auto | arguments () const |
Gets a view of all the arguments defined by the parser. More... | |
const string_type & | command_name () const noexcept |
Returns the command name used when generating usage help. More... | |
const string_type & | description () const noexcept |
Returns the description used when generating usage help. More... | |
argument_base_type & | get_argument (size_t pos) |
Gets an argument by position. More... | |
const argument_base_type & | get_argument (size_t pos) const |
Gets an argument by position. More... | |
const argument_base_type * | get_argument (string_view_type name) const noexcept |
Gets an argument by name. More... | |
argument_base_type * | get_argument (string_view_type name) noexcept |
Gets an argument by name. More... | |
const argument_base_type * | get_help_argument () const noexcept |
Gets the help argument, if there is one. More... | |
const argument_base_type * | get_short_argument (CharType name) const noexcept |
Gets an argument by short name. More... | |
argument_base_type * | get_short_argument (CharType name) noexcept |
Gets an argument by short name. More... | |
bool | help_requested () const noexcept |
Gets a value that indicates help should be shown if parse() returns a parse_result with parse_error::parsing_cancelled. More... | |
void | help_requested (bool help_requested) noexcept |
Sets a value that indicates help should be shown if parse() returns a parse_result with parse_error::parsing_cancelled. More... | |
const std::locale & | locale () const noexcept |
Gets the locale used to parse argument values and to format strings. More... | |
const string_type & | long_prefix () const noexcept |
Gets the long argument prefix. More... | |
parsing_mode | mode () const noexcept |
Gets the parsing mode used by this parser. More... | |
void | on_parsed (on_parsed_callback callback) |
Sets a callback that will be invoked every time an argument is parsed. More... | |
template<typename Range > | |
result_type | parse (const Range &range) |
Parses the arguments in the specified range. More... | |
result_type | parse (int argc, const CharType *const argv[]) |
Parses the provided arguments. More... | |
result_type | parse (int argc, const CharType *const argv[], usage_writer_type *usage) |
Parses the provided arguments, and writes error and usage information to the console if a parsing error occurs. More... | |
template<typename Iterator > | |
result_type | parse (Iterator begin, Iterator end) |
Parses the arguments in the range specified by the iterators. More... | |
template<typename Iterator > | |
result_type | parse (Iterator begin, Iterator end, usage_writer_type *usage) |
Parses the arguments in the range specified by the iterators, and writes error and usage information to the console if a parsing error occurs. More... | |
template<typename Range > | |
result_type | parse (Range range, usage_writer_type *usage) |
Parses the arguments in the specified range, and writes error and usage information to the console if a parsing error occurs. More... | |
template<typename T > | |
result_type | parse (std::initializer_list< T > args) |
Parses the arguments in the specified initializer list. More... | |
template<typename T > | |
result_type | parse (std::initializer_list< T > args, usage_writer_type *usage) |
Parses the arguments in the specified initializer list, and writes error and usage information to the console if a parsing error occurs. More... | |
size_t | positional_argument_count () const |
Gets the number of positional arguments. More... | |
const std::vector< string_type > & | prefixes () const noexcept |
Gets a list of all the argument name prefixes accepted by the parser. More... | |
auto | short_argument_comparer () const |
Gets the comparer used for short argument names. More... | |
const string_provider_type & | string_provider () const noexcept |
Gets the basic_localized_string_provider implementation used to get strings for error messages etc. More... | |
void | write_usage (usage_writer_type *usage=nullptr, usage_help_request request=usage_help_request::full) |
Writes usage help for this parser's arguments. More... | |
Static Public Member Functions | |
static constexpr std::vector< string_type > | get_default_prefixes () |
Gets the default prefixes accepted by the parser. More... | |
static string_type | get_executable_name (int argc, const CharType *const argv[], bool include_extension=false) |
Extracts the executable name from the application's arguments. More... | |
Parses command line arguments into strongly-typed values.
The basic_command_line_parser class can parse a set of command line arguments in string form into a set of typed values. Which arguments are accepted and where their values will be stored is defined by the basic_parser_builder that created the basic_command_line_parser instance.
In addition, this class can generate detailed usage help for the defined arguments, which can be shown to the user in case an argument parsing error happens or help is requested.
To parse arguments, call one of the overloads of the parse() method. Those overloads taking a basic_usage_writer will handle any errors and print the error as well as usage information to the console. Most commonly, the overload you'll want to use is parse(int argc, const CharType *const argv[], usage_writer_type *usage).
Two typedefs for common character types are provided:
Type | Definition |
---|---|
ookii::command_line_parser | ookii::basic_command_line_parser<char> |
ookii::wcommand_line_parser | ookii::basic_command_line_parser<wchar_t> |
CharType | The character type used for arguments and other strings. Only char and wchar_t are supported. Defaults to wchar_t if _UNICODE is defined, otherwise to char . |
Traits | The character traits to use for strings. Defaults to std::char_traits<CharType> . |
Alloc | The allocator to use for strings. Defaults to std::allocator<CharType> . |
using ookii::basic_command_line_parser< CharType, Traits, Alloc >::argument_type = command_line_argument<T, CharType, Traits, Alloc> |
The specialized type of command_line_argument used.
T | The type of the argument. |
using ookii::basic_command_line_parser< CharType, Traits, Alloc >::converter_type = typename argument_type<T>::typed_storage_type::converter_type |
The specialized type of the custom command line converter function used.
T | The type of the argument. |
|
inline |
Creates a new instance of the basic_command_line_parser class.
Range | The type of a range containing basic_parser_builder::argument_builder instances. |
arguments | A range containing basic_parser_builder::argument_builder instances. |
storage | Parameters for the basic_command_line_parser. |
options | Creation options provided by the basic_parser_builder. |
|
inlinenoexcept |
Indicates whether duplicate arguments are allowed.
This value is set by basic_parser_builder::allow_duplicate_arguments()
|
inlinenoexcept |
Indicates whether argument names and values can be separated by white space.
This value is set by basic_parser_builder::allow_white_space_separator()
|
inline |
Gets the comparer used for argument names.
If the mode() method returns parsing_mode::long_short, this is the argument comparer for short names.
|
inlinenoexcept |
Indicates the non-whitespace separator used to separaet argument names and values.
This value is set by basic_parser_builder::argument_value_separator()
|
inline |
Gets a view of all the arguments defined by the parser.
The arguments will be returned in alphabetical order.
|
inlinenoexcept |
Returns the command name used when generating usage help.
This value is set by the basic_parser_builder::basic_parser_builder() constructor.
|
inlinenoexcept |
Returns the description used when generating usage help.
This value is set by basic_parser_builder::description().
|
inline |
Gets an argument by position.
pos | The argument's position. |
Only positional arguments can be retrieved using this method.
Positional arguments are created using basic_parser_builder::argument_builder_common::positional().
std::out_of_range | There is no argument at the specified position. |
|
inline |
Gets an argument by position.
pos | The argument's position. |
Only positional arguments can be retrieved using this method.
Positional arguments are created using basic_parser_builder::argument_builder_common::positional().
std::out_of_range | There is no argument at the specified position. |
|
inlinenoexcept |
Gets an argument by name.
Both the argument's main name and any of its aliases can be used.
name | The argument's name or alias. |
nullptr
if the argument was not found.
|
inlinenoexcept |
Gets an argument by name.
Both the argument's main name and any of its aliases can be used.
name | The argument's name or alias. |
nullptr
if the argument was not found.
|
inlinestaticconstexpr |
Gets the default prefixes accepted by the parser.
By default, the parser accepts '/' and '-' on Windows, and only '-' on other systems.
Which prefixes are accepted can be changed using basic_parser_builder::prefixes().
|
inlinestatic |
Extracts the executable name from the application's arguments.
This can be used to get the command name for the basic_parser_builder's constructor.
argc | The number of arguments. |
argv | An array containing the arguments. |
include_extension | true to include the file's extension; otherwise, false . |
argc
was zero.
|
inlinenoexcept |
Gets the help argument, if there is one.
nullptr
.
|
inlinenoexcept |
Gets an argument by short name.
Both the argument's main short name and any of its short aliases can be used.
name | The argument's short name or alias. |
nullptr
if the argument was not found.
|
inlinenoexcept |
Gets an argument by short name.
Both the argument's main short name and any of its short aliases can be used.
name | The argument's short name or alias. |
nullptr
if the argument was not found.
|
inlinenoexcept |
Gets a value that indicates help should be shown if parse() returns a parse_result with parse_error::parsing_cancelled.
After calling parse(), if the result was parse_error::none, this method always returns false
. If the result was an error other than parse_error::parsing_cancelled, it always returns true
.
Arguments cancelled using basic_parser_builder::argument_builder_common::cancel_parsing() automatically set this value to true
. Action arguments that cancelled parsing must explicitly do so.
|
inlinenoexcept |
Sets a value that indicates help should be shown if parse() returns a parse_result with parse_error::parsing_cancelled.
help_requested | The new value. |
|
inlinenoexcept |
Gets the locale used to parse argument values and to format strings.
This value is set by basic_parser_builder::locale()
|
inlinenoexcept |
Gets the long argument prefix.
By default, the parser accepts '–' for long argument names.
If not using parsing_mode::long_short, this value is not used and is always empty.
|
inlinenoexcept |
Gets the parsing mode used by this parser.
This value is set by the basic_parser_builder::mode() function.
|
inline |
Sets a callback that will be invoked every time an argument is parsed.
callback | The callback to be invoked. |
The callback must have the signature on_parsed_action(argument_base_type &arg, string_view_type value)
.
The callback will be invoked after the argument's value has been set. If the argument's value was invalid, it will not be invoked. The value may be an empty string if this is a switch argument.
The callback can control whether parsing continues with the returned on_parsed_action value.
Only one callback can be registered. Calling this function again will replace the previous callback.
|
inline |
Parses the arguments in the specified range.
Range | The type of the range. This type must define global begin() and end() functions. |
range | A range containing the arguments. |
|
inline |
Parses the provided arguments.
argv[0]
contains the application name, so this value is skipped when parsing.argc | The number of arguments. |
argv | An array containing the arguments. |
|
inline |
Parses the provided arguments, and writes error and usage information to the console if a parsing error occurs.
argv[0]
contains the application name, so this value is skipped when parsing.argc | The number of arguments. |
argv | An array containing the arguments. |
usage | The basic_usage_writer used for writing errors and usage help in the case of an error. Use {} to specify a default usage writer. |
|
inline |
Parses the arguments in the range specified by the iterators.
Iterator | The type of the iterator. This type must meet the requirements of a forward_iterator. |
begin | An iterator pointing to the first argument. |
end | An iterator pointing to directly after the last argument. |
|
inline |
Parses the arguments in the range specified by the iterators, and writes error and usage information to the console if a parsing error occurs.
Iterator | The type of the iterator. This type must meet the requirements of a forward_iterator. |
begin | An iterator pointing to the first argument. |
end | An iterator pointing to directly after the last argument. |
usage | The basic_usage_writer used for writing errors and usage help in the case of an error. Use {} to specify a default usage writer. |
|
inline |
Parses the arguments in the specified range, and writes error and usage information to the console if a parsing error occurs.
Range | The type of the range. This type must define global begin() and end() functions. |
range | A range containing the arguments. |
usage | The basic_usage_writer used for writing errors and usage help in the case of an error. Use {} to specify a default usage writer. |
|
inline |
Parses the arguments in the specified initializer list.
T | The type of the elements in the initializer list. This must be a string type that can be converted to std::basic_string<CharType, Traits, Alloc>. |
args | The arguments. |
|
inline |
Parses the arguments in the specified initializer list, and writes error and usage information to the console if a parsing error occurs.
T | The type of the elements in the initializer list. This must be a string type that can be converted to std::basic_string<CharType, Traits, Alloc>. |
args | The arguments. |
usage | The basic_usage_writer used for writing errors and usage help in the case of an error. Use {} to specify a default usage writer. |
|
inline |
Gets the number of positional arguments.
Positional arguments are created using basic_parser_builder::argument_builder_common::positional().
|
inlinenoexcept |
Gets a list of all the argument name prefixes accepted by the parser.
By default, the parser accepts '-' and '/' on Windows, and only '-' on other systems.
Which prefixes are accepted can be changed using basic_parser_builder::prefixes().
|
inline |
Gets the comparer used for short argument names.
If the mode() method does not return parsing_mode::long_short, this will never be used.
|
inlinenoexcept |
Gets the basic_localized_string_provider implementation used to get strings for error messages etc.
|
inline |
Writes usage help for this parser's arguments.
Usage will be written to basic_usage_writer::output. In the default basic_usage_writer, this is a basic_line_wrapping_ostream for the standard output stream.
usage | The basic_usage_writer used for creating the usage help. |
request | The parts of usage to write. |