From 9ba12e5496c49f48f40ad52e816641dfdbb0474e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 7 Jul 2023 22:13:00 +0200 Subject: test: add some basic command-line usage tests --- src/client.c | 24 +++++++++++++++--------- src/client.h | 2 +- src/client_main.c | 6 ++++-- 3 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index b783afe..5259d52 100644 --- a/src/client.c +++ b/src/client.c @@ -6,6 +6,7 @@ */ #include "client.h" +#include "cmd_line.h" #include "compiler.h" #include "log.h" #include "msg.h" @@ -35,25 +36,30 @@ void client_destroy(struct client *client) free(client); } -int client_main(UNUSED const struct client *client, const struct settings *settings, +int client_main(UNUSED const struct client *client, const struct settings *settings, int argc, const char **argv) { struct msg *response = NULL; int ret = 0; - ret = msg_connect_and_talk_argv(settings->host, settings->port, argv, &response); - if (ret < 0) - return ret; + if (argc < 1) { + exit_with_usage_err("no message to send to the server"); + return -1; + } - if (!msg_is_success(response)) { - log_err("Server failed to process the request\n"); - msg_dump(response); - ret = -1; + ret = msg_connect_and_talk_argv(settings->host, settings->port, argv, &response); + if (ret < 0 || !response || !msg_is_success(response)) { + log_err("Failed to connect to server or it couldn't process the request\n"); + if (response) + msg_dump(response); + if (!ret) + ret = -1; goto free_response; } free_response: - msg_free(response); + if (response) + msg_free(response); return ret; } diff --git a/src/client.h b/src/client.h index 471a7be..ff91170 100644 --- a/src/client.h +++ b/src/client.h @@ -18,6 +18,6 @@ struct client; int client_create(struct client **); void client_destroy(struct client *); -int client_main(const struct client *, const struct settings *, const char **argv); +int client_main(const struct client *, const struct settings *, int argc, const char **argv); #endif diff --git a/src/client_main.c b/src/client_main.c index 0a68a55..ac092d1 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -24,7 +24,9 @@ static struct settings default_settings(void) const char *get_usage_string(void) { - return "[-h|--help] [-V|--version] [-v|--verbose] [-H|--host HOST] [-p|--port PORT]"; + /* clang-format off */ + return "[-h|--help] [-V|--version] [-v|--verbose] [-H|--host HOST] [-p|--port PORT] ACTION [ARG...]"; + /* clang-format on */ } static int parse_settings(struct settings *settings, int argc, char *argv[]) @@ -84,7 +86,7 @@ int main(int argc, char *argv[]) if (ret < 0) return ret; - ret = client_main(client, &settings, (const char **)argv + optind); + ret = client_main(client, &settings, argc - optind, (const char **)argv + optind); if (ret < 0) goto destroy_client; -- cgit v1.2.3