diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2021-04-26 07:40:37 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2021-04-26 07:40:37 +0200 |
commit | e6279af295cc0e8b985a5a43a16ac6313eb6c698 (patch) | |
tree | 52f39915b61988b784fd03495234dd75001e033e | |
parent | 67d5d1b742584ffeb9859eb7b92a41e0d365622d (diff) | |
download | polecat-e6279af295cc0e8b985a5a43a16ac6313eb6c698.tar.gz polecat-e6279af295cc0e8b985a5a43a16ac6313eb6c698.zip |
implement flags and sort commands by alphabet
stupid standard --help and --version
-rw-r--r-- | src/command.h | 22 | ||||
-rw-r--r-- | src/common.c | 55 | ||||
-rw-r--r-- | src/common.h | 15 | ||||
-rw-r--r-- | src/dxvk.c | 8 | ||||
-rw-r--r-- | src/lutris.c | 9 | ||||
-rw-r--r-- | src/main.c | 16 | ||||
-rw-r--r-- | src/main.h | 1 | ||||
-rw-r--r-- | src/wine.c | 14 |
8 files changed, 114 insertions, 26 deletions
diff --git a/src/command.h b/src/command.h index b45f8b9..54ce16f 100644 --- a/src/command.h +++ b/src/command.h @@ -28,8 +28,8 @@ #define COMMAND_HELP(GROUP, MSG) \ COMMAND(GROUP, help) \ { \ - fprintf(stderr, USAGE_STR MSG " <command>\n\nList of commands:\n"); \ - print_help(GROUP##_commands, ARRAY_LEN(GROUP##_commands)); \ + fprintf(stderr, USAGE_STR MSG " <command>\n"); \ + print_help(GROUP##_commands, ARRAY_LEN(GROUP##_commands), GROUP##_flags, ARRAY_LEN(GROUP##_flags)); \ return 0; \ } @@ -58,7 +58,23 @@ { \ for (unsigned long i = 0; i < ARRAY_LEN(GROUP##_commands); ++i) \ if (!strcmp(GROUP##_commands[i].name, argv[1])) return GROUP##_commands[i].func(argc-1, argv+1); \ - fprintf(stderr, NAME ": '%s' is not a command\n", argv[1]); \ + \ + for (int j = 1; j < argc; ++j) \ + { \ + if (argv[j][0] != '-') continue; \ + \ + for (unsigned long i = 0; i < ARRAY_LEN(GROUP##_flags); ++i) \ + { \ + if (GROUP##_flags[i].variant & SINGLE && argv[j][1] == GROUP##_flags[i].name[0] || \ + GROUP##_flags[i].variant & DOUBLE && argv[j][1] == '-' \ + && !strcmp(GROUP##_flags[i].name, argv[j]+2)) \ + { \ + return GROUP##_flags[i].func(0, NULL); \ + } \ + } \ + } \ + \ + fprintf(stderr, NAME ": '%s' is not a command or flag\n", argv[1]); \ return 0; \ } \ return GROUP##_help(argc-1, argv+1); \ diff --git a/src/common.c b/src/common.c index 36cd2d9..ae2087e 100644 --- a/src/common.c +++ b/src/common.c @@ -8,20 +8,59 @@ #include "common.h" -void print_help(const struct Command* commands, const size_t size) +void print_help(const struct Command* commands, const size_t commands_size, + const struct Flag* flags, size_t flags_size) { - size_t longestCommand = 0; + size_t longestStr; + size_t length; - for (size_t i = 0; i < size; ++i) + if (commands_size) { - size_t commandLength = strlen(commands[i].name); + longestStr = 0; - if (commandLength > longestCommand) longestCommand = commandLength; + for (size_t i = 0; i < commands_size; ++i) + { + length = strlen(commands[i].name); + + if (length > longestStr) longestStr = length; + } + + fprintf(stderr, "\nList of commands:\n"); + for (size_t i = 0; i < commands_size; ++i) + { + fprintf(stderr, "\t%-*s\t %s\n", (int)longestStr, commands[i].name, commands[i].description); + } } - for (size_t i = 0; i < size; ++i) + + if (flags_size) { - printf("\t%-*s\t %s\n", (int)longestCommand, commands[i].name, commands[i].description); + longestStr = 0; + + for (size_t i = 0; i < flags_size; ++i) + { + length = strlen(flags[i].name); + + if (length > longestStr) longestStr = length; + } + + fprintf(stderr, "\nList of flags:\n"); + for (size_t i = 0; i < flags_size; ++i) + { + fprintf(stderr, "\t"); + if (flags[i].variant & SINGLE) + fprintf(stderr, "-%c", flags[i].name[0]); + else + fprintf(stderr, " "); + + if (flags[i].variant & DOUBLE) + { + if (flags[i].variant & SINGLE) fprintf(stderr, ","); + fprintf(stderr, " --%-*s", (int)longestStr, flags[i].name); + } + + fprintf(stderr, "\t %s\n", flags[i].description); + } } } @@ -96,7 +135,7 @@ int removeDir(const char *path) if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) continue; - len = path_len + strlen(p->d_name) + 2; + len = path_len + strlen(p->d_name) + 2; buf = malloc(len); if (buf) { diff --git a/src/common.h b/src/common.h index 9680e8d..5f09c07 100644 --- a/src/common.h +++ b/src/common.h @@ -65,7 +65,20 @@ struct Command { char* description; }; -void print_help(const struct Command*, size_t); +enum flag_variants { + SINGLE = 1, + DOUBLE = 2, + BOTH = SINGLE + DOUBLE +}; + +struct Flag { + char* name; + enum flag_variants variant; + int (*func)(int, char**); + char* description; +}; + +void print_help(const struct Command*, size_t, const struct Flag*, size_t); struct stat getStat(const char* path); int isFile(const char*); @@ -14,10 +14,14 @@ static const struct Command dxvk_commands[] = { { .name = "download", .func = dxvk_download, .description = "download and install a dxvk version" }, - { .name = "remove", .func = dxvk_remove, .description = "remove a dxvk version" }, - { .name = "list", .func = dxvk_list, .description = "list available dxvk versions" }, { .name = "install", .func = dxvk_install, .description = "run the DXVK installer" }, + { .name = "list", .func = dxvk_list, .description = "list available dxvk versions" }, { .name = "list-installed", .func = dxvk_installed, .description = "list installed dxvk versions" }, + { .name = "remove", .func = dxvk_remove, .description = "remove a dxvk version" }, +}; + +static const struct Flag dxvk_flags[] = { + { .name = "help", .variant = DOUBLE, .func = dxvk_help, .description = "show this message"} }; COMMAND_GROUP_FUNC(dxvk) diff --git a/src/lutris.c b/src/lutris.c index 7d663c0..da893d1 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -9,12 +9,15 @@ #include "net.h" static const struct Command lutris_commands[] = { - { .name = "search", .func = lutris_search, .description = "search for lutris installers" }, - { .name = "list", .func = lutris_list, .description = "list installers for a game"}, - { .name = "install", .func = lutris_install, .description = "install a lutris script" }, { .name = "info", .func = lutris_info, .description = "show information about a lutris script" }, + { .name = "install", .func = lutris_install, .description = "install a lutris script" }, + { .name = "list", .func = lutris_list, .description = "list installers for a game"}, + { .name = "search", .func = lutris_search, .description = "search for lutris installers" }, }; +static const struct Flag lutris_flags[] = { + { .name = "help", .variant = DOUBLE, .func = lutris_help, .description = "show this message"} +}; char* getpwd() { @@ -17,6 +17,10 @@ static const struct Command main_commands[] = { { .name = "env", .func = main_env, .description = "show some information about polecat" }, }; +static const struct Flag main_flags[] = { + { .name = "help", .variant = DOUBLE, .func = main_help, .description = "show this message"}, + { .name = "version", .variant = BOTH, .func = main_version, .description = "prints the program version"} +}; COMMAND_GROUP_FUNC(main) @@ -25,10 +29,7 @@ COMMAND(main, env) char buffer[PATH_MAX]; - printf("version:\t\t%s\n" - "user-Agent:\t\t%s\n", - VERSION, - USER_AGENT); + printf("user-Agent:\t\t%s\n", USER_AGENT); getConfigDir(buffer, sizeof(buffer)); printf("config dir\t\t%s\n", buffer); @@ -40,4 +41,11 @@ COMMAND(main, env) return 0; } +COMMAND(main, version) +{ + puts(VERSION); + + return 0; +} + COMMAND_HELP(main, "") @@ -4,6 +4,7 @@ #include "command.h" COMMAND(main, env); +COMMAND(main, version); COMMAND(main, help); #endif @@ -16,12 +16,16 @@ static const struct Command wine_commands[] = { { .name = "download", .func = wine_download, .description = "download and extract a wine versions" }, - { .name = "remove", .func = wine_remove, .description = "remove a wine version" }, + { .name = "env", .func = wine_env, .description = "add wine to your PATH in a POSIX shell"}, + { .name = "env-fish", .func = wine_env, .description = "add wine to your PATH in the fish shell"}, { .name = "list", .func = wine_list, .description = "list installable wine versions" }, - { .name = "run", .func = wine_run, .description = "run an installed wine version" }, { .name = "list-installed", .func = wine_installed, .description = "list already installed wine versions" }, - { .name = "env", .func = wine_env, .description = "add wine to your PATH in a POSIX shell"}, - { .name = "fish-env", .func = wine_env, .description = "add wine to your PATH in the fish shell"}, + { .name = "remove", .func = wine_remove, .description = "remove a wine version" }, + { .name = "run", .func = wine_run, .description = "run an installed wine version" }, +}; + +static const struct Flag wine_flags[] = { + { .name = "help", .variant = DOUBLE, .func = wine_help, .description = "show this message"} }; COMMAND_GROUP_FUNC(wine) @@ -281,7 +285,7 @@ COMMAND(wine, env) if (argc > 1) { // instead of creating redundant copies we just check for fish - int fish_env = (strcmp(argv[0], "fish-env") == 0); + int fish_env = (strcmp(argv[0], "env-fish") == 0); char winepath[PATH_MAX]; char* winebinloc = NULL; // to be set by the wine type check |