1 #ifndef OOKII_COMMAND_LINE_BUILDER_H_
6 #define OOKII_COMMAND_LINE_BUILDER_H_
16 template <
typename Ret,
typename Arg,
typename... Rest>
17 Arg first_argument_helper(Ret (*)(Arg, Rest...));
19 template <
typename Ret,
typename F,
typename Arg,
typename... Rest>
20 Arg first_argument_helper(Ret (F::*)(Arg, Rest...));
22 template <
typename Ret,
typename F,
typename Arg,
typename... Rest>
23 Arg first_argument_helper(Ret (F::*)(Arg, Rest...)
const);
26 decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
29 using first_argument_type = decltype(first_argument_helper(std::declval<T>()));
32 template<
typename CharType>
33 inline void show_win32_version()
35 auto info = details::get_version_resource();
38 console_stream<CharType>::cout() <<
"Unknown version." << std::endl;
42 console_stream<CharType>::cout() << info->product_name <<
" " << info->version << std::endl;
43 if (!info->copyright.empty())
45 console_stream<CharType>::cout() << info->copyright << std::endl;
71 template<
typename CharType = details::default_
char_type,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
74 using parser_storage_type = details::parser_storage<CharType, Traits, Alloc>;
75 using creation_options_type = details::creation_options<CharType, Traits, Alloc>;
115 using storage_type =
typename argument_base_type::storage_type;
140 template<
typename Action>
147 template<
typename Action>
173 #if defined(_WIN32) || defined(DOXYGEN)
184 return _parser_builder.
build();
190 return _storage.name;
196 return _storage.short_name;
222 _storage.has_long_name =
false;
234 return _parser_builder.get_next_position();
238 storage_type _storage;
247 template<
typename BuilderType>
262 this->
storage().has_long_name =
true;
263 return *
static_cast<BuilderType*
>(
this);
274 return *
static_cast<BuilderType*
>(
this);
284 return *
static_cast<BuilderType*
>(
this);
322 return *
static_cast<BuilderType*
>(
this);
337 return *
static_cast<BuilderType*
>(
this);
356 return *
static_cast<BuilderType*
>(
this);
367 this->
storage().is_required =
true;
368 return *
static_cast<BuilderType*
>(
this);
385 this->
storage().aliases.push_back(alias);
386 return *
static_cast<BuilderType*
>(
this);
399 this->
storage().short_aliases.push_back(alias);
400 return *
static_cast<BuilderType*
>(
this);
417 this->
storage().cancel_parsing =
true;
418 return *
static_cast<BuilderType*
>(
this);
428 template<
typename ArgumentType,
typename BuilderType>
432 using typed_storage_type =
typename ArgumentType::typed_storage_type;
433 using value_type =
typename ArgumentType::value_type;
434 using element_type =
typename ArgumentType::element_type;
435 using converter_type =
typename typed_storage_type::converter_type;
444 _typed_storage{value}
454 _typed_storage{value}
475 return *
static_cast<BuilderType*
>(
this);
490 this->_typed_storage.converter =
converter;
491 return *
static_cast<BuilderType*
>(
this);
495 virtual std::unique_ptr<argument_base_type> to_argument(
parser_type &parser)
override
500 return make_unique<ArgumentType>(parser, std::move(this->
storage()), std::move(_typed_storage));
503 typed_storage_type _typed_storage;
557 using converter_type =
typename typed_storage_type::converter_type;
569 _typed_storage{action}
579 _typed_storage{action}
586 this->_typed_storage.converter =
converter;
591 virtual std::unique_ptr<argument_base_type> to_argument(
parser_type &parser)
override
596 return make_unique<argument_type>(parser, std::move(this->
storage()), std::move(_typed_storage));
599 typed_storage_type _typed_storage;
608 : _storage{command_name, string_provider}
695 template<
typename Action>
699 _arguments.push_back(std::make_unique<argument_type>(*
this, name, action));
700 return *
static_cast<argument_type*
>(_arguments.back().get());
716 template<
typename Action>
720 _arguments.push_back(std::make_unique<argument_type>(*
this, short_name, action));
721 return *
static_cast<argument_type*
>(_arguments.back().get());
784 if (_version_argument !=
nullptr)
786 throw std::logic_error(
"Duplicate version argument.");
789 auto action = [
function = std::move(
function)](bool,
parser_type &)
796 auto &argument =
add_action_argument(action, _storage.string_provider->automatic_version_name())
797 .description(_storage.string_provider->automatic_version_description());
799 _version_argument = &argument;
803 #if defined(_WIN32) || defined(DOXYGEN)
836 if (_storage.prefixes.empty())
841 if (_storage.long_prefix.empty())
843 _storage.long_prefix = c_default_long_prefix.data();
848 _storage.long_prefix.clear();
852 if (_version_argument !=
nullptr)
854 auto it = std::find_if(_arguments.begin(), _arguments.end(), [
this](
auto &item) { return item.get() != _version_argument; });
855 if (it != _arguments.end())
857 auto name = _version_argument->
name();
858 if (std::isupper((*it)->name()[0], _storage.locale))
860 name[0] = std::toupper(name[0], _storage.locale);
864 name[0] = std::tolower(name[0], _storage.locale);
867 _version_argument->
name(name);
871 return parser_type{_arguments, std::move(_storage), _options};
897 _storage.locale = loc;
908 throw std::invalid_argument(
"mode");
911 _storage.mode =
mode;
929 template<
typename Range>
935 _storage.prefixes = std::vector<string_type>{ begin(
prefixes), end(
prefixes) };
954 return this->prefixes<std::initializer_list<T>>(
prefixes);
966 _storage.long_prefix = prefix;
983 _storage.allow_white_space_separator = allow;
1000 _storage.allow_duplicate_arguments = allow;
1017 _storage.argument_value_separator = separator;
1051 _options.automatic_help_argument = enable;
1065 _storage.show_usage_on_error = request;
1070 size_t get_next_position() noexcept
1072 return _next_position++;
1075 std::vector<std::unique_ptr<argument_builder_base>> _arguments;
1076 size_t _next_position{};
1077 action_argument_builder<bool> *_version_argument{};
1079 creation_options_type _options{};
1080 parser_storage_type _storage;
1082 static constexpr
auto c_default_long_prefix = literal_cast<CharType>(
"--");
Class that provides information about action arguments.
Definition: command_line_argument.h:700
details::action_argument_storage< value_type, CharType, Traits, Alloc > typed_storage_type
The concrete type of argument information storage used. For internal use.
Definition: command_line_argument.h:716
T value_type
The type of the argument's value, which equals T.
Definition: command_line_argument.h:705
typename typed_storage_type::function_type function_type
The type of the function used by this action argument.
Definition: command_line_argument.h:718
T element_type
The type of the argument's elements, which equals T.
Definition: command_line_argument.h:710
Parses command line arguments into strongly-typed values.
Definition: command_line_parser.h:106
command_line_argument_base< CharType, Traits, Alloc > argument_base_type
The specialized type of command_line_argument_base used.
Definition: command_line_parser.h:109
static constexpr std::vector< string_type > get_default_prefixes()
Gets the default prefixes accepted by the parser.
Definition: command_line_parser.h:144
basic_localized_string_provider< CharType, Traits, Alloc > string_provider_type
The specialized type of basic_localized_string_provider used.
Definition: command_line_parser.h:125
typename argument_base_type::string_type string_type
The specialized type of std::basic_string used.
Definition: command_line_parser.h:111
Specifies options for an action argument.
Definition: command_line_builder.h:551
action_argument_builder(basic_parser_builder &parser_builder, CharType short_name, function_type action)
Initializes a new instance of the argument_builder class.
Definition: command_line_builder.h:577
action_argument_builder(basic_parser_builder &parser_builder, string_type name, function_type action)
Initializes a new instance of the argument_builder class.
Definition: command_line_builder.h:567
typename argument_type::function_type function_type
The type of function used for the action.
Definition: command_line_builder.h:561
action_argument_builder & converter(converter_type converter)
Supplies a custom function to convert strings to the argument's type.
Definition: command_line_builder.h:584
Abstract base class with common functionality for the argument builders.
Definition: command_line_builder.h:114
action_argument_builder< bool > & add_win32_version_argument()
Adds the standard version argument, using version information from the VERSIONINFO resource.
Definition: command_line_builder.h:175
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.
Definition: command_line_builder.h:134
const CharType & short_name() const noexcept
Returns the short name of the argument.
Definition: command_line_builder.h:194
storage_type & storage()
Provides access to the argument's options storage.
Definition: command_line_builder.h:226
size_t get_next_position()
Gets the next position for a positional argument.
Definition: command_line_builder.h:232
parser_type build()
Creates a basic_command_line_parser using the current options and arguments.
Definition: command_line_builder.h:182
action_argument_builder< details::first_argument_type< Action > > & add_action_argument(Action action, string_type name)
Adds a new action argument.
Definition: command_line_builder.h:141
multi_value_argument_builder< T > & add_multi_value_argument(T &value, string_type name)
Adds a new multi-value argument.
Definition: command_line_builder.h:155
virtual ~argument_builder_base()=default
Default destructor.
action_argument_builder< bool > & add_version_argument(version_function function)
Adds the standard version argument.
Definition: command_line_builder.h:168
argument_builder_base(basic_parser_builder &basic_parser_builder, string_type name)
Initializes a new instance of the argument_builder_base class.
Definition: command_line_builder.h:208
multi_value_argument_builder< T > & add_multi_value_argument(T &value, CharType short_name)
Adds a new multi-value argument.
Definition: command_line_builder.h:162
argument_builder_base(argument_builder_base &)=delete
Deleted copy constructor.
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.
Definition: command_line_builder.h:127
virtual std::unique_ptr< argument_base_type > to_argument(parser_type &parser)=0
Converts the argument_builder_base into a command_line_argument_base that can be used by the basic_co...
action_argument_builder< details::first_argument_type< Action > > & add_action_argument(Action action, CharType short_name)
Adds a new action argument.
Definition: command_line_builder.h:148
argument_builder_base(basic_parser_builder &basic_parser_builder, CharType short_name)
Initializes a new instance of the argument_builder_base class.
Definition: command_line_builder.h:217
const string_type & name() const noexcept
Returns the name of the argument.
Definition: command_line_builder.h:188
Specifies options common to all argument types, for an argument under construction.
Definition: command_line_builder.h:249
const CharType & short_name() const noexcept
Returns the short name of the argument.
Definition: command_line_builder.h:294
BuilderType & description(string_type description)
Sets the long description for the argument.
Definition: command_line_builder.h:334
BuilderType & short_name(CharType short_name)
Sets an explicit short name for an argument.
Definition: command_line_builder.h:281
BuilderType & short_alias(CharType alias)
Adds a short alias to the argument.
Definition: command_line_builder.h:397
BuilderType & alias(string_type alias)
Adds an alias to the argument.
Definition: command_line_builder.h:383
BuilderType & name(string_type name)
Changes the name of the argument.
Definition: command_line_builder.h:259
const string_type & name() const noexcept
Returns the name of the argument.
Definition: command_line_builder.h:288
BuilderType & cancel_parsing()
Indicates that supplying this argument will cancel parsing.
Definition: command_line_builder.h:415
BuilderType & required()
Indicates that the argument is required.
Definition: command_line_builder.h:365
BuilderType & value_description(string_type value_description)
Sets the value description for the argument.
Definition: command_line_builder.h:319
BuilderType & short_name()
Sets a short name for the argument that matches the first character of the long name.
Definition: command_line_builder.h:271
BuilderType & positional()
Indicates that the argument can be specified by position.
Definition: command_line_builder.h:349
Specifies options for an argument, other than a multi-value or action argument.
Definition: command_line_builder.h:510
Specified options for a multi-value argument.
Definition: command_line_builder.h:521
multi_value_argument_builder & separator(CharType separator)
Specifies a separator that separates multiple values in a single argument value.
Definition: command_line_builder.h:540
Specifies options for a regular or multi-value argument under construction.
Definition: command_line_builder.h:430
typed_argument_builder(basic_parser_builder &basic_parser_builder, CharType short_name, value_type &value)
Initializes a new instance of the argument_builder class.
Definition: command_line_builder.h:452
BuilderType & converter(converter_type converter)
Supplies a custom function to convert strings to the argument's type.
Definition: command_line_builder.h:488
typed_argument_builder(basic_parser_builder &basic_parser_builder, string_type name, value_type &value)
Initializes a new instance of the argument_builder class.
Definition: command_line_builder.h:442
BuilderType & default_value(element_type default_value)
Sets a default value for the argument, which will be used if the argument is not supplied....
Definition: command_line_builder.h:472
Provides functionality to specify options and arguments to create a new basic_command_line_parser.
Definition: command_line_builder.h:73
basic_parser_builder & mode(parsing_mode mode)
Sets the command line parsing rules to use.
Definition: command_line_builder.h:904
basic_parser_builder & prefixes(const Range &prefixes)
Sets the argument name prefixes accepted by the basic_command_line_parser.
Definition: command_line_builder.h:930
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.
Definition: command_line_builder.h:666
basic_parser_builder & allow_whitespace_separator(bool allow) noexcept
Sets a value that indicates whether argument names and values can be separated by whitespace.
Definition: command_line_builder.h:981
basic_parser_builder & allow_duplicate_arguments(bool allow) noexcept
Sets a value that indicates whether arguments may be specified multiple times.
Definition: command_line_builder.h:998
multi_value_argument_builder< T > & add_multi_value_argument(T &value, string_type name)
Adds a new multi-value argument.
Definition: command_line_builder.h:739
std::function< void()> version_function
The function type used by the add_version_argument function.
Definition: command_line_builder.h:87
basic_parser_builder & argument_value_separator(CharType separator) noexcept
Sets the character used to separate argument names and values.
Definition: command_line_builder.h:1015
basic_parser_builder & case_sensitive(bool case_sensitive) noexcept
Sets a value that indicates whether argument names are case sensitive.
Definition: command_line_builder.h:884
basic_parser_builder & locale(std::locale loc)
Sets the locale to use when converting argument values and writing usage help.
Definition: command_line_builder.h:895
basic_parser_builder & description(string_type description)
Sets a description for the application.
Definition: command_line_builder.h:1026
basic_parser_builder & show_usage_on_error(usage_help_request request)
Sets a value that indicates how usage is shown after a parsing error occurred.
Definition: command_line_builder.h:1063
action_argument_builder< bool > & add_win32_version_argument()
Adds the standard version argument, using version information from the VERSIONINFO resource.
Definition: command_line_builder.h:822
typename parser_type::argument_base_type argument_base_type
The specialized type of command_line_argument_base used.
Definition: command_line_builder.h:83
action_argument_builder< details::first_argument_type< Action > > & add_action_argument(Action action, CharType short_name)
Adds a new action argument.
Definition: command_line_builder.h:717
typename parser_type::string_provider_type string_provider_type
The specialized type of basic_localized_string_provider used.
Definition: command_line_builder.h:85
action_argument_builder< details::first_argument_type< Action > > & add_action_argument(Action action, string_type name)
Adds a new action argument.
Definition: command_line_builder.h:696
action_argument_builder< bool > & add_version_argument(version_function function)
Adds the standard version argument.
Definition: command_line_builder.h:782
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.
Definition: command_line_builder.h:644
basic_parser_builder & prefixes(std::initializer_list< T > prefixes)
Sets the argument name prefixes accepted by the basic_command_line_parser.
Definition: command_line_builder.h:952
basic_parser_builder(string_type command_name, const string_provider_type *string_provider=nullptr)
Initializes a new instance of the basic_parser_builder class.
Definition: command_line_builder.h:607
basic_parser_builder & automatic_help_argument(bool enable)
Set a value that indicates whether to create an automatic help argument.
Definition: command_line_builder.h:1049
multi_value_argument_builder< T > & add_multi_value_argument(T &value, CharType short_name)
Adds a new multi-value argument.
Definition: command_line_builder.h:761
typename parser_type::string_type string_type
The specialized type of std::basic_string used.
Definition: command_line_builder.h:81
basic_parser_builder & long_prefix(string_type prefix)
Sets the long argument name prefix accepted by the basic_command_line_parser.
Definition: command_line_builder.h:964
parser_type build()
Creates a basic_command_line_parser using the current options and arguments.
Definition: command_line_builder.h:834
Class that provides information about arguments that are not multi-value arguments.
Definition: command_line_argument.h:432
Class that provides information about arguments that are not multi-value arguments.
Definition: command_line_argument.h:554
Provides the ookii::basic_command_line_parser class.
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
usage_help_request
Indicates if and how usage is shown if an error occurred parsing the command line.
Definition: usage_writer.h:26
parsing_mode
Indicates what argument parsing rules should be used to interpret the command line.
Definition: parsing_mode.h:14
@ long_short
Use POSIX-like rules where arguments have separate long and short names.
Template used to specify the default value description for a type.
Definition: value_description.h:30
static std::basic_string< CharType, Traits, Alloc > get()
Gets the value description for the type.
Definition: value_description.h:32