-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.hpp
More file actions
44 lines (36 loc) · 1.38 KB
/
Parser.hpp
File metadata and controls
44 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
#include <cstdlib>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
#include "Arg.hpp"
#include "Parsables.hpp"
#include "utils.hpp"
class ClapParser {
public:
void add_arg(const Arg& arg);
void parse(const int& argc, char* argv[]);
void print_help() const;
template <typename T>
requires Parseable<T>
std::optional<T> get_one_as(const std::string& name) {
Arg* arg = ok_or(ClapParser::find_arg(*this, "--" + name), [] { return std::nullopt; });
return Parse<T>::parse(arg->get__value().value());
}
static void print_parser(std::ostream& os, const ClapParser& parser, int indent);
friend std::ostream& operator<<(std::ostream& os, const ClapParser& parser);
private:
std::vector<Arg> args_;
std::string program_name_;
// Helper methods
static bool is_option(const std::string& token);
static bool is_long_option(const std::string& token);
static bool is_short_option(const std::string& token);
static std::optional<Arg*> find_arg(ClapParser& parser, const std::string& name);
void apply_defaults();
void parse_cli_args(const std::vector<std::string>& args);
void check_env();
void parse_positional_args(const std::vector<std::string>& args);
static void parse_value_for_non_flag(Arg* arg, size_t& cli_index, const std::vector<std::string>& args);
};