tools: add getopt argument parsing

Add some help, version, remote options for tools
Add option for output filename in pw-profiler
Add option to start pw-cli as daemon or not, make it connect to the
default PipeWire instance by default (instead of local instance)
This commit is contained in:
Wim Taymans 2020-03-24 16:36:48 +01:00
parent a1846c9780
commit 646088b90c
4 changed files with 158 additions and 25 deletions

View file

@ -24,6 +24,7 @@
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
@ -678,14 +679,51 @@ static void do_quit(void *data, int signal_number)
pw_main_loop_quit(d->loop);
}
static void show_help(const char *name)
{
fprintf(stdout, "%s [options]\n"
" -h, --help Show this help\n"
" -v, --version Show version\n"
" -r, --remote Remote daemon name\n",
name);
}
int main(int argc, char *argv[])
{
struct data data = { 0 };
struct pw_loop *l;
struct pw_properties *props = NULL;
const char *opt_remote = NULL;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"remote", 1, NULL, 'r'},
{NULL, 0, NULL, 0}
};
int c;
pw_init(&argc, &argv);
while ((c = getopt_long(argc, argv, "hvr:", long_options, NULL)) != -1) {
switch (c) {
case 'h':
show_help(argv[0]);
return 0;
case 'v':
fprintf(stdout, "%s\n"
"Compiled with libpipewire %s\n"
"Linked with libpipewire %s\n",
argv[0],
pw_get_headers_version(),
pw_get_library_version());
return 0;
case 'r':
opt_remote = optarg;
break;
default:
return -1;
}
}
data.loop = pw_main_loop_new(NULL);
if (data.loop == NULL)
return -1;
@ -698,12 +736,13 @@ int main(int argc, char *argv[])
if (data.context == NULL)
return -1;
if (argc > 1)
props = pw_properties_new(PW_KEY_REMOTE_NAME, argv[1], NULL);
spa_list_init(&data.pending_list);
data.core = pw_context_connect(data.context, props, 0);
data.core = pw_context_connect(data.context,
pw_properties_new(
PW_KEY_REMOTE_NAME, opt_remote,
NULL),
0);
if (data.core == NULL)
return -1;