mirror of
https://git.lynn.is/Gwen/argparser.git
synced 2024-05-08 02:31:09 +02:00
61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
#ifndef ARGPARSER_SINGLE_FLAG_H
|
|
#define ARGPARSER_SINGLE_FLAG_H
|
|
|
|
#include <cassert>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include "option.h"
|
|
#include "parse-result.h"
|
|
#include "type.h"
|
|
|
|
namespace argparser {
|
|
|
|
class flag_impl : public option, public std::enable_shared_from_this<flag_impl> {
|
|
public:
|
|
explicit flag_impl(std::string name) : option(std::move(name)) {}
|
|
|
|
[[nodiscard]] bool is_inverted() const {
|
|
return is_inverted_;
|
|
}
|
|
|
|
std::shared_ptr<flag_impl> invert() {
|
|
this->is_inverted_ = true;
|
|
return this->shared_from_this();
|
|
}
|
|
|
|
[[nodiscard]] bool consumes_value() const override {
|
|
return false;
|
|
}
|
|
|
|
void validate(const parse_result &) const override {
|
|
}
|
|
|
|
[[nodiscard]] bool get(const parse_result &pr) const {
|
|
bool val = pr.has_opt(name);
|
|
if (is_inverted_) {
|
|
val = !val;
|
|
}
|
|
return val;
|
|
}
|
|
[[nodiscard]] bool has(const parse_result &pr) const {
|
|
return pr.has_opt(name);
|
|
}
|
|
|
|
private:
|
|
void do_parse(std::optional<std::string> arg, std::any &val) override {
|
|
assert(!arg.has_value());
|
|
if (!val.has_value()) {
|
|
val = std::make_any<bool>(true);
|
|
}
|
|
}
|
|
bool is_inverted_{};
|
|
};
|
|
|
|
using flag_handle_impl = std::shared_ptr<flag_impl>;
|
|
|
|
}// namespace argparser
|
|
|
|
#endif//ARGPARSER_SINGLE_FLAG_H
|