mini-llvm 0.1.0
Loading...
Searching...
No Matches
CommandLineParser.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <cstddef>
6#include <optional>
7#include <string>
8#include <unordered_map>
9#include <utility>
10#include <variant>
11#include <vector>
12
15
16namespace mini_llvm {
17
19public:
21 public:
22 explicit OptionArgument(std::string name)
23 : name_(std::move(name)) {}
24
25 OptionArgument(std::string name, std::string value)
26 : name_(std::move(name)), value_(std::move(value)) {}
27
28 const std::string &name() const & {
29 return name_;
30 }
31
32 std::string &&name() && {
33 return std::move(name_);
34 }
35
36 const std::optional<std::string> &value() const & {
37 return value_;
38 }
39
40 std::optional<std::string> &&value() && {
41 return std::move(value_);
42 }
43
44 private:
45 std::string name_;
46 std::optional<std::string> value_;
47 };
48
50 public:
51 explicit PositionalArgument(std::string arg)
52 : arg_(std::move(arg)) {}
53
54 const std::string &arg() const & {
55 return arg_;
56 }
57
58 std::string &&arg() && {
59 return std::move(arg_);
60 }
61
62 private:
63 std::string arg_;
64 };
65
66 class Separator {};
67
68 class Argument {
69 public:
71 : arg_(std::move(option)) {}
72
73 explicit Argument(PositionalArgument positionalArg)
74 : arg_(std::move(positionalArg)) {}
75
77 : arg_(separator) {}
78
79 const OptionArgument *option() const {
80 return std::get_if<OptionArgument>(&arg_);
81 }
82
84 return std::get_if<PositionalArgument>(&arg_);
85 }
86
87 const Separator *separator() const {
88 return std::get_if<Separator>(&arg_);
89 }
90
91 private:
92 std::variant<OptionArgument, PositionalArgument, Separator> arg_;
93 };
94
95 class Result {
96 public:
97 using iterator = std::vector<Argument>::const_iterator;
98
99 explicit Result(std::vector<Argument> args)
100 : result_(std::move(args)) {}
101
102 iterator begin() const {
103 return result_.begin();
104 }
105
106 iterator end() const {
107 return result_.end();
108 }
109
110 size_t size() const {
111 return result_.size();
112 }
113
114 bool empty() const {
115 return result_.empty();
116 }
117
118 private:
119 std::vector<Argument> result_;
120 };
121
122 enum class ErrorKind {
123 kMissingValue,
124 kUnexpectedValue,
125 kUnrecognizedOption,
126 };
127
128 class Error {
129 public:
131 : kind_(kind), optionName_(std::move(optionName)) {}
132
133 ErrorKind kind() const {
134 return kind_;
135 }
136
137 const std::string &optionName() const & {
138 return optionName_;
139 }
140
141 std::string &&optionName() && {
142 return std::move(optionName_);
143 }
144
145 private:
146 ErrorKind kind_;
147 std::string optionName_;
148 };
149
150 void addOption(std::string name);
151
152 Expected<Result, Error> operator()(const std::vector<std::string> &args);
153
154private:
155 enum class OptionKind {
156 kNoValue,
157 kOptionalValue,
158 kRequiredValue,
159 };
160
161 std::unordered_map<std::string, OptionKind> options_;
162 std::vector<Argument> args_;
163};
164
165} // namespace mini_llvm
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
const Separator * separator() const
Definition CommandLineParser.h:87
const OptionArgument * option() const
Definition CommandLineParser.h:79
Argument(PositionalArgument positionalArg)
Definition CommandLineParser.h:73
Argument(OptionArgument option)
Definition CommandLineParser.h:70
Argument(Separator separator)
Definition CommandLineParser.h:76
const PositionalArgument * positional() const
Definition CommandLineParser.h:83
const std::string & optionName() const &
Definition CommandLineParser.h:137
ErrorKind kind() const
Definition CommandLineParser.h:133
std::string && optionName() &&
Definition CommandLineParser.h:141
Error(ErrorKind kind, std::string optionName)
Definition CommandLineParser.h:130
Definition CommandLineParser.h:20
const std::string & name() const &
Definition CommandLineParser.h:28
OptionArgument(std::string name, std::string value)
Definition CommandLineParser.h:25
const std::optional< std::string > & value() const &
Definition CommandLineParser.h:36
OptionArgument(std::string name)
Definition CommandLineParser.h:22
std::optional< std::string > && value() &&
Definition CommandLineParser.h:40
std::string && name() &&
Definition CommandLineParser.h:32
Definition CommandLineParser.h:49
const std::string & arg() const &
Definition CommandLineParser.h:54
PositionalArgument(std::string arg)
Definition CommandLineParser.h:51
std::string && arg() &&
Definition CommandLineParser.h:58
iterator end() const
Definition CommandLineParser.h:106
Result(std::vector< Argument > args)
Definition CommandLineParser.h:99
std::vector< Argument >::const_iterator iterator
Definition CommandLineParser.h:97
bool empty() const
Definition CommandLineParser.h:114
iterator begin() const
Definition CommandLineParser.h:102
size_t size() const
Definition CommandLineParser.h:110
Definition CommandLineParser.h:66
Definition CommandLineParser.h:18
void addOption(std::string name)
ErrorKind
Definition CommandLineParser.h:122
Expected< Result, Error > operator()(const std::vector< std::string > &args)
Definition Expected.h:65
Definition GraphColoringAllocator.h:13
constexpr const char * name(Diagnostic::Level level)
Definition Diagnostic.h:38