2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2020-09-14 13:45:22 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace td {
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2020-06-17 02:04:36 +02:00
|
|
|
class OptionParser {
|
2018-12-31 20:04:05 +01:00
|
|
|
class Option {
|
|
|
|
public:
|
2020-06-16 17:11:32 +02:00
|
|
|
enum class Type { NoArg, Arg };
|
2018-12-31 20:04:05 +01:00
|
|
|
Type type;
|
|
|
|
char short_key;
|
2020-06-16 17:11:32 +02:00
|
|
|
string long_key;
|
|
|
|
string description;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::function<Status(Slice)> arg_callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
void add_option(Option::Type type, char short_key, Slice long_key, Slice description,
|
2019-07-06 13:29:15 +02:00
|
|
|
std::function<Status(Slice)> callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-16 17:11:32 +02:00
|
|
|
public:
|
2020-09-14 13:45:22 +02:00
|
|
|
template <class T>
|
|
|
|
static std::function<Status(Slice)> parse_integer(T &value) {
|
|
|
|
return [&value](Slice value_str) {
|
|
|
|
TRY_RESULT_ASSIGN(value, to_integer_safe<T>(value_str));
|
|
|
|
return Status::OK();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-14 13:51:52 +02:00
|
|
|
static std::function<void(Slice)> parse_string(string &value) {
|
|
|
|
return [&value](Slice value_str) {
|
|
|
|
value = value_str.str();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-14 16:04:45 +02:00
|
|
|
void set_usage(Slice executable_name, Slice usage);
|
|
|
|
|
2020-06-16 17:11:32 +02:00
|
|
|
void set_description(string description);
|
|
|
|
|
2020-06-19 03:44:38 +02:00
|
|
|
void add_checked_option(char short_key, Slice long_key, Slice description, std::function<Status(Slice)> callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-19 03:44:38 +02:00
|
|
|
void add_checked_option(char short_key, Slice long_key, Slice description, std::function<Status(void)> callback);
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
void add_option(char short_key, Slice long_key, Slice description, std::function<Status(Slice)> callback) = delete;
|
|
|
|
|
|
|
|
void add_option(char short_key, Slice long_key, Slice description, std::function<Status(void)> callback) = delete;
|
|
|
|
|
2020-06-19 03:44:38 +02:00
|
|
|
void add_option(char short_key, Slice long_key, Slice description, std::function<void(Slice)> callback);
|
|
|
|
|
|
|
|
void add_option(char short_key, Slice long_key, Slice description, std::function<void(void)> callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-19 04:50:18 +02:00
|
|
|
void add_check(std::function<Status()> check);
|
|
|
|
|
2020-06-17 02:04:36 +02:00
|
|
|
// returns found non-option parameters
|
2020-06-19 05:00:01 +02:00
|
|
|
Result<vector<char *>> run(int argc, char *argv[], int expected_non_option_count = -1) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-20 01:33:09 +01:00
|
|
|
// for testing only
|
|
|
|
Result<vector<char *>> run_impl(int argc, char *argv[], int expected_non_option_count) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2020-06-17 02:04:36 +02:00
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const OptionParser &o);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2020-06-16 17:11:32 +02:00
|
|
|
vector<Option> options_;
|
2020-06-19 04:50:18 +02:00
|
|
|
vector<std::function<Status()>> checks_;
|
2020-09-14 16:04:45 +02:00
|
|
|
string usage_;
|
2020-06-16 17:11:32 +02:00
|
|
|
string description_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|