diff --git a/CMakeLists.txt b/CMakeLists.txt index 77483e8..9b3db5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED true) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build) -file(GLOB_RECURSE SRC ./src/*.c ./src/*.h) +file(GLOB_RECURSE SRC CONFIGURE_DEPENDS ./src/*.c ./src/*.h) + +configure_file(src/gimi_constants.h.in src/gimi_constants.h) add_executable(gimi ${SRC}) diff --git a/src/cli/cli.c b/src/cli/cli.c new file mode 100644 index 0000000..600eb84 --- /dev/null +++ b/src/cli/cli.c @@ -0,0 +1,25 @@ +#include "../gimi_constants.h" +#include "command/init.h" +#include +#include + +#define HELP \ + "Usage: gimi [-h | --help] [-v | --version]\n\n" \ + "A simple tool for managing multiple git remotes as mirrors\n\n" \ + "Commands\n" + +void cli_print_help() { printf("%s", HELP); } +void cli_print_version() { + printf("gimi version %d.%d.%d\n", GIMI_VERSION_MAJOR, GIMI_VERSION_MINOR, + GIMI_VERSION_PATCH); +} + +int cli_handle(int argc, char **argv) { + char *sub_command = argv[0]; + + if (strcmp(sub_command, "init") == 0) { + cli_command_init(argc, argv); + } + + return 0; +} diff --git a/src/cli/cli.h b/src/cli/cli.h new file mode 100644 index 0000000..12b01a6 --- /dev/null +++ b/src/cli/cli.h @@ -0,0 +1,25 @@ +#define HANDLE_OPTIONS(argc, argv, optstring, ...) \ + int opt; \ + opterr = 0; \ + while ((opt = getopt(argc, argv, optstring)) != -1) { \ + switch (opt) { \ + __VA_ARGS__ \ + default: { \ + printf("unknown option -%c\n", (char)optopt); \ + break; \ + } \ + } \ + } \ + argc -= optind; \ + argv += optind; + +#define OPTION(opt, func) \ + case opt: { \ + func(); \ + break; \ + } + +void cli_print_help(); +void cli_print_version(); + +int cli_handle(int argc, char **argv); diff --git a/src/cli/command/init.c b/src/cli/command/init.c new file mode 100644 index 0000000..d469162 --- /dev/null +++ b/src/cli/command/init.c @@ -0,0 +1,5 @@ +#include "../cli.h" +#include +#include + +void cli_command_init(int argc, char **argv) { printf("init"); } diff --git a/src/cli/command/init.h b/src/cli/command/init.h new file mode 100644 index 0000000..24d1da9 --- /dev/null +++ b/src/cli/command/init.h @@ -0,0 +1 @@ +void cli_command_init(int argc, char **argv); diff --git a/src/gimi_constants.h b/src/gimi_constants.h new file mode 100644 index 0000000..ae8ad72 --- /dev/null +++ b/src/gimi_constants.h @@ -0,0 +1,4 @@ +// Do not edit manually! Generated by cmake +#define GIMI_VERSION_MAJOR 0 +#define GIMI_VERSION_MINOR 1 +#define GIMI_VERSION_PATCH 0 diff --git a/src/gimi_constants.h.in b/src/gimi_constants.h.in new file mode 100644 index 0000000..30e4842 --- /dev/null +++ b/src/gimi_constants.h.in @@ -0,0 +1,4 @@ +// Do not edit manually! Generated by cmake +#define GIMI_VERSION_MAJOR @gimi_VERSION_MAJOR@ +#define GIMI_VERSION_MINOR @gimi_VERSION_MINOR@ +#define GIMI_VERSION_PATCH @gimi_VERSION_PATCH@ diff --git a/src/main.c b/src/main.c index ad4274c..f345022 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,47 @@ -#include +#include "cli/cli.h" +#include -int main() { - printf("hello!"); +int main(int argc, char **argv) { + if (argc == 1) { + cli_print_help(); + return 0; + } + + if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { + cli_print_help(); + return 0; + } + + if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { + cli_print_version(); + return 0; + } + + argc -= 1; + argv += 1; + + cli_handle(argc, argv); + + /*int opt; + opterr = 0; + while ((opt = getopt(argc, argv, "hv")) != -1) { + switch (opt) { + case 'h': { + print_help(); + break; + } + case 'v': { + print_version(); + break; + } + default: { + printf("unknown option -%c\n", (char)optopt); + return 1; + } + } + } + argc -= optind; + argv += optind; +*/ return 0; }