diff --git a/.gitmodules b/.gitmodules index 0257eb5..f246ab5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/lib/cargs b/lib/cargs new file mode 160000 index 0000000..0698c3f --- /dev/null +++ b/lib/cargs @@ -0,0 +1 @@ +Subproject commit 0698c3f90333446d0fc2745c1e9ce10dd4a9497a diff --git a/meson.build b/meson.build index abd3876..e9ce887 100644 --- a/meson.build +++ b/meson.build @@ -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 ) diff --git a/src/main.c b/src/main.c index 846e73f..9560852 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,36 @@ #include #include +#include + #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);