switch to cargs
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -4,3 +4,6 @@
|
||||
[submodule "lib/ds"]
|
||||
path = lib/ds
|
||||
url = https://git.squi.bid/squibid/ds
|
||||
[submodule "lib/cargs"]
|
||||
path = lib/cargs
|
||||
url = https://github.com/likle/cargs
|
||||
|
1
lib/cargs
Submodule
1
lib/cargs
Submodule
Submodule lib/cargs added at 0698c3f903
@@ -37,6 +37,7 @@ executable('wom', srcfiles,
|
||||
|
||||
include_directories('lib/log.c/src'),
|
||||
include_directories('lib/ds'),
|
||||
include_directories('lib/cargs/include'),
|
||||
],
|
||||
link_with: [
|
||||
static_library('ds', 'lib/ds/ds.c',
|
||||
@@ -44,6 +45,9 @@ executable('wom', srcfiles,
|
||||
|
||||
static_library('log.c', 'lib/log.c/src/log.c',
|
||||
include_directories: 'lib/log.c/src'),
|
||||
|
||||
static_library('cargs', 'lib/cargs/src/cargs.c',
|
||||
include_directories: 'lib/cargs/include'),
|
||||
],
|
||||
install: true
|
||||
)
|
||||
|
59
src/main.c
59
src/main.c
@@ -6,12 +6,36 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cargs.h>
|
||||
|
||||
#include "conf.h"
|
||||
#include "lua/wom.h"
|
||||
#include "lua/wom_fs.h"
|
||||
#include "subcmds.h"
|
||||
#include "api.h"
|
||||
|
||||
static struct cag_option options[] = {
|
||||
{
|
||||
.identifier = 'v',
|
||||
.access_letters = "v",
|
||||
.access_name = "version",
|
||||
.description = "get the version of womblic",
|
||||
},
|
||||
{
|
||||
.identifier = 'c',
|
||||
.access_letters = "c",
|
||||
.access_name = "config",
|
||||
.value_name = "PATH",
|
||||
.description = "load an alternative config file"
|
||||
},
|
||||
{
|
||||
.identifier = 'h',
|
||||
.access_letters = "h",
|
||||
.access_name = "help",
|
||||
.description = "Shows the command help",
|
||||
}
|
||||
};
|
||||
|
||||
static lua_State
|
||||
*load_wom_lua_lib(lua_State *L)
|
||||
{
|
||||
@@ -42,24 +66,31 @@ static lua_State
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int c, l;
|
||||
/* FIXME: config path can't be freed after being passed to lualib which causes
|
||||
* a minor memory leak */
|
||||
char *config_path = { 0 };
|
||||
int l;
|
||||
lua_State *L;
|
||||
const char *cag_path;
|
||||
char *config_path;
|
||||
bool show_help = false;
|
||||
cag_option_context context;
|
||||
|
||||
/* general options for womblic */
|
||||
while ((c = getopt(argc, argv, "hvc:")) != -1) {
|
||||
switch (c) {
|
||||
cag_option_init(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
|
||||
while (cag_option_fetch(&context)) {
|
||||
switch (cag_option_get_identifier(&context)) {
|
||||
case 'c':
|
||||
l = strlen(optarg);
|
||||
cag_path = cag_option_get_value(&context);
|
||||
l = strlen(cag_path);
|
||||
config_path = calloc(l + 1, sizeof(char));
|
||||
strncpy(config_path, optarg, l);
|
||||
break;
|
||||
strncpy(config_path, cag_path, l);
|
||||
break;
|
||||
case 'v': printf("%s-%s\n", argv[0], VERSION); break;
|
||||
case 'h':
|
||||
default:
|
||||
printf("help text\n");
|
||||
printf("Usage: wom [OPTION]...\n");
|
||||
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
|
||||
show_help = true;
|
||||
break;
|
||||
case '?':
|
||||
cag_option_print_error(&context, stdout);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -86,6 +117,12 @@ main(int argc, char *argv[])
|
||||
lua_pop(L, lua_gettop(L));
|
||||
}
|
||||
|
||||
/* show help info about which subcmds are available */
|
||||
if (show_help) {
|
||||
subcmds_subcmd(NULL, 0, NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
run_subcmds(argc, argv);
|
||||
|
||||
lua_close(L);
|
||||
|
Reference in New Issue
Block a user