feat: basic cli & constants

This commit is contained in:
Jozef Steinhübl 2024-07-21 22:15:01 +02:00
parent 2bc2a5387e
commit 39714f1f3f
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
8 changed files with 111 additions and 4 deletions

View file

@ -6,6 +6,8 @@ set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED true) set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build) 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}) add_executable(gimi ${SRC})

25
src/cli/cli.c Normal file
View file

@ -0,0 +1,25 @@
#include "../gimi_constants.h"
#include "command/init.h"
#include <stdio.h>
#include <string.h>
#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;
}

25
src/cli/cli.h Normal file
View file

@ -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);

5
src/cli/command/init.c Normal file
View file

@ -0,0 +1,5 @@
#include "../cli.h"
#include <stdio.h>
#include <unistd.h>
void cli_command_init(int argc, char **argv) { printf("init"); }

1
src/cli/command/init.h Normal file
View file

@ -0,0 +1 @@
void cli_command_init(int argc, char **argv);

4
src/gimi_constants.h Normal file
View file

@ -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

4
src/gimi_constants.h.in Normal file
View file

@ -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@

View file

@ -1,6 +1,47 @@
#include <stdio.h> #include "cli/cli.h"
#include <string.h>
int main() { int main(int argc, char **argv) {
printf("hello!"); 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; return 0;
} }