argon 0.12.0
Command line argument parser
Loading...
Searching...
No Matches
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
11namespace argon {
12
14
17enum Action : std::uint8_t {
19 USAGE
20};
21
24class 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
33public:
34 Parser(int argc, const char* const argv[]);
35 void add_option(const std::string& flags, const std::string& description, Action action, const std::string& output);
36 void add_option(const std::string& flags, const std::string& description, bool& found);
37
38 template <typename T>
39 void add_option(const std::string& flags, const std::string& description, T& value);
40
41 void add_position(const std::string& name, const std::string& description);
42 void parse();
43
44 [[nodiscard]] auto get_position(size_t index) const -> std::string_view;
45 [[nodiscard]] auto args() const -> std::vector<std::string_view>;
46};
47
48template <typename T>
49void Parser::add_option(const std::string& flags, const std::string& description, T& value)
50{
51 m_options.push_back(std::make_unique<ValueOption>(
52 flags, description, [&value](std::string_view s) { std::istringstream(std::string(s)) >> value; }));
53}
54
55}
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