-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathargs.h
119 lines (102 loc) · 4.07 KB
/
args.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef __ASHUFFLE_ARGS_H__
#define __ASHUFFLE_ARGS_H__
#include <istream>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include <absl/time/time.h>
#include "mpd.h"
#include "rule.h"
namespace ashuffle {
struct ParseError {
enum Type {
kUnknown, // Initial error type, unknown error.
kGeneric, // Generic failure. Described by 'msg'.
kHelp, // The user requested the help to be printed.
kVersion, // The user requested the version to be printed.
};
Type type;
std::string msg;
ParseError() : type(kUnknown){};
ParseError(std::string_view m) : ParseError(kGeneric, m){};
ParseError(Type t, std::string_view m) : type(t), msg(m){};
};
class Options {
public:
std::vector<Rule> ruleset;
unsigned queue_only = 0;
std::istream *file_in = nullptr;
std::ostream *log_file = nullptr;
bool check_uris = true;
unsigned queue_buffer = 0;
std::optional<std::string> host = {};
unsigned port = 0;
// Special test-only options.
struct {
bool print_all_songs_and_exit = false;
} test = {};
// Minor "tweak" options that are not part of the main options.
struct {
// Window size to use for the global shuffle chain.
int window_size = 7;
// If true, start playing music when ashuffle is first started.
// Otherwise, ashuffle will wait for an MPD event before playing
// music.
bool play_on_startup = true;
// Duration to wait before checking queue length for suspend/resume.
absl::Duration suspend_timeout = absl::ZeroDuration();
// If true, exit when MPD produces a database update event. This is
// intented to be used in cases where the user is passing in a
// list of songs via -f, and they may want to re-generate that list.
bool exit_on_db_update = false;
// Duration to attempt to reconnect to MPD after a disconnection.
// After this time, ashuffle will assume it cannot reconnect and
// will quit.
absl::Duration reconnect_timeout = absl::Seconds(10);
} tweak = {};
std::vector<enum mpd_tag_type> group_by = {};
// Parse parses the arguments in the given vector and returns ParseResult
// based on the success/failure of the parse.
static std::variant<Options, ParseError> Parse(
const mpd::TagParser &, const std::vector<std::string> &);
// ParseFromC parses the arguments in the given c-style arguments list,
// like would be given in `main`.
static std::variant<Options, ParseError> ParseFromC(
const mpd::TagParser &tag_parser, const char **argv, int argc) {
std::vector<std::string> args;
// Start from '1' to skip the program name itself.
for (int i = 1; i < argc; i++) {
args.push_back(argv[i]);
}
return Options::Parse(tag_parser, args);
}
// Take ownership fo the given istream, and set the file_in member to
// point to the referenced istream. This should only be used while the
// Options are being constructed.
void InternalTakeIstream(std::unique_ptr<std::istream> &&is) {
file_in = is.get();
owned_file_ = std::move(is);
};
// Same as InternalTakeIstream but for the log file ostream.
void InternalTakeLog(std::unique_ptr<std::ostream> &&os) {
log_file = os.get();
owned_log_file_ = std::move(os);
}
private:
// The owned_file is set if this Options class owns the file_in ptr.
// The file_in ptr is only *sometimes* owned. For example, the file_in ptr
// may point to std::cin, which has static lifetime, and is not owned by
// this object.
std::unique_ptr<std::istream> owned_file_;
// Same as above.
std::unique_ptr<std::ostream> owned_log_file_;
};
// Print the help message on the given output stream, and return the input
// ostream.
std::ostream &DisplayHelp(std::ostream &);
// Print the given ParseError to the given output stream.
std::ostream &operator<<(std::ostream &out, const ParseError &e);
} // namespace ashuffle
#endif