Switch wayvncctl to use option_parser

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
pull/201/head
Jim Ramsay 2022-12-06 07:07:22 -05:00
parent e3238cf71d
commit 4def8f3cb8
5 changed files with 45 additions and 39 deletions

View File

@ -33,7 +33,10 @@ struct option_parser {
int n_opts; int n_opts;
}; };
void option_parser_init(struct option_parser* self, const struct wv_option* options); #define OPTION_PARSER_CHECK_ALL_ARGUMENTS 0
#define OPTION_PARSER_STOP_ON_FIRST_NONOPTION 1
void option_parser_print_usage(struct option_parser* self, FILE* stream); void option_parser_init(struct option_parser* self, const struct wv_option* options, unsigned flags);
void option_parser_print_options(struct option_parser* self, FILE* stream);
int option_parser_getopt(struct option_parser* self, int argc, char* argv[]); int option_parser_getopt(struct option_parser* self, int argc, char* argv[]);

View File

@ -120,6 +120,7 @@ ctlsources = [
'src/json-ipc.c', 'src/json-ipc.c',
'src/ctl-client.c', 'src/ctl-client.c',
'src/strlcpy.c', 'src/strlcpy.c',
'src/option-parser.c',
] ]
ctldependencies = [ ctldependencies = [

View File

@ -909,7 +909,7 @@ void on_capture_done(struct screencopy* sc)
int wayvnc_usage(struct option_parser* parser, FILE* stream, int rc) int wayvnc_usage(struct option_parser* parser, FILE* stream, int rc)
{ {
fprintf(stream, "Usage: wayvnc [options] [address [port]]\n\n"); fprintf(stream, "Usage: wayvnc [options] [address [port]]\n\n");
option_parser_print_usage(parser, stream); option_parser_print_options(parser, stream);
fprintf(stream, "\n"); fprintf(stream, "\n");
return rc; return rc;
} }
@ -1297,7 +1297,8 @@ int main(int argc, char* argv[])
}; };
struct option_parser option_parser; struct option_parser option_parser;
option_parser_init(&option_parser, opts); option_parser_init(&option_parser, opts,
OPTION_PARSER_CHECK_ALL_ARGUMENTS);
while (1) { while (1) {
int c = option_parser_getopt(&option_parser, argc, argv); int c = option_parser_getopt(&option_parser, argc, argv);

View File

@ -30,7 +30,8 @@ static int count_options(const struct wv_option* opts)
} }
void option_parser_init(struct option_parser* self, void option_parser_init(struct option_parser* self,
const struct wv_option* options) const struct wv_option* options,
unsigned flags)
{ {
memset(self, 0, sizeof(*self)); memset(self, 0, sizeof(*self));
@ -40,6 +41,9 @@ void option_parser_init(struct option_parser* self,
int short_opt_index = 0; int short_opt_index = 0;
int long_opt_index = 0; int long_opt_index = 0;
if (flags && OPTION_PARSER_STOP_ON_FIRST_NONOPTION)
self->short_opts[short_opt_index++] = '+';
for (int i = 0; i < self->n_opts; ++i) { for (int i = 0; i < self->n_opts; ++i) {
assert(options[i].short_opt); // TODO: Make this optional? assert(options[i].short_opt); // TODO: Make this optional?
@ -157,8 +161,9 @@ static void format_option(const struct wv_option* opt, int left_col_width,
} }
} }
void option_parser_print_usage(struct option_parser* self, FILE* stream) void option_parser_print_options(struct option_parser* self, FILE* stream)
{ {
fprintf(stream, "Options:\n");
int left_col_width = get_left_col_width(self->options, self->n_opts); int left_col_width = get_left_col_width(self->options, self->n_opts);
for (int i = 0; i < self->n_opts; ++i) { for (int i = 0; i < self->n_opts; ++i) {

View File

@ -32,6 +32,7 @@
#include "util.h" #include "util.h"
#include "ctl-client.h" #include "ctl-client.h"
#include "option-parser.h"
#define MAYBE_UNUSED __attribute__((unused)) #define MAYBE_UNUSED __attribute__((unused))
@ -41,30 +42,16 @@ struct wayvncctl {
struct ctl_client* ctl; struct ctl_client* ctl;
}; };
static int wayvncctl_usage(FILE* stream, 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 [--param1=value1 ...]]\n"
"\n" "\n"
"Connects to and interacts with a running wayvnc instance." "Connects to and interacts with a running wayvnc instance."
"\n" "\n"
"Try 'wayvncctl help' for a list of available commands.\n" "Try 'wayvncctl help' for a list of available commands.";
"\n" fprintf(stream, "%s\n\n", usage);
"Options:\n" option_parser_print_options(options, stream);
" -S,--socket=<path> Wayvnc control socket path.\n"
" Default: $XDG_RUNTIME_DIR/wayvncctl\n"
" -w,--wait Wait for wayvnc to start up if it's\n"
" not already running.\n"
" -r,--reconnect If disconnected while waiting for\n"
" events, wait for wayvnc to restart.\n"
" -j,--json Output json on stdout.\n"
" -V,--version Show version info.\n"
" -v,--verbose Be more verbose.\n"
" -h,--help Get help (this text).\n"
"\n";
fprintf(stream, "%s", usage);
return rc; return rc;
} }
@ -78,27 +65,36 @@ int main(int argc, char* argv[])
{ {
struct wayvncctl self = { 0 }; struct wayvncctl self = { 0 };
static const char* shortopts = "+S:hVvjwr";
bool verbose = false; bool verbose = false;
const char* socket_path = NULL; const char* socket_path = NULL;
int wait_for_socket = 0; int wait_for_socket = 0;
unsigned flags = 0; unsigned flags = 0;
static const struct option longopts[] = { static const struct wv_option opts[] = {
{ "socket", required_argument, NULL, 'S' }, { 'S', "socket", "<path>",
{ "wait", no_argument, NULL, 'w' }, "Control socket path." },
{ "reconnect", no_argument, NULL, 'r' }, { 'w', "wait", NULL,
{ "json", no_argument, NULL, 'j' }, "Wait for wayvnc to start up if it's not already running." },
{ "help", no_argument, NULL, 'h' }, { 'r', "reconnect", NULL,
{ "version", no_argument, NULL, 'V' }, "If disconnected while waiting for events, wait for wayvnc to restart." },
{ "verbose", no_argument, NULL, 'v' }, { 'j', "json", NULL,
{ NULL, 0, NULL, 0 } "Output json on stdout." },
{ 'V', "version", NULL,
"Show version info." },
{ 'v', "verbose", NULL,
"Be more verbose." },
{ 'h', "help", NULL,
"Get help (this text)." },
{ '\0', NULL, NULL, NULL }
}; };
struct option_parser option_parser;
option_parser_init(&option_parser, opts,
OPTION_PARSER_STOP_ON_FIRST_NONOPTION);
while (1) { while (1) {
int c = getopt_long(argc, argv, shortopts, longopts, NULL); int c = option_parser_getopt(&option_parser, argc, argv);
if (c < 0) if (c < 0)
break; break;
@ -121,14 +117,14 @@ int main(int argc, char* argv[])
case 'V': case 'V':
return show_version(); return show_version();
case 'h': case 'h':
return wayvncctl_usage(stdout, 0); return wayvncctl_usage(stdout, &option_parser, 0);
default: default:
return wayvncctl_usage(stderr, 1); return wayvncctl_usage(stderr, &option_parser, 1);
} }
} }
argc -= optind; argc -= optind;
if (argc <= 0) if (argc <= 0)
return wayvncctl_usage(stderr, 1); return wayvncctl_usage(stderr, &option_parser, 1);
argv = &argv[optind]; argv = &argv[optind];
ctl_client_debug_log(verbose); ctl_client_debug_log(verbose);