Ookii.CommandLine for C++  1.0.0
command_line_builder.h
Go to the documentation of this file.
1 #ifndef OOKII_COMMAND_LINE_BUILDER_H_
6 #define OOKII_COMMAND_LINE_BUILDER_H_
7 
8 #pragma once
9 
10 #include "command_line_core.h"
11 
12 namespace ookii
13 {
34  template<typename CharType = details::default_char_type, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
36  {
37  using parser_storage_type = details::parser_storage<CharType, Traits, Alloc>;
38 
39  public:
46 
49  template<typename T>
51 
53 
54  template<typename ArgumentType, typename BaseType>
55  class argument_builder;
56 
57  template<typename T>
59 
64  {
65  using storage_type = typename argument_base_type::storage_type;
66 
67  public:
69  virtual ~argument_builder_base() = default;
70 
72  argument_builder_base &operator=(argument_builder_base &) = delete;
73 
75  template<typename T>
77  {
78  return _parser_builder.add_argument(value, name);
79  }
80 
82  template<typename T>
84  {
85  return _parser_builder.add_multi_value_argument(value, name);
86  }
87 
90  {
91  return _parser_builder.build();
92  }
93 
95  const string_type &name() const
96  {
97  return this->_storage.name;
98  }
99 
103 
104  protected:
109  : _storage{name},
110  _parser_builder{basic_parser_builder}
111  {
112  }
113 
115  storage_type &storage()
116  {
117  return _storage;
118  }
119 
122  {
123  return _parser_builder.get_next_position();
124  }
125 
126  private:
127  storage_type _storage;
128  basic_parser_builder &_parser_builder;
129  };
130 
135  template<typename ArgumentType, typename BaseType>
136  class argument_builder : public BaseType
137  {
138  using typed_storage_type = typename ArgumentType::typed_storage_type;
139  using value_type = typename ArgumentType::value_type;
140  using element_type = typename ArgumentType::element_type;
141  using converter_type = typename typed_storage_type::converter_type;
142 
143  public:
149  : BaseType{basic_parser_builder, name},
150  _typed_storage{value}
151  {
152  }
153 
175  {
176  this->storage().value_description = value_description;
177  return *this;
178  }
179 
190  {
191  this->storage().description = description;
192  return *this;
193  }
194 
205  {
206  if (!this->storage().position)
207  {
208  this->storage().position = this->get_next_position();
209  }
210 
211  return *this;
212  }
213 
221  {
222  this->storage().is_required = true;
223  return *this;
224  }
225 
241  {
242  this->_typed_storage.default_value = default_value;
243  return *this;
244  }
245 
257  {
258  this->_typed_storage.converter = converter;
259  return *this;
260  }
261 
272  {
273  this->storage().aliases.push_back(alias);
274  return *this;
275  }
276 
290  {
291  this->storage().cancel_parsing = true;
292  return *this;
293  }
294 
295  private:
296  virtual owned_or_borrowed_ptr<argument_base_type> to_argument() override
297  {
298  if (this->storage().value_description.empty())
300 
301  return make_owned_ptr<ArgumentType>(std::move(this->storage()), std::move(_typed_storage));
302  }
303 
304  typed_storage_type _typed_storage;
305  };
306 
309  template<typename T>
311  {
312  public:
315 
330  {
331  this->storage().multi_value_separator = separator;
332  return *static_cast<builder_type*>(this);
333  }
334 
335  protected:
341  {
342  }
343  };
344 
349  : _storage{command_name}
350  {
351  }
352 
382  template<typename T>
384  {
385  _arguments.push_back(std::make_unique<argument_builder<typed_argument_type<T>, argument_builder_base>>(*this, name, value));
386  return *static_cast<argument_builder<typed_argument_type<T>, argument_builder_base>*>(_arguments.back().get());
387  }
388 
402  template<typename T>
404  {
405  _arguments.push_back(std::make_unique<typename multi_value_argument_builder<T>::builder_type>(*this, name, value));
406  return *static_cast<typename multi_value_argument_builder<T>::builder_type*>(_arguments.back().get());
407  }
408 
416  {
417  if (_storage.prefixes.empty())
418  _storage.prefixes = parser_type::get_default_prefixes();
419 
420  return parser_type{_arguments, std::move(_storage), _case_sensitive};
421  }
422 
433  {
434  _case_sensitive = case_sensitive;
435  return *this;
436  }
437 
442  basic_parser_builder &locale(std::locale loc)
443  {
444  _storage.locale = loc;
445  return *this;
446  }
447 
461  template<typename Range>
463  {
464  // Using enables ADL.
465  using std::begin;
466  using std::end;
467  _storage.prefixes = std::vector<string_type>{ begin(prefixes), end(prefixes) };
468  return *this;
469  }
470 
484  template<typename T>
485  basic_parser_builder &prefixes(std::initializer_list<T> prefixes)
486  {
487  return this->prefixes<std::initializer_list<T>>(prefixes);
488  }
489 
501  {
502  _storage.allow_white_space_separator = allow;
503  return *this;
504  }
505 
517  {
518  _storage.allow_duplicate_arguments = allow;
519  return *this;
520  }
521 
532  basic_parser_builder &argument_value_separator(CharType separator) noexcept
533  {
534  _storage.argument_value_separator = separator;
535  return *this;
536  }
537 
543  {
544  _storage.description = description;
545  return *this;
546  }
547 
548  private:
549  size_t get_next_position() noexcept
550  {
551  return _next_position++;
552  }
553 
554  std::vector<std::unique_ptr<argument_builder_base>> _arguments;
555  size_t _next_position{};
556  bool _case_sensitive{};
557 
558  parser_storage_type _storage;
559  };
560 
565 
566 }
567 
568 #endif
Parses command line arguments into strongly-typed values.
Definition: command_line_core.h:92
command_line_argument_base< CharType, Traits, Alloc > argument_base_type
The specialized type of command_line_argument_base used.
Definition: command_line_core.h:95
static constexpr std::vector< string_type > get_default_prefixes()
Gets the default prefixes accepted by the parser.
Definition: command_line_core.h:126
typename argument_base_type::string_type string_type
The specialized type of std::basic_string used.
Definition: command_line_core.h:97
Abstract base class with common functionality for the argument builders.
Definition: command_line_builder.h:64
storage_type & storage()
Provides access to the argument's options storage.
Definition: command_line_builder.h:115
size_t get_next_position()
Gets the next position for a positional argument.
Definition: command_line_builder.h:121
const string_type & name() const
Returns the name of the argument.
Definition: command_line_builder.h:95
parser_type build()
Creates a basic_command_line_parser using the current options and arguments.
Definition: command_line_builder.h:89
virtual owned_or_borrowed_ptr< argument_base_type > to_argument()=0
Converts the argument_builder_base into a command_line_argument_base that can be used by the basic_co...
virtual ~argument_builder_base()=default
Default destructor.
multi_value_argument_builder< T >::builder_type & add_multi_value_argument(T &value, string_type name)
Adds a new multi-value argument, and returns an argument_builder that can be used to further customiz...
Definition: command_line_builder.h:83
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:108
argument_builder< typed_argument_type< T >, argument_builder_base > & 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:76
Specifies options for an argument under construction.
Definition: command_line_builder.h:137
argument_builder & value_description(string_type value_description)
Sets the value description for the argument.
Definition: command_line_builder.h:174
argument_builder & alias(string_type alias)
Adds an alias to the argument.
Definition: command_line_builder.h:271
argument_builder & cancel_parsing()
Indicates that supplying this argument will cancel parsing.
Definition: command_line_builder.h:289
argument_builder & converter(converter_type converter)
Supplies a custom function to convert strings to the argument's type.
Definition: command_line_builder.h:256
argument_builder & required()
Indicates that the argument is required.
Definition: command_line_builder.h:220
argument_builder & 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:240
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:148
argument_builder & positional()
Indicates that the argument can be specified by position.
Definition: command_line_builder.h:204
argument_builder & description(string_type description)
Sets the long description for the argument.
Definition: command_line_builder.h:189
Base class for argument_builder for multi-value arguments.
Definition: command_line_builder.h:311
multi_value_argument_builder(basic_parser_builder &basic_parser_builder, string_type name)
Initializes a new instance of the multi_value_argument_builder class.
Definition: command_line_builder.h:339
builder_type & separator(CharType separator)
Specifies a separator that separates multiple values in a single argument value.
Definition: command_line_builder.h:329
Provides functionality to specify options and arguments to create a new basic_command_line_parser.
Definition: command_line_builder.h:36
basic_parser_builder & prefixes(const Range &prefixes)
Sets the argument name prefixes accepted by the basic_command_line_parser.
Definition: command_line_builder.h:462
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:500
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:516
basic_parser_builder & argument_value_separator(CharType separator) noexcept
Sets the character used to separate argument names and values.
Definition: command_line_builder.h:532
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:432
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:442
basic_parser_builder & description(string_type description)
Sets a description for the application.
Definition: command_line_builder.h:542
basic_parser_builder(string_type command_name)
Initializes a new instance of the basic_parser_builder class.
Definition: command_line_builder.h:348
typename parser_type::argument_base_type argument_base_type
The specialized type of command_line_argument_base used.
Definition: command_line_builder.h:45
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:485
typename parser_type::string_type string_type
The specialized type of std::basic_string used.
Definition: command_line_builder.h:43
multi_value_argument_builder< T >::builder_type & add_multi_value_argument(T &value, string_type name)
Adds a new multi-value argument, and returns an argument_builder that can be used to further customiz...
Definition: command_line_builder.h:403
parser_type build()
Creates a basic_command_line_parser using the current options and arguments.
Definition: command_line_builder.h:415
argument_builder< typed_argument_type< T >, argument_builder_base > & 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:383
Class that provides information about arguments that are not multi-value arguments.
Definition: command_line_argument.h:313
Smart pointer that may or may not own the contained pointer.
Definition: owned_or_borrowed_ptr.h:28
Provides the ookii::basic_command_line_parser class.
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
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