|
- #pragma once
-
- #include <cppargs/parser/parser.h>
- #include <cppargs/group/member_group.h>
-
- namespace cppargs
- {
-
- struct member_parser_base
- : public parser
- {
- public:
- using parser::parser;
-
- public:
- /**
- * @brief Create new member option.
- *
- * @param[in] meta Meta data of the new option.
- * @param[in] member Member to write parsed argument to.
- *
- * @return Created option.
- */
- template<typename T_instance, typename T_object, typename T_value>
- static constexpr decltype(auto) make_member_option(const option_meta& meta, T_value T_object::*member);
-
- /**
- * @brief Create new group.
- *
- * @param[in] meta Meta data of the new option.
- * @param[in] args Subgroups and options.
- *
- * @return Created group.
- */
- template<typename T_instance, typename... T_args>
- static constexpr decltype(auto) make_group(const group_meta& meta, T_args&&... args);
-
- /**
- * @brief Create new group.
- *
- * @param[in] meta Meta data of the new option.
- * @param[in] member Member of current instance to use for this group.
- * @param[in] args Subgroups and options.
- *
- * @return Created group.
- */
- template<typename T_instance, typename T_object, typename T_value, typename... T_args>
- static constexpr decltype(auto) make_member_group(const group_meta& meta, T_value T_object::*member, T_args&&... args);
- };
-
- /**
- * @brief Class to parse options using a object to store values in.
- */
- template<typename T_instance>
- struct member_parser
- : public member_parser_base
- {
- public:
- using base_type = member_parser_base;
- using instance_type = T_instance;
- using this_type = member_parser<instance_type>;
- using option_ptr_type = typename base_type::option_ptr_type;
-
- private:
- struct base_group_pred
- {
- this_type& owner;
-
- instance_type* operator()(void*) const
- { return owner._instance; }
- };
-
- using base_group_type = member_group<void, instance_type, base_group_pred>;
-
- template<typename... T_args>
- constexpr decltype(auto) make_base_group(T_args&&... args)
- {
- using owner_instance_type = void;
- using group_instance_type = T_instance;
- using predicate_type = base_group_pred;
- using group_type = member_group<owner_instance_type, group_instance_type, predicate_type>;
- return group_type({ }, predicate_type { *this }, std::forward<T_args>(args)...);
- }
-
- private:
- base_group_type _group;
- mutable instance_type* _instance { nullptr };
-
- public:
- /**
- * @brief Constructor.
- *
- * @param[in] p_default Default option to use for unknown arguents (optional / can be null).
- * @param[in] p_args Options and subgroups.
- */
- template<typename... T_args>
- member_parser(
- option_ptr_type&& p_default,
- T_args&&... args);
-
- /**
- * @brief Parse the command line arguments using the passed options.
- *
- * @param[in] instance Instance to store parsed values in.
- * @param[in] argc Number of arguments in argv.
- * @param[in] argv Array of all arguments.
- */
- inline void parse(instance_type& instance, int argc, char const * argv[]) const;
- };
-
- }
|