core-legacy/src/helpers/parse_cli.c

64 lines
1.5 KiB
C
Raw Normal View History

2020-05-05 09:42:02 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <argparse.h>
2020-08-13 09:26:06 +00:00
2020-05-05 09:42:02 +00:00
#include <config.h>
#include <logger.h>
#include <helpers.h>
2020-06-16 23:52:06 +00:00
#include <version.h>
2020-05-05 09:42:02 +00:00
static const char *const usage[] = {
"core [options] [[--] args]",
"core [options]",
2020-05-05 09:42:02 +00:00
NULL,
};
void
helper_parse_cli(int argc, const char **argv, config_t *config)
{
2020-06-16 23:52:06 +00:00
int version = 0;
2020-05-05 09:42:02 +00:00
struct argparse_option options[] =
{
OPT_HELP(),
OPT_GROUP("Basic options"),
OPT_STRING('c', "config", &config->file, "path to config file", NULL, 0, OPT_NONEG),
2020-06-16 23:52:06 +00:00
OPT_BOOLEAN('v', "version", &version, "print version", NULL, 0, OPT_NONEG),
2020-05-05 09:42:02 +00:00
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, usage, 0);
argparse_describe(
&argparse,
"\nA brief description of what the program does and how it works.",
"\nAdditional description of the program after the description of the arguments."
);
argc = argparse_parse(&argparse, argc, argv);
2020-06-16 23:52:06 +00:00
if(version)
{
printf("%s\n", EMGAUWA_CORE_VERSION);
exit(0);
}
2020-05-05 09:42:02 +00:00
if(argc == 1)
{
config->run_type = RUN_TYPE_INVALID;
2020-05-05 09:42:02 +00:00
if(strcmp(argv[0], "start") == 0)
{
config->run_type = RUN_TYPE_START;
return;
}
2020-07-26 19:00:05 +00:00
LOGGER_CRIT("bad action '%s' given ('start')\n", argv[0]);
2020-05-05 09:42:02 +00:00
exit(1);
}
else
{
2020-07-26 19:00:05 +00:00
LOGGER_CRIT("no action given ('start')\n");
2020-05-05 09:42:02 +00:00
exit(1);
}
return;
}