Add command and event details to help output

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
pull/218/head
Jim Ramsay 2023-01-06 13:39:23 -05:00
parent d1e1f62d1e
commit dd19da6143
3 changed files with 50 additions and 20 deletions

View File

@ -25,6 +25,7 @@
#include <signal.h> #include <signal.h>
#include <assert.h> #include <assert.h>
#include <jansson.h> #include <jansson.h>
#include <sys/param.h>
#include "json-ipc.h" #include "json-ipc.h"
#include "ctl-client.h" #include "ctl-client.h"
@ -33,6 +34,7 @@
#include "strlcpy.h" #include "strlcpy.h"
#include "util.h" #include "util.h"
#include "option-parser.h" #include "option-parser.h"
#include "table-printer.h"
#define LOG(level, fmt, ...) \ #define LOG(level, fmt, ...) \
fprintf(stderr, "[%s:%d] <" level "> " fmt "\n", __FILE__, __LINE__, \ fprintf(stderr, "[%s:%d] <" level "> " fmt "\n", __FILE__, __LINE__, \
@ -49,11 +51,11 @@ static bool do_debug = false;
static struct cmd_info internal_events[] = { static struct cmd_info internal_events[] = {
{ .name = "wayvnc-startup", { .name = "wayvnc-startup",
.description = "Sent when a successful wayvnc control connection is established and event registration has succeeded, both upon initial startup and on subsequent registrations with --reconnect.", .description = "Sent by wayvncctl when a successful wayvnc control connection is established and event registration has succeeded, both upon initial startup and on subsequent registrations with --reconnect.",
.params = {{}}, .params = {{}},
}, },
{ .name = "wayvnc-shutdown", { .name = "wayvnc-shutdown",
.description = "Sent when the wayvnc control connection is dropped, usually due to wayvnc exiting.", .description = "Sent by wayvncctl when the wayvnc control connection is dropped, usually due to wayvnc exiting.",
.params = {{}}, .params = {{}},
}, },
}; };
@ -643,23 +645,39 @@ static int ctl_client_print_single_command(struct ctl_client* self,
void ctl_client_print_command_list(FILE* stream) void ctl_client_print_command_list(FILE* stream)
{ {
fprintf(stream, "Commands:\n"); fprintf(stream, "Commands:\n");
size_t max_namelen = 0;
for (size_t i = 0; i < CMD_LIST_LEN; ++i) { for (size_t i = 0; i < CMD_LIST_LEN; ++i) {
if (i == CMD_HELP) // hidden if (i == CMD_HELP) // hidden
continue; continue;
fprintf(stream, " %s\n", ctl_command_list[i].name); max_namelen = MAX(max_namelen, strlen(ctl_command_list[i].name));
}
struct table_printer printer;
table_printer_init(&printer, stdout, max_namelen);
for (size_t i = 0; i < CMD_LIST_LEN; ++i) {
if (i == CMD_HELP) // hidden
continue;
table_printer_print_line(&printer, ctl_command_list[i].name,
ctl_command_list[i].description);
} }
fprintf(stream, "\nRun 'wayvncctl command-name --help' for command-specific details.\n"); fprintf(stream, "\nRun 'wayvncctl command-name --help' for command-specific details.\n");
} }
static void print_event_info(const struct cmd_info* info) static void print_event_info(const struct cmd_info* info)
{ {
printf("Event: %s\n\n%s\n", info->name, printf("%s\n\n", info->name);
info->description); table_printer_indent_and_reflow_text(stdout, info->description, 80, 0, 0);
if (info->params[0].name != NULL) { if (info->params[0].name != NULL) {
printf("\nData fields:"); printf("\nData fields:\n");
size_t max_namelen = 0;
for (int i = 0; info->params[i].name != NULL; ++i) for (int i = 0; info->params[i].name != NULL; ++i)
printf("\n %s=...\n %s\n", info->params[i].name, max_namelen = MAX(max_namelen, strlen(info->params[i].name));
info->params[i].description);
struct table_printer printer;
table_printer_init(&printer, stdout, max_namelen);
for (int i = 0; info->params[i].name != NULL; ++i)
table_printer_print_fmtline(&printer,
info->params[i].description,
"%s=...", info->params[i].name);
} }
} }
@ -683,10 +701,21 @@ static int print_event_details(const char* evt_name)
void ctl_client_print_event_list(FILE* stream) void ctl_client_print_event_list(FILE* stream)
{ {
printf("Events:\n"); printf("Events:\n");
size_t max_namelen = 0;
for (size_t i = 0; i < EVT_LIST_LEN; ++i) for (size_t i = 0; i < EVT_LIST_LEN; ++i)
printf(" %s\n", ctl_event_list[i].name); max_namelen = MAX(max_namelen, strlen(ctl_event_list[i].name));
for (size_t i = 0; i < INTERNAL_EVT_LEN; ++i) for (size_t i = 0; i < INTERNAL_EVT_LEN; ++i)
printf(" %s\n", internal_events[i].name); max_namelen = MAX(max_namelen, strlen(internal_events[i].name));
struct table_printer printer;
table_printer_init(&printer, stdout, max_namelen);
for (size_t i = 0; i < EVT_LIST_LEN; ++i)
table_printer_print_line(&printer, ctl_event_list[i].name,
ctl_event_list[i].description);
for (size_t i = 0; i < INTERNAL_EVT_LEN; ++i)
table_printer_print_line(&printer, internal_events[i].name,
internal_events[i].description);
} }
static int print_command_usage(struct ctl_client* self, static int print_command_usage(struct ctl_client* self,
@ -695,7 +724,7 @@ static int print_command_usage(struct ctl_client* self,
struct option_parser* parent_options) struct option_parser* parent_options)
{ {
if (self->flags & CTL_CLIENT_PRINT_JSON) { if (self->flags & CTL_CLIENT_PRINT_JSON) {
WARN("JSON output is not supported for the \"help\" command"); WARN("JSON output is not supported for \"help\" output");
return 1; return 1;
} }
struct cmd_info* info = ctl_command_by_type(cmd); struct cmd_info* info = ctl_command_by_type(cmd);
@ -703,8 +732,9 @@ static int print_command_usage(struct ctl_client* self,
WARN("No such command"); WARN("No such command");
return 1; return 1;
} }
printf("Usage: wayvncctl [options] %s [parameters]\n\n%s\n\n", info->name, printf("Usage: wayvncctl [options] %s [parameters]\n\n", info->name);
info->description); table_printer_indent_and_reflow_text(stdout, info->description, 80, 0, 0);
printf("\n");
option_parser_print_options(cmd_options, stdout); option_parser_print_options(cmd_options, stdout);
printf("\n"); printf("\n");
option_parser_print_options(parent_options, stdout); option_parser_print_options(parent_options, stdout);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 Jim Ramsay * Copyright (c) 2022-2023 Jim Ramsay
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -75,15 +75,15 @@ struct cmd_info ctl_command_list[] = {
struct cmd_info ctl_event_list[] = { struct cmd_info ctl_event_list[] = {
[EVT_CLIENT_CONNECTED] = {"client-connected", [EVT_CLIENT_CONNECTED] = {"client-connected",
"Sent when a new vnc client connects to wayvnc", "Sent by wayvnc when a new vnc client connects",
{ CLIENT_EVENT_PARAMS("including") } { CLIENT_EVENT_PARAMS("including") }
}, },
[EVT_CLIENT_DISCONNECTED] = {"client-disconnected", [EVT_CLIENT_DISCONNECTED] = {"client-disconnected",
"Sent when a vnc client disconnects from wayvnc", "Sent by waynvc when a vnc client disconnects",
{ CLIENT_EVENT_PARAMS("not including") } { CLIENT_EVENT_PARAMS("not including") }
}, },
[EVT_CAPTURE_CHANGED] = {"capture-changed", [EVT_CAPTURE_CHANGED] = {"capture-changed",
"Sent when wayvnc changes which output is captured", "Sent by wayvnc when the catured output is changed",
{ {
{"output", "The name of the output now being captured"}, {"output", "The name of the output now being captured"},
{NULL, NULL}, {NULL, NULL},

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 Jim Ramsay * Copyright (c) 2022-2023 Jim Ramsay
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -44,7 +44,7 @@ struct wayvncctl {
static int wayvncctl_usage(FILE* stream, struct option_parser* options, int rc) static int wayvncctl_usage(FILE* stream, struct option_parser* options, int rc)
{ {
static const char* usage = static const char* usage =
"Usage: wayvncctl [options] [command [--param1=value1 ...]]\n" "Usage: wayvncctl [options] <command> [parameters]\n"
"\n" "\n"
"Connects to and interacts with a running wayvnc instance."; "Connects to and interacts with a running wayvnc instance.";
fprintf(stream, "%s\n\n", usage); fprintf(stream, "%s\n\n", usage);
@ -112,7 +112,7 @@ int main(int argc, char* argv[])
// No command; nothing to do... // No command; nothing to do...
if (!option_parser_get_value(&option_parser, "command")) if (!option_parser_get_value(&option_parser, "command"))
return 0; return wayvncctl_usage(stdout, &option_parser, 1);
ctl_client_debug_log(verbose); ctl_client_debug_log(verbose);