Basic argument parsing

This commit is contained in:
buffet 2019-03-19 20:33:39 +01:00
parent 47a960678a
commit fe3798e48a
2 changed files with 98 additions and 6 deletions

View file

@ -5,17 +5,111 @@
* You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <wayland-server.h>
#include <wlr/util/log.h>
#include "kiwmi/server.h"
int
main(void)
struct kiwmi_config {
int verbosity;
const char *frontend_path;
};
struct option {
const char *name;
int val;
};
static void
parse_arguments(int argc, char **argv, struct kiwmi_config *config)
{
wlr_log_init(WLR_DEBUG, NULL);
static struct option options[] = {
{"help", 'h'},
{"version", 'v'},
{"verbose", 'V'},
{0, 0},
};
const char *usage =
"Usage: kiwmi [options] FRONTEND\n"
"\n"
" -h, --help Show help message and quit\n"
" -v, --version Show version number and quit\n"
" -V, --verbose Increase verbosity Level\n";
config->verbosity = 0;
config->frontend_path = NULL;
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':
printf("%s", usage);
exit(EXIT_SUCCESS);
break;
case 'v':
printf("kiwmi version " KIWMI_VERSION "\n");
exit(EXIT_SUCCESS);
break;
case 'V':
++config->verbosity;
break;
default:
fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE);
}
} else {
config->frontend_path = opt;
break;
}
}
if (!config->frontend_path) {
fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE);
}
}
int
main(int argc, char **argv)
{
struct kiwmi_config config;
parse_arguments(argc, argv, &config);
switch (config.verbosity) {
case 0:
wlr_log_init(WLR_ERROR, NULL);
break;
case 1:
wlr_log_init(WLR_INFO, NULL);
break;
default:
wlr_log_init(WLR_DEBUG, NULL);
}
fprintf(stderr, "Using kiwmi v" KIWMI_VERSION "\n");
struct kiwmi_server server;
@ -24,8 +118,6 @@ main(void)
exit(EXIT_FAILURE);
}
wlr_log(WLR_INFO, "Starting kiwmi v" KIWMI_VERSION);
if (!server_run(&server)) {
wlr_log(WLR_ERROR, "Failed to run server");
exit(EXIT_FAILURE);

View file

@ -11,7 +11,7 @@ project(
add_project_arguments(
[
'-DWLR_USE_UNSTABLE',
'-D_POSIX_C_SOURCE=200112L',
'-D_POSIX_C_SOURCE=200809L',
],
language: 'c',
)