-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparsecommand.cpp
254 lines (186 loc) · 8.63 KB
/
parsecommand.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <cstddef>
#include <limits>
#include <memory>
#include <CLI/App.hpp>
#include <CLI/CLI.hpp> // NOLINT: Need to include this for impls of some CLI11 classes
#include <CLI/Validators.hpp>
#include "launch_p.hpp"
namespace qs::launch {
int parseCommand(int argc, char** argv, CommandState& state) {
state.exec = {
.argc = argc,
.argv = argv,
};
auto addConfigSelection = [&](CLI::App* cmd, bool withNewestOption = false) {
auto* group = cmd->add_option_group("Config Selection")
->description("If no options in this group are specified,\n"
"$XDG_CONFIG_HOME/quickshell/shell.qml will be used.");
auto* path = group->add_option("-p,--path", state.config.path)
->description("Path to a QML file.")
->envname("QS_CONFIG_PATH");
group->add_option("-m,--manifest", state.config.manifest)
->description("Path to a quickshell manifest.\n"
"Defaults to $XDG_CONFIG_HOME/quickshell/manifest.conf")
->envname("QS_MANIFEST")
->excludes(path);
group->add_option("-c,--config", state.config.name)
->description("Name of a quickshell configuration to run.\n"
"If -m is specified, this is a configuration in the manifest,\n"
"otherwise it is the name of a folder in $XDG_CONFIG_HOME/quickshell.")
->envname("QS_CONFIG_NAME");
if (withNewestOption) {
group->add_flag("-n,--newest", state.config.newest)
->description("Operate on the most recently launched instance instead of the oldest");
}
return group;
};
auto addDebugOptions = [&](CLI::App* cmd) {
auto* group = cmd->add_option_group("Debugging", "Options for QML debugging.");
auto* debug = group->add_option("--debug", state.debug.port)
->description("Open the given port for a QML debugger connection.")
->check(CLI::Range(0, 65535));
group->add_flag("--waitfordebug", state.debug.wait)
->description("Wait for a QML debugger to connect before executing the configuration.")
->needs(debug);
return group;
};
auto addLoggingOptions = [&](CLI::App* cmd, bool noGroup, bool noDisplay = false) {
auto* group = noGroup ? cmd : cmd->add_option_group(noDisplay ? "" : "Logging");
group->add_flag("--no-color", state.log.noColor)
->description("Disables colored logging.\n"
"Colored logging can also be disabled by specifying a non empty value\n"
"for the NO_COLOR environment variable.");
group->add_flag("--log-times", state.log.timestamp)
->description("Log timestamps with each message.");
group->add_option("--log-rules", state.log.rules)
->description("Log rules to apply, in the format of QT_LOGGING_RULES.");
group->add_flag("-v,--verbose", [&](size_t count) { state.log.verbosity = count; })
->description("Increases log verbosity.\n"
"-v will show INFO level internal logs.\n"
"-vv will show DEBUG level internal logs.");
auto* hgroup = cmd->add_option_group("");
hgroup->add_flag("--no-detailed-logs", state.log.sparse);
};
auto addInstanceSelection = [&](CLI::App* cmd) {
auto* group = cmd->add_option_group("Instance Selection");
group->add_option("-i,--id", state.instance.id)
->description("The instance id to operate on.\n"
"You may also use a substring the id as long as it is unique,\n"
"for example \"abc\" will select \"abcdefg\".");
group->add_option("--pid", state.instance.pid)
->description("The process id of the instance to operate on.");
return group;
};
state.app = std::make_unique<CLI::App>();
auto* cli = state.app.get();
// Require 0-1 subcommands. Without this, positionals can be parsed as more subcommands.
cli->require_subcommand(0, 1);
addConfigSelection(cli);
addLoggingOptions(cli, false);
addDebugOptions(cli);
{
cli->add_option_group("")->add_flag("--private-check-compat", state.misc.checkCompat);
cli->add_flag("-V,--version", state.misc.printVersion)
->description("Print quickshell's version and exit.");
cli->add_flag("-n,--no-duplicate", state.misc.noDuplicate)
->description("Exit immediately if another instance of the given config is running.");
cli->add_flag("-d,--daemonize", state.misc.daemonize)
->description("Detach from the controlling terminal.");
}
{
auto* sub = cli->add_subcommand("log", "Print quickshell logs.");
auto* file = sub->add_option("file", state.log.file, "Log file to read.");
sub->add_option("-t,--tail", state.log.tail)
->description("Maximum number of lines to print, starting from the bottom.")
->check(CLI::Range(1, std::numeric_limits<int>::max(), "INT > 0"));
sub->add_flag("-f,--follow", state.log.follow)
->description("Keep reading the log until the logging process terminates.");
sub->add_option("-r,--rules", state.log.readoutRules, "Log file to read.")
->description("Rules to apply to the log being read, in the format of QT_LOGGING_RULES.");
auto* instance = addInstanceSelection(sub)->excludes(file);
addConfigSelection(sub, true)->excludes(instance)->excludes(file);
addLoggingOptions(sub, false);
state.subcommand.log = sub;
}
{
auto* sub = cli->add_subcommand("list", "List running quickshell instances.");
auto* all = sub->add_flag("-a,--all", state.instance.all)
->description("List all instances.\n"
"If unspecified, only instances of"
"the selected config will be listed.");
sub->add_flag("-j,--json", state.output.json, "Output the list as a json.");
addConfigSelection(sub, true)->excludes(all);
addLoggingOptions(sub, false, true);
state.subcommand.list = sub;
}
{
auto* sub = cli->add_subcommand("kill", "Kill quickshell instances.");
//sub->add_flag("-a,--all", "Kill all matching instances instead of just one.");
auto* instance = addInstanceSelection(sub);
addConfigSelection(sub, true)->excludes(instance);
addLoggingOptions(sub, false, true);
state.subcommand.kill = sub;
}
{
auto* sub = cli->add_subcommand("ipc", "Communicate with other Quickshell instances.")
->require_subcommand();
state.ipc.ipc = sub;
auto* instance = addInstanceSelection(sub);
addConfigSelection(sub, true)->excludes(instance);
addLoggingOptions(sub, false, true);
{
auto* show = sub->add_subcommand("show", "Print information about available IPC targets.");
state.ipc.show = show;
}
{
auto* call = sub->add_subcommand("call", "Call an IpcHandler function.");
state.ipc.call = call;
call->add_option("target", state.ipc.target, "The target to message.");
call->add_option("function", state.ipc.name)
->description("The function to call in the target.");
call->add_option("arguments", state.ipc.arguments)
->description("Arguments to the called function.")
->allow_extra_args();
}
{
auto* callJson = sub->add_subcommand(
"callJson",
"Call an IpcHandler function with a single JSON argument."
);
state.ipc.callJson = callJson;
callJson->add_option("target", state.ipc.target, "The target to message.");
callJson->add_option("function", state.ipc.name)
->description("The function to call in the target.");
callJson->add_option("json", state.ipc.jsonArgument)
->description("JSON string sent the called function.");
}
{
auto* prop =
sub->add_subcommand("prop", "Manipulate IpcHandler properties.")->require_subcommand();
{
auto* get = prop->add_subcommand("get", "Read the value of a property.");
state.ipc.getprop = get;
get->add_option("target", state.ipc.target, "The target to read the property of.");
get->add_option("property", state.ipc.name)->description("The property to read.");
}
}
}
{
auto* sub = cli->add_subcommand("msg", "[DEPRECATED] Moved to `ipc call`.")->require_option();
sub->add_option("target", state.ipc.target, "The target to message.");
sub->add_option("function", state.ipc.name)->description("The function to call in the target.");
sub->add_option("arguments", state.ipc.arguments)
->description("Arguments to the called function.")
->allow_extra_args();
sub->add_flag("-s,--show", state.ipc.showOld)
->description("Print information about a function or target if given, or all available "
"targets if not.");
auto* instance = addInstanceSelection(sub);
addConfigSelection(sub, true)->excludes(instance);
addLoggingOptions(sub, false, true);
state.subcommand.msg = sub;
}
CLI11_PARSE(*cli, argc, argv);
return 65535;
}
} // namespace qs::launch