Skip to content

Commit d0cb452

Browse files
committed
Fix multiple options accepted for single-value options
Currently single-value options such as --tunnel will accept multiple values, e.g. --tunnel "<...>" --tunnel "<...>". Confusingly for the user only the last value will be used without any warning. This commit fixes this by emitting an error message and exiting the program if multiple option values were supplied when single value was expected.
1 parent bca3f9e commit d0cb452

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/terminal/TerminalClientMain.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ void handleParseException(std::exception& e, cxxopts::Options& options) {
2929
exit(1);
3030
}
3131

32+
template <class T, class DefaultT>
33+
T extractSingleOptionWithDefault(const cxxopts::ParseResult& result,
34+
const cxxopts::Options& options,
35+
const string& name, DefaultT defaultValue) {
36+
auto count = result.count(name);
37+
if (count == 0) {
38+
return defaultValue;
39+
}
40+
if (count == 1) {
41+
return result[name].as<T>();
42+
}
43+
CLOG(INFO, "stdout") << "Value for " << name
44+
<< " must be specified only once\n";
45+
CLOG(INFO, "stdout") << options.help({}) << endl;
46+
exit(0);
47+
}
48+
3249
int main(int argc, char** argv) {
3350
WinsockContext context;
3451
string tmpDir = GetTempDirectory();
@@ -222,10 +239,9 @@ int main(int argc, char** argv) {
222239
string host_alias = destinationHost;
223240

224241
string jumphost =
225-
result.count("jumphost") ? result["jumphost"].as<string>() : "";
226-
int keepaliveDuration = result.count("keepalive")
227-
? result["keepalive"].as<int>()
228-
: MAX_CLIENT_KEEP_ALIVE_DURATION;
242+
extractSingleOptionWithDefault<string>(result, options, "jumphost", "");
243+
int keepaliveDuration = extractSingleOptionWithDefault<int>(
244+
result, options, "keepalive", MAX_CLIENT_KEEP_ALIVE_DURATION);
229245
if (keepaliveDuration < 1 ||
230246
keepaliveDuration > MAX_CLIENT_KEEP_ALIVE_DURATION) {
231247
CLOG(INFO, "stdout") << "Keep-alive duration must between 1 and "
@@ -361,10 +377,9 @@ int main(int argc, char** argv) {
361377
TelemetryService::get()->logToDatadog("Session Started", el::Level::Info,
362378
__FILE__, __LINE__);
363379
string tunnel_arg =
364-
result.count("tunnel") ? result["tunnel"].as<string>() : "";
365-
string r_tunnel_arg = result.count("reversetunnel")
366-
? result["reversetunnel"].as<string>()
367-
: "";
380+
extractSingleOptionWithDefault<string>(result, options, "tunnel", "");
381+
string r_tunnel_arg = extractSingleOptionWithDefault<string>(
382+
result, options, "reversetunnel", "");
368383
TerminalClient terminalClient(clientSocket, clientPipeSocket,
369384
socketEndpoint, id, passkey, console,
370385
is_jumphost, tunnel_arg, r_tunnel_arg,

0 commit comments

Comments
 (0)