-
Notifications
You must be signed in to change notification settings - Fork 31
/
command_line_options.hpp
69 lines (55 loc) · 2.55 KB
/
command_line_options.hpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#ifndef WAYWARD_SUPPORT_COMMAND_LINE_OPTIONS_HPP_INCLUDED
#define WAYWARD_SUPPORT_COMMAND_LINE_OPTIONS_HPP_INCLUDED
#include <functional>
#include <string>
#include <vector>
#include <wayward/support/maybe.hpp>
namespace wayward {
struct CommandLineOptions {
CommandLineOptions();
void program_name(std::string); // Defaults to argv[0]
/*
parse returns a list of unmatched non-option arguments, and everything after "--".
*/
std::vector<std::string>
parse(int argc, char const* const* argv);
void description(std::string next_option_description);
void option(std::string long_form, std::string short_form, std::function<void()> callback);
void option(std::string long_form, std::string short_form, std::function<void(std::string)> callback_with_argument);
void option(std::string long_form, std::string short_form, std::function<void(int64_t)> callback_with_argument);
void option(std::string long_form, std::function<void()> callback);
void option(std::string long_form, std::function<void(std::string)> callback_with_argument);
void option(std::string long_form, std::function<void(int64_t)> callback_with_argument);
void usage(std::string long_form = "--help", std::string short_form = "-h");
void usage(std::function<void()> action, std::string long_form = "--help", std::string short_form = "-h");
void unrecognized(std::function<void(std::string)>);
void display_usage();
void display_usage_and_exit();
private:
Maybe<std::string> program_name_;
struct Option {
Maybe<std::string> long_form;
Maybe<std::string> short_form;
std::string description;
std::function<void(Maybe<std::string>)> action;
bool needs_value;
};
std::vector<Option> options_;
std::string next_option_description_;
size_t longest_long_form_ = 0;
size_t longest_short_form_ = 0;
std::function<void(std::string)> unrecognized_action_;
void add_option(Maybe<std::string> long_form, Maybe<std::string> short_form, std::function<void(Maybe<std::string>)> action, bool needs_value = false);
};
inline void CommandLineOptions::program_name(std::string pn) {
program_name_ = std::move(pn);
}
inline void CommandLineOptions::description(std::string next_option_description) {
next_option_description_ = std::move(next_option_description);
}
inline void CommandLineOptions::unrecognized(std::function<void(std::string)> action) {
unrecognized_action_ = std::move(action);
}
}
#endif // WAYWARD_SUPPORT_COMMAND_LINE_OPTIONS_HPP_INCLUDED