Skip to content

Commit 7e477ab

Browse files
clickhouse/ch aliases can invoke clickhouse-client if connection parameters are specified
1 parent 95616d3 commit 7e477ab

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

programs/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,22 @@ int main(int argc_, char ** argv_)
283283
}
284284
}
285285

286+
/// If host/port arguments are passed to clickhouse/ch shortcuts,
287+
/// interpret it as clickhouse-client invocation for usability.
288+
if (main_func == printHelp && argv.size() >= 2)
289+
{
290+
for (size_t i = 1, num_args = argv.size(); i < num_args; ++i)
291+
{
292+
if ((i + 1 < num_args && argv[i] == std::string_view("--host")) || startsWith(argv[i], "--host=")
293+
|| (i + 1 < num_args && argv[i] == std::string_view("--port")) || startsWith(argv[i], "--port=")
294+
|| startsWith(argv[i], "-h"))
295+
{
296+
main_func = mainEntryClickHouseClient;
297+
break;
298+
}
299+
}
300+
}
301+
286302
/// Interpret binary without argument or with arguments starts with dash
287303
/// ('-') as clickhouse-local for better usability:
288304
///
@@ -295,6 +311,7 @@ int main(int argc_, char ** argv_)
295311
///
296312
std::error_code ec;
297313
if (main_func == printHelp && !argv.empty()
314+
&& (argv.size() < 2 || argv[1] != std::string_view("--help"))
298315
&& (argv.size() == 1 || argv[1][0] == '-' || std::string_view(argv[1]).contains(' ')
299316
|| std::filesystem::is_regular_file(std::filesystem::path{argv[1]}, ec)))
300317
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Use one of the following commands:
2+
Use one of the following commands:
3+
Use one of the following commands:
4+
Overlay
5+
Overlay
6+
Atomic
7+
Atomic
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4+
# shellcheck source=../shell_config.sh
5+
. "$CUR_DIR"/../shell_config.sh
6+
7+
CLICKHOUSE_BINARY_CH=${CLICKHOUSE_BINARY/clickhouse/ch}
8+
9+
# Invocation with unknown tool name prints help:
10+
${CLICKHOUSE_BINARY} test 2>&1 | grep -F 'Use one of the following commands'
11+
12+
# Invocation with --help works the same:
13+
${CLICKHOUSE_BINARY} --help 2>&1 | grep -F 'Use one of the following commands'
14+
${CLICKHOUSE_BINARY_CH} --help 2>&1 | grep -F 'Use one of the following commands'
15+
16+
# This is recognized as clickhouse-local:
17+
${CLICKHOUSE_BINARY} --query "SELECT engine FROM system.databases WHERE name = currentDatabase()"
18+
${CLICKHOUSE_BINARY_CH} --query "SELECT engine FROM system.databases WHERE name = currentDatabase()"
19+
20+
# This is recognized as clickhouse-client:
21+
${CLICKHOUSE_BINARY} --host ${CLICKHOUSE_HOST} --port ${CLICKHOUSE_PORT_TCP} --query "SELECT engine FROM system.databases WHERE name = currentDatabase()"
22+
${CLICKHOUSE_BINARY_CH} --query "SELECT engine FROM system.databases WHERE name = currentDatabase()" -h${CLICKHOUSE_HOST} --port=${CLICKHOUSE_PORT_TCP}

0 commit comments

Comments
 (0)