25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
1.8 KiB

  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include <memory>
  5. #include <cstring>
  6. #include <cppargs/group/group.h>
  7. namespace cppargs
  8. {
  9. /**
  10. * @brief Class to parse command line arguments.
  11. */
  12. struct parser
  13. {
  14. public:
  15. using option_type = option;
  16. using option_ptr_type = std::unique_ptr<option_type>;
  17. using option_map_type = std::map<std::string, option_type*>;
  18. private:
  19. group& _group;
  20. option_ptr_type _default; //!< Default option to parse unknown arguments.
  21. option_map_type _map; //!< Map of option name to option.
  22. protected:
  23. /**
  24. * @brief Add the passed group to the options map
  25. */
  26. inline void add_to_map(const group& g);
  27. /**
  28. * @brief Update the option map.
  29. */
  30. inline void update_map();
  31. /**
  32. * @brief Handle invalid argument (use default option or throw exception).
  33. */
  34. inline void handle_invalid_arg(context& c) const;
  35. /**
  36. * @brief Parse the command line arguments using the passed options.
  37. *
  38. * @param[in] argc Number of arguments in argv.
  39. * @param[in] argv Array of all arguments.
  40. */
  41. inline void parse(int argc, char const * argv[]) const;
  42. public:
  43. /**
  44. * @brief Constructor.
  45. *
  46. * @param[in] p_default Default option to use for unknown arguents (optional / can be null).
  47. * @param[in] p_group Group with options and subgroups.
  48. */
  49. inline parser(
  50. option_ptr_type&& p_default,
  51. group& p_group);
  52. /**
  53. * @brief Print the help of all options
  54. *
  55. * @param[in] os Stream to write help to.
  56. */
  57. inline void print_help(std::ostream& os) const;
  58. };
  59. }