Add help output for wayvncctl positional arguments

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
wayvncctl-polishing
Jim Ramsay 2023-01-08 12:14:32 -05:00
parent 23527a095a
commit 405e9a13df
3 changed files with 33 additions and 1 deletions

View File

@ -50,6 +50,8 @@ struct option_parser {
void option_parser_init(struct option_parser* self,
const struct wv_option* options);
int option_parser_print_arguments(struct option_parser* self, FILE* stream);
void option_parser_print_options(struct option_parser* self, FILE* stream);
int option_parser_parse(struct option_parser* self, int argc,

View File

@ -737,9 +737,12 @@ static int print_command_usage(struct ctl_client* self,
for (int i = 0; i < cmd_options->n_opts; ++i)
if (cmd_options->options[i].positional)
printf("<%s> ", cmd_options->options[i].positional);
printf("[parameters]\n\n");
table_printer_indent_and_reflow_text(stdout, info->description, 80, 0, 0);
printf("\n");
if (option_parser_print_arguments(cmd_options, stdout))
printf("\n");
option_parser_print_options(cmd_options, stdout);
printf("\n");
option_parser_print_options(parent_options, stdout);
@ -772,6 +775,7 @@ int ctl_client_init_cmd_parser(struct option_parser* parser, enum cmd_type cmd)
if (param_count == 1) {
// Represent a single parameter as a positional argument
options[0].positional = info->params[0].name;
options[0].help = info->params[0].description;
i++;
} else {
for (; i < param_count; ++i) {

View File

@ -22,6 +22,7 @@
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/param.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
@ -75,7 +76,7 @@ static int get_left_col_width(const struct wv_option* opts, int n)
static void format_option(struct table_printer* printer, const struct wv_option* opt)
{
if (!opt->help)
if (!opt->help || opt->positional)
return;
int n_chars = 0;
@ -104,6 +105,31 @@ void option_parser_print_options(struct option_parser* self, FILE* stream)
format_option(&printer, &self->options[i]);
}
int option_parser_print_arguments(struct option_parser* self, FILE* stream)
{
size_t max_arg = 0;
for (int i = 0; i < self->n_opts; ++i) {
const struct wv_option* opt = &self->options[i];
if (!opt->positional || !opt->help || opt->is_subcommand)
continue;
max_arg = MAX(max_arg, strlen(opt->positional));
}
if (!max_arg)
return 0;
fprintf(stream, "Arguments:\n");
struct table_printer printer;
table_printer_init(&printer, stream, max_arg);
int i;
for (i = 0; i < self->n_opts; ++i) {
const struct wv_option* opt = &self->options[i];
if (!opt->positional || !opt->help || opt->is_subcommand)
continue;
table_printer_print_line(&printer, opt->positional, opt->help);
}
return i;
}
static const struct wv_option* find_long_option(
const struct option_parser* self, const char* name)
{