Not worth implementing my own parser for those 3 options

This commit is contained in:
buffet 2019-03-19 21:58:29 +01:00
parent fe3798e48a
commit 99bcc1dc51

View file

@ -9,6 +9,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <getopt.h>
#include <limits.h> #include <limits.h>
#include <wayland-server.h> #include <wayland-server.h>
@ -16,26 +17,11 @@
#include "kiwmi/server.h" #include "kiwmi/server.h"
struct kiwmi_config { int
int verbosity; main(int argc, char **argv)
const char *frontend_path;
};
struct option {
const char *name;
int val;
};
static void
parse_arguments(int argc, char **argv, struct kiwmi_config *config)
{ {
static struct option options[] = { int verbosity = 0;
{"help", 'h'}, const char *frontend_path = NULL;
{"version", 'v'},
{"verbose", 'V'},
{0, 0},
};
const char *usage = const char *usage =
"Usage: kiwmi [options] FRONTEND\n" "Usage: kiwmi [options] FRONTEND\n"
@ -44,27 +30,9 @@ parse_arguments(int argc, char **argv, struct kiwmi_config *config)
" -v, --version Show version number and quit\n" " -v, --version Show version number and quit\n"
" -V, --verbose Increase verbosity Level\n"; " -V, --verbose Increase verbosity Level\n";
config->verbosity = 0; int option;
config->frontend_path = NULL; while ((option = getopt(argc, argv, "hvV")) != -1) {
switch (option) {
for (int i = 1; i < argc; ++i) {
const char *opt = argv[i];
char val = '?';
if (opt[0] == '-') {
if (opt[1] == '-') {
for (const struct option *current = options; current->name;
++current) {
if (strcmp(&opt[2], current->name)) {
val = current->val;
break;
}
}
} else {
val = opt[1];
}
switch (val) {
case 'h': case 'h':
printf("%s", usage); printf("%s", usage);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -74,31 +42,23 @@ parse_arguments(int argc, char **argv, struct kiwmi_config *config)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'V': case 'V':
++config->verbosity; ++verbosity;
break; break;
default: default:
fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else {
config->frontend_path = opt;
break;
}
} }
if (!config->frontend_path) { // no frontend passsed
if (optind >= argc) {
fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}
int frontend_path = argv[optind];
main(int argc, char **argv)
{
struct kiwmi_config config;
parse_arguments(argc, argv, &config);
switch (config.verbosity) { switch (verbosity) {
case 0: case 0:
wlr_log_init(WLR_ERROR, NULL); wlr_log_init(WLR_ERROR, NULL);
break; break;