argon  0.12.0
Command line argument parser
Parser.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <argon/Option.hpp>
4 #include <argon/Position.hpp>
5 
6 #include <cstdint>
7 #include <memory>
8 #include <sstream>
9 #include <string_view>
10 
11 namespace argon {
12 
14 
17 enum Action : std::uint8_t {
19  USAGE
20 };
21 
24 class ARGON_EXPORT Parser {
25  [[nodiscard]] auto make_usage(const std::string& help) const -> std::string;
26 
27  std::string m_program_name;
28 
29  std::vector<std::string_view> m_args;
30  std::vector<std::shared_ptr<Option>> m_options;
31  std::vector<Position> m_positions;
32 
33 public:
34  Parser(int argc, const char* const argv[]);
35  void add_option(const std::string& flags,
36  const std::string& description,
37  const Action& action,
38  const std::string& output);
39  void add_option(const std::string& flags, const std::string& description, bool& found);
40 
41  template <typename T>
42  void add_option(const std::string& flags, const std::string& description, T& value);
43 
44  void add_position(const std::string& name, const std::string& description);
45  void parse();
46 
47  [[nodiscard]] auto get_position(size_t index) const -> std::string_view;
48  [[nodiscard]] auto args() const -> std::vector<std::string_view>;
49 };
50 
51 template <typename T>
52 void Parser::add_option(const std::string& flags, const std::string& description, T& value)
53 {
54  m_options.push_back(std::make_unique<ValueOption>(
55  flags, description, [&value](std::string_view s) { std::istringstream(std::string(s)) >> value; }));
56 }
57 
58 }
Action
Special tags for options.
Definition: Parser.hpp:17
@ USAGE
Print usage text.
Definition: Parser.hpp:19
@ PRINT
Print message to console then exit.
Definition: Parser.hpp:18
Argument parser.
Definition: Parser.hpp:24