2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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"
|
|
|
|
#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:
|
|
|
|
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-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-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
|