Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

112 рядки
3.6 KiB

  1. #pragma once
  2. #include <cppargs/parser/parser.h>
  3. #include <cppargs/group/member_group.h>
  4. namespace cppargs
  5. {
  6. struct member_parser_base
  7. : public parser
  8. {
  9. public:
  10. using parser::parser;
  11. public:
  12. /**
  13. * @brief Create new member option.
  14. *
  15. * @param[in] meta Meta data of the new option.
  16. * @param[in] member Member to write parsed argument to.
  17. *
  18. * @return Created option.
  19. */
  20. template<typename T_instance, typename T_object, typename T_value>
  21. static constexpr decltype(auto) make_member_option(const option_meta& meta, T_value T_object::*member);
  22. /**
  23. * @brief Create new group.
  24. *
  25. * @param[in] meta Meta data of the new option.
  26. * @param[in] args Subgroups and options.
  27. *
  28. * @return Created group.
  29. */
  30. template<typename T_instance, typename... T_args>
  31. static constexpr decltype(auto) make_group(const group_meta& meta, T_args&&... args);
  32. /**
  33. * @brief Create new group.
  34. *
  35. * @param[in] meta Meta data of the new option.
  36. * @param[in] member Member of current instance to use for this group.
  37. * @param[in] args Subgroups and options.
  38. *
  39. * @return Created group.
  40. */
  41. template<typename T_instance, typename T_object, typename T_value, typename... T_args>
  42. static constexpr decltype(auto) make_member_group(const group_meta& meta, T_value T_object::*member, T_args&&... args);
  43. };
  44. /**
  45. * @brief Class to parse options using a object to store values in.
  46. */
  47. template<typename T_instance>
  48. struct member_parser
  49. : public member_parser_base
  50. {
  51. public:
  52. using base_type = member_parser_base;
  53. using instance_type = T_instance;
  54. using this_type = member_parser<instance_type>;
  55. using option_ptr_type = typename base_type::option_ptr_type;
  56. private:
  57. struct base_group_pred
  58. {
  59. this_type& owner;
  60. instance_type* operator()(void*) const
  61. { return owner._instance; }
  62. };
  63. using base_group_type = member_group<void, instance_type, base_group_pred>;
  64. template<typename... T_args>
  65. constexpr decltype(auto) make_base_group(T_args&&... args)
  66. {
  67. using owner_instance_type = void;
  68. using group_instance_type = T_instance;
  69. using predicate_type = base_group_pred;
  70. using group_type = member_group<owner_instance_type, group_instance_type, predicate_type>;
  71. return group_type({ }, predicate_type { *this }, std::forward<T_args>(args)...);
  72. }
  73. private:
  74. base_group_type _group;
  75. mutable instance_type* _instance { nullptr };
  76. public:
  77. /**
  78. * @brief Constructor.
  79. *
  80. * @param[in] p_default Default option to use for unknown arguents (optional / can be null).
  81. * @param[in] p_args Options and subgroups.
  82. */
  83. template<typename... T_args>
  84. member_parser(
  85. option_ptr_type&& p_default,
  86. T_args&&... args);
  87. /**
  88. * @brief Parse the command line arguments using the passed options.
  89. *
  90. * @param[in] instance Instance to store parsed values in.
  91. * @param[in] argc Number of arguments in argv.
  92. * @param[in] argv Array of all arguments.
  93. */
  94. inline void parse(instance_type& instance, int argc, char const * argv[]) const;
  95. };
  96. }